Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Retrieve and Modify Message History


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
  • Twilio executes a <Message> TwiML instruction
  • 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.

Looking to send WhatsApp messages with Twilio? Try one of the WhatsApp quickstarts.

If you're looking for how to respond to incoming messages, check out the How to Receive and Reply to SMS Messages tutorial.


Search Previous Messages

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
_16
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_16
const authToken = process.env.TWILIO_AUTH_TOKEN;
_16
const client = twilio(accountSid, authToken);
_16
_16
async function listMessage() {
_16
const messages = await client.messages.list({ limit: 20 });
_16
_16
messages.forEach((m) => console.log(m.body));
_16
}
_16
_16
listMessage();

Output

_70
{
_70
"end": 1,
_70
"first_page_uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=0",
_70
"next_page_uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=1&PageToken=PAMMc26223853f8c46b4ab7dfaa6abba0a26",
_70
"page": 0,
_70
"page_size": 2,
_70
"previous_page_uri": null,
_70
"messages": [
_70
{
_70
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_70
"api_version": "2010-04-01",
_70
"body": "testing",
_70
"date_created": "Fri, 24 May 2019 17:44:46 +0000",
_70
"date_sent": "Fri, 24 May 2019 17:44:50 +0000",
_70
"date_updated": "Fri, 24 May 2019 17:44:50 +0000",
_70
"direction": "outbound-api",
_70
"error_code": null,
_70
"error_message": null,
_70
"from": "+12019235161",
_70
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_70
"num_media": "0",
_70
"num_segments": "1",
_70
"price": "-0.00750",
_70
"price_unit": "USD",
_70
"sid": "SMded05904ccb347238880ca9264e8fe1c",
_70
"status": "sent",
_70
"subresource_uris": {
_70
"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c/Media.json",
_70
"feedback": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c/Feedback.json"
_70
},
_70
"tags": {
_70
"campaign_name": "Spring Sale 2022",
_70
"message_type": "cart_abandoned"
_70
},
_70
"to": "+18182008801",
_70
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c.json"
_70
},
_70
{
_70
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_70
"api_version": "2010-04-01",
_70
"body": "look mom I have media!",
_70
"date_created": "Fri, 24 May 2019 17:44:46 +0000",
_70
"date_sent": "Fri, 24 May 2019 17:44:49 +0000",
_70
"date_updated": "Fri, 24 May 2019 17:44:49 +0000",
_70
"direction": "inbound",
_70
"error_code": 30004,
_70
"error_message": "Message blocked",
_70
"from": "+12019235161",
_70
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_70
"num_media": "3",
_70
"num_segments": "1",
_70
"price": "-0.00750",
_70
"price_unit": "USD",
_70
"sid": "MMc26223853f8c46b4ab7dfaa6abba0a26",
_70
"status": "received",
_70
"subresource_uris": {
_70
"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26/Media.json",
_70
"feedback": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26/Feedback.json"
_70
},
_70
"tags": {
_70
"campaign_name": "Spring Sale 2022",
_70
"message_type": "cart_abandoned"
_70
},
_70
"to": "+18182008801",
_70
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26.json"
_70
}
_70
],
_70
"start": 0,
_70
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=0"
_70
}

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
_21
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_21
const authToken = process.env.TWILIO_AUTH_TOKEN;
_21
const client = twilio(accountSid, authToken);
_21
_21
async function listMessage() {
_21
const messages = await client.messages.list({
_21
dateSent: new Date("2016-08-31 00:00:00"),
_21
from: "+15017122661",
_21
to: "+15558675310",
_21
limit: 20,
_21
});
_21
_21
messages.forEach((m) => console.log(m.body));
_21
}
_21
_21
listMessage();

Output

