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:
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.
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:
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function listMessage() {11const messages = await client.messages.list({ limit: 20 });1213messages.forEach((m) => console.log(m.body));14}1516listMessage();
1{2"end": 1,3"first_page_uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=0",4"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",5"page": 0,6"page_size": 2,7"previous_page_uri": null,8"messages": [9{10"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",11"api_version": "2010-04-01",12"body": "testing",13"date_created": "Fri, 24 May 2019 17:44:46 +0000",14"date_sent": "Fri, 24 May 2019 17:44:50 +0000",15"date_updated": "Fri, 24 May 2019 17:44:50 +0000",16"direction": "outbound-api",17"error_code": null,18"error_message": null,19"from": "+12019235161",20"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",21"num_media": "0",22"num_segments": "1",23"price": "-0.00750",24"price_unit": "USD",25"sid": "SMded05904ccb347238880ca9264e8fe1c",26"status": "sent",27"subresource_uris": {28"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c/Media.json",29"feedback": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c/Feedback.json"30},31"tags": {32"campaign_name": "Spring Sale 2022",33"message_type": "cart_abandoned"34},35"to": "+18182008801",36"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c.json"37},38{39"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",40"api_version": "2010-04-01",41"body": "look mom I have media!",42"date_created": "Fri, 24 May 2019 17:44:46 +0000",43"date_sent": "Fri, 24 May 2019 17:44:49 +0000",44"date_updated": "Fri, 24 May 2019 17:44:49 +0000",45"direction": "inbound",46"error_code": 30004,47"error_message": "Message blocked",48"from": "+12019235161",49"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",50"num_media": "3",51"num_segments": "1",52"price": "-0.00750",53"price_unit": "USD",54"sid": "MMc26223853f8c46b4ab7dfaa6abba0a26",55"status": "received",56"subresource_uris": {57"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26/Media.json",58"feedback": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26/Feedback.json"59},60"tags": {61"campaign_name": "Spring Sale 2022",62"message_type": "cart_abandoned"63},64"to": "+18182008801",65"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26.json"66}67],68"start": 0,69"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:
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function listMessage() {11const messages = await client.messages.list({12dateSent: new Date("2016-08-31 00:00:00"),13from: "+15017122661",14to: "+15558675310",15limit: 20,16});1718messages.forEach((m) => console.log(m.body));19}2021listMessage();
1{2"end": 1,3"first_page_uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=0",4"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",5"page": 0,6"page_size": 2,7"previous_page_uri": null,8"messages": [9{10"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",11"api_version": "2010-04-01",12"body": "testing",13"date_created": "Fri, 24 May 2019 17:44:46 +0000",14"date_sent": "Fri, 24 May 2019 17:44:50 +0000",15"date_updated": "Fri, 24 May 2019 17:44:50 +0000",16"direction": "outbound-api",17"error_code": null,18"error_message": null,19"from": "+12019235161",20"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",21"num_media": "0",22"num_segments": "1",23"price": "-0.00750",24"price_unit": "USD",25"sid": "SMded05904ccb347238880ca9264e8fe1c",26"status": "sent",27"subresource_uris": {28"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c/Media.json",29"feedback": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c/Feedback.json"30},31"tags": {32"campaign_name": "Spring Sale 2022",33"message_type": "cart_abandoned"34},35"to": "+18182008801",36"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c.json"37},38{39"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",40"api_version": "2010-04-01",41"body": "look mom I have media!",42"date_created": "Fri, 24 May 2019 17:44:46 +0000",43"date_sent": "Fri, 24 May 2019 17:44:49 +0000",44"date_updated": "Fri, 24 May 2019 17:44:49 +0000",45"direction": "inbound",46"error_code": 30004,47"error_message": "Message blocked",48"from": "+12019235161",49"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",50"num_media": "3",51"num_segments": "1",52"price": "-0.00750",53"price_unit": "USD",54"sid": "MMc26223853f8c46b4ab7dfaa6abba0a26",55"status": "received",56"subresource_uris": {57"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26/Media.json",58"feedback": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26/Feedback.json"59},60"tags": {61"campaign_name": "Spring Sale 2022",62"message_type": "cart_abandoned"63},64"to": "+18182008801",65"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26.json"66}67],68"start": 0,69"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=0"70}
If you know the message SID (i.e. the message's unique identifier), then you can retrieve that specific message directly.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function fetchMessage() {11const message = await client12.messages("MM800f449d0399ed014aae2bcc0cc2f2ec")13.fetch();1415console.log(message.body);16}1718fetchMessage();
1{2"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"api_version": "2010-04-01",4"body": "testing",5"date_created": "Fri, 24 May 2019 17:18:27 +0000",6"date_sent": "Fri, 24 May 2019 17:18:28 +0000",7"date_updated": "Fri, 24 May 2019 17:18:28 +0000",8"direction": "outbound-api",9"error_code": 30007,10"error_message": "Carrier violation",11"from": "+12019235161",12"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",13"num_media": "0",14"num_segments": "1",15"price": "-0.00750",16"price_unit": "USD",17"sid": "MM800f449d0399ed014aae2bcc0cc2f2ec",18"status": "sent",19"subresource_uris": {20"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMb7c0a2ce80504485a6f653a7110836f5/Media.json",21"feedback": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMb7c0a2ce80504485a6f653a7110836f5/Feedback.json"22},23"tags": {24"campaign_name": "Spring Sale 2022",25"message_type": "cart_abandoned"26},27"to": "+18182008801",28"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.
If you want to delete a message from history, you can do so by deleting the Message instance resource.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function deleteMessage() {11await client.messages("MM800f449d0399ed014aae2bcc0cc2f2ec").remove();12}1314deleteMessage();
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):
1// Download the Node helper library from twilio.com/docs/node/install2// These consts are your accountSid and authToken from https://www.twilio.com/console3// To set up environmental variables, see http://twil.io/secure4const accountSid = process.env.TWILIO_ACCOUNT_SID;5const authToken = process.env.TWILIO_AUTH_TOKEN;6const client = require('twilio')(accountSid, authToken);78client9.messages('MM800f449d0399ed014aae2bcc0cc2f2ec')10.update({ body: '' })11.then(message => process.stdout.write(message.body));
1{2"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"api_version": "2010-04-01",4"body": "",5"error_code": null,6"error_message": null,7"num_segments": "1",8"num_media": "0",9"date_created": "Mon, 16 Aug 2010 03:45:01 +0000",10"date_sent": "Mon, 16 Aug 2010 03:45:03 +0000",11"date_updated": "Mon, 16 Aug 2010 03:45:03 +0000",12"direction": "outbound-api",13"from": "+14158141829",14"price": "-0.02000",15"sid": "MM800f449d0399ed014aae2bcc0cc2f2ec",16"status": "sent",17"to": "+15558675310",18"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/MM800f449d0399ed014aae2bcc0cc2f2ec.json"19}