This guide will show you how you can search, retrieve, and modify the messages you send or receive with Programmable Messaging, using the Message Resource.
A Message resource represents an inbound or outbound message. Twilio creates a Message when any of the following occur:
You create a Message resource (i.e., send an outbound message) via the
REST API
Someone sends a message to one of your Twilio numbers or messaging channel addresses
(information)
Info
For step-by-step instructions for sending your first SMS message with Twilio, check out one of the SMS quickstarts.
For detailed instructions on setting up your local environment to code in all of our supported programming languages, see the Environment Setup section of this Guide.
When you send an SMS or MMS message via the REST API, using the <Message> verb in TwiML, or someone sends a message to one of your Twilio numbers or other channels, Twilio creates a Message instance resource. The Messages list resource represents the set of messages sent from and received by an account.
Retrieving sent and received messages from history can be achieved by querying the Messages list resource. Here you can see how to retrieve all messages from your account:
List all Messages in your account
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl
_16
// Download the helper library from https://www.twilio.com/docs/node/install
_16
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_16
_16
// Find your Account SID and Auth Token at twilio.com/console
_16
// and set the environment variables. See http://twil.io/secure
If you'd like to have Twilio narrow down this list of messages for you, you can do so by specifying a To number, From number, and a DateSent. The following example shows passing all three but you can pass any combination of parameters you need. This example filters messages for those sent from a specific number to another specific number on or after a certain date:
List Messages matching filter criteria
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl
_21
// Download the helper library from https://www.twilio.com/docs/node/install
_21
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_21
_21
// Find your Account SID and Auth Token at twilio.com/console
_21
// and set the environment variables. See http://twil.io/secure
How might you know the SID? When sending a message using the REST API, you will receive a Message instance resource as the response from Twilio. Using this, you can inspect the Sid property of the resource. Read more about sending messages in our guide on the topic.
When using the <Message> verb in TwiML, you will need to specify a webhook URL the action attribute to have Twilio call your webhook when the status of the message changes. Your webhook will be passed a MessageSid parameter identifying the incoming message. Read our guide on tracking message status for more on how to do this.
When receiving a message, your webhook will be passed a MessageSid parameter identifying the incoming message. You can learn more about receiving messages here.
However you obtain the SID, you can immediately request the message using the above code, or, you can save the SID in a database for later recall.
Delete or Redact Previously Sent Messages
If you want to delete a message from history, you can easily do so by deleting the Message instance resource.
Delete a Message
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl
_14
// Download the helper library from https://www.twilio.com/docs/node/install
_14
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_14
_14
// Find your Account SID and Auth Token at twilio.com/console
_14
// and set the environment variables. See http://twil.io/secure
Perhaps you want to redact the body of the message for security purposes, but you don't want to delete the message from history entirely. Redacting a message is done by posting an empty body to the message resource (i.e., this is a specific use of the Message update call):
Redact the body of a Message
Node.js
Python
C#
Java
PHP
Ruby
curl
_11
// Download the Node helper library from twilio.com/docs/node/install
_11
// These consts are your accountSid and authToken from https://www.twilio.com/console
_11
// To set up environmental variables, see http://twil.io/secure