_70
{
_70
"end": 1,
_70
"first_page_uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=0",
_70
"next_page_uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=1&PageToken=PAMMc26223853f8c46b4ab7dfaa6abba0a26",
_70
"page": 0,
_70
"page_size": 2,
_70
"previous_page_uri": null,
_70
"messages": [
_70
{
_70
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_70
"api_version": "2010-04-01",
_70
"body": "testing",
_70
"date_created": "Fri, 24 May 2019 17:44:46 +0000",
_70
"date_sent": "Fri, 24 May 2019 17:44:50 +0000",
_70
"date_updated": "Fri, 24 May 2019 17:44:50 +0000",
_70
"direction": "outbound-api",
_70
"error_code": null,
_70
"error_message": null,
_70
"from": "+12019235161",
_70
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_70
"num_media": "0",
_70
"num_segments": "1",
_70
"price": "-0.00750",
_70
"price_unit": "USD",
_70
"sid": "SMded05904ccb347238880ca9264e8fe1c",
_70
"status": "sent",
_70
"subresource_uris": {
_70
"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c/Media.json",
_70
"feedback": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c/Feedback.json"
_70
},
_70
"tags": {
_70
"campaign_name": "Spring Sale 2022",
_70
"message_type": "cart_abandoned"
_70
},
_70
"to": "+18182008801",
_70
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c.json"
_70
},
_70
{
_70
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_70
"api_version": "2010-04-01",
_70
"body": "look mom I have media!",
_70
"date_created": "Fri, 24 May 2019 17:44:46 +0000",
_70
"date_sent": "Fri, 24 May 2019 17:44:49 +0000",
_70
"date_updated": "Fri, 24 May 2019 17:44:49 +0000",
_70
"direction": "inbound",
_70
"error_code": 30004,
_70
"error_message": "Message blocked",
_70
"from": "+12019235161",
_70
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_70
"num_media": "3",
_70
"num_segments": "1",
_70
"price": "-0.00750",
_70
"price_unit": "USD",
_70
"sid": "MMc26223853f8c46b4ab7dfaa6abba0a26",
_70
"status": "received",
_70
"subresource_uris": {
_70
"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26/Media.json",
_70
"feedback": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26/Feedback.json"
_70
},
_70
"tags": {
_70
"campaign_name": "Spring Sale 2022",
_70
"message_type": "cart_abandoned"
_70
},
_70
"to": "+18182008801",
_70
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26.json"
_70
}
_70
],
_70
"start": 0,
_70
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=0"
_70
}


Retrieve a Single Message

If you know the message SID (i.e. the message's unique identifier), then you can retrieve that specific message directly.

Fetch a single Message

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_18
// Download the helper library from https://www.twilio.com/docs/node/install
_18
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_18
_18
// Find your Account SID and Auth Token at twilio.com/console
_18
// and set the environment variables. See http://twil.io/secure
_18
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_18
const authToken = process.env.TWILIO_AUTH_TOKEN;
_18
const client = twilio(accountSid, authToken);
_18
_18
async function fetchMessage() {
_18
const message = await client
_18
.messages("MM800f449d0399ed014aae2bcc0cc2f2ec")
_18
.fetch();
_18
_18
console.log(message.to);
_18
}
_18
_18
fetchMessage();

Output

_29
{
_29
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_29
"api_version": "2010-04-01",
_29
"body": "testing",
_29
"date_created": "Fri, 24 May 2019 17:18:27 +0000",
_29
"date_sent": "Fri, 24 May 2019 17:18:28 +0000",
_29
"date_updated": "Fri, 24 May 2019 17:18:28 +0000",
_29
"direction": "outbound-api",
_29
"error_code": 30007,
_29
"error_message": "Carrier violation",
_29
"from": "+12019235161",
_29
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_29
"num_media": "0",
_29
"num_segments": "1",
_29
"price": "-0.00750",
_29
"price_unit": "USD",
_29
"sid": "MM800f449d0399ed014aae2bcc0cc2f2ec",
_29
"status": "sent",
_29
"subresource_uris": {
_29
"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMb7c0a2ce80504485a6f653a7110836f5/Media.json",
_29
"feedback": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMb7c0a2ce80504485a6f653a7110836f5/Feedback.json"
_29
},
_29
"tags": {
_29
"campaign_name": "Spring Sale 2022",
_29
"message_type": "cart_abandoned"
_29
},
_29
"to": "+18182008801",
_29
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMb7c0a2ce80504485a6f653a7110836f5.json"
_29
}

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
_14
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_14
const authToken = process.env.TWILIO_AUTH_TOKEN;
_14
const client = twilio(accountSid, authToken);
_14
_14
async function deleteMessage() {
_14
await client.messages("MM800f449d0399ed014aae2bcc0cc2f2ec").remove();
_14
}
_14
_14
deleteMessage();

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
_11
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_11
const authToken = process.env.TWILIO_AUTH_TOKEN;
_11
const client = require('twilio')(accountSid, authToken);
_11
_11
client
_11
.messages('MM800f449d0399ed014aae2bcc0cc2f2ec')
_11
.update({ body: '' })
_11
.then(message => process.stdout.write(message.body));

Output

_19
{
_19
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_19
"api_version": "2010-04-01",
_19
"body": "",
_19
"error_code": null,
_19
"error_message": null,
_19
"num_segments": "1",
_19
"num_media": "0",
_19
"date_created": "Mon, 16 Aug 2010 03:45:01 +0000",
_19
"date_sent": "Mon, 16 Aug 2010 03:45:03 +0000",
_19
"date_updated": "Mon, 16 Aug 2010 03:45:03 +0000",
_19
"direction": "outbound-api",
_19
"from": "+14158141829",
_19
"price": "-0.02000",
_19
"sid": "MM800f449d0399ed014aae2bcc0cc2f2ec",
_19
"status": "sent",
_19
"to": "+15558675310",
_19
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/MM800f449d0399ed014aae2bcc0cc2f2ec.json"
_19
}


Rate this page: