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

Make an HTTP Request to Twilio


There are a lot of ways you can make an HTTP request to the Twilio API. You can make a raw HTTP request in your code (for example, using a module like got in NodeJS) or by using a tool like Postman. You might find it easier to use the Twilio Helper Library or SDK for your preferred programming language - even if that's $bash, and you need to use the Twilio CLI.


Credentials

All requests to Twilio's REST API need to be authenticated. Twilio supports two forms of authentication, both using HTTP basic auth, which use the following username/password schemes:

Account SID and Auth Token

The account SID and auth token are the master keys to your account. They can be found on your Account Dashboard in the Twilio Console.

UsernamePassword
AccountSidAuthToken

API Keys

API Keys are credentials you can create and revoke in the Console or using the API using your AccountSid and AuthToken. API Keys are typically safer to work within your Twilio projects. You can quickly delete them to revoke access if they become compromised and create a fresh API Key for different use cases - so, if you need to turn off a specific use case for some reason, you can just delete the API Key!

UsernamePassword
API Key SIDAPI Key Secret

The examples below will demonstrate how to use both username/password schemes with cURL, Twilio's SDKs, and the Twilio CLI.


HTTP Methods

Twilio mostly uses the GET, POST, and DELETE HTTP methods on various resources. Here's how all of these methods might be used for one of Twilio's most common resources: the SMS Message Resource.

Creating or Updating Resources with the POST Method

You can create or update a resource using the HTTP POST method on a resource URI. All you need to provide is the description of what it is that you want to make! The Twilio helper libraries are useful for making these requests simpler, but you can see the parameters for any post request in the documentation for a resource. Here's a POST request that will create and send an SMS using the Twilio API.

POST a New Message via SMS

Using Account SID and Auth Token for Authentication

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

_20
// Download the helper library from https://www.twilio.com/docs/node/install
_20
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_20
_20
// Find your Account SID and Auth Token at twilio.com/console
_20
// and set the environment variables. See http://twil.io/secure
_20
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_20
const authToken = process.env.TWILIO_AUTH_TOKEN;
_20
const client = twilio(accountSid, authToken);
_20
_20
async function createMessage() {
_20
const message = await client.messages.create({
_20
body: "This will be the body of the new message!",
_20
from: "+14155552344",
_20
to: "+14155552345",
_20
});
_20
_20
console.log(message.body);
_20
}
_20
_20
createMessage();

Output

_28
{
_28
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_28
"api_version": "2010-04-01",
_28
"body": "This will be the body of the new message!",
_28
"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
_28
"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
_28
"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
_28
"direction": "outbound-api",
_28
"error_code": null,
_28
"error_message": null,
_28
"from": "+14155552344",
_28
"num_media": "0",
_28
"num_segments": "1",
_28
"price": null,
_28
"price_unit": null,
_28
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_28
"sid": "SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_28
"status": "queued",
_28
"subresource_uris": {
_28
"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Media.json"
_28
},
_28
"tags": {
_28
"campaign_name": "Spring Sale 2022",
_28
"message_type": "cart_abandoned"
_28
},
_28
"to": "+14155552345",
_28
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"
_28
}

POST a New Message via SMS

Using API Key SID and Secret for Authentication

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

_22
// Download the helper library from https://www.twilio.com/docs/node/install
_22
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_22
_22
// Find your Account SID at twilio.com/console
_22
// Provision API Keys at twilio.com/console/runtime/api-keys
_22
// and set the environment variables. See http://twil.io/secure
_22
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_22
const apiKey = process.env.TWILIO_API_KEY;
_22
const apiSecret = process.env.TWILIO_API_SECRET;
_22
const client = twilio(apiKey, apiSecret, { accountSid: accountSid });
_22
_22
async function createMessage() {
_22
const message = await client.messages.create({
_22
body: "This will be the body of the new message!",
_22
from: "+14155552344",
_22
to: "+14155552345",
_22
});
_22
_22
console.log(message.body);
_22
}
_22
_22
createMessage();

Output

_28
{
_28
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_28
"api_version": "2010-04-01",
_28
"body": "This will be the body of the new message!",
_28
"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
_28
"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
_28
"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
_28
"direction": "outbound-api",
_28
"error_code": null,
_28
"error_message": null,
_28
"from": "+14155552344",
_28
"num_media": "0",
_28
"num_segments": "1",
_28
"price": null,
_28
"price_unit": null,
_28
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_28
"sid": "SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_28
"status": "queued",
_28
"subresource_uris": {
_28
"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Media.json"
_28
},
_28
"tags": {
_28
"campaign_name": "Spring Sale 2022",
_28
"message_type": "cart_abandoned"
_28
},
_28
"to": "+14155552345",
_28
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"
_28
}

Retrieving Resources with the GET Method

Congrats on sending a message! You can see in the output that you'll receive a Message SID that looks something like MM followed by a long string of letters and numbers. In the sample output, it's just a long string of X's. You can use that SID to retrieve information about the message using the GET method.

GET the sent SMS

Using Account SID and Auth Token for Authentication

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("MMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_18
.fetch();
_18
_18
console.log(message.body);
_18
}
_18
_18
fetchMessage();

Output

_29
{
_29
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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": "MMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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
}

GET the sent SMS

Using API Key SID and Secret for Authentication

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

_20
// Download the helper library from https://www.twilio.com/docs/node/install
_20
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_20
_20
// Find your Account SID at twilio.com/console
_20
// Provision API Keys at twilio.com/console/runtime/api-keys
_20
// and set the environment variables. See http://twil.io/secure
_20
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_20
const apiKey = process.env.TWILIO_API_KEY;
_20
const apiSecret = process.env.TWILIO_API_SECRET;
_20
const client = twilio(apiKey, apiSecret, { accountSid: accountSid });
_20
_20
async function fetchMessage() {
_20
const message = await client
_20
.messages("MMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_20
.fetch();
_20
_20
console.log(message.body);
_20
}
_20
_20
fetchMessage();

Output

_29
{
_29
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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": "MMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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
}

Deleting a Resource with the DELETE Method

Finally, there are times when you might want to delete the message. You can use the same Message SID to make an HTTP DELETE request. Note that not all Twilio REST API resources support DELETE.

DELETE the Message

Using Account SID and Auth Token for Authentication

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("MMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").remove();
_14
}
_14
_14
deleteMessage();

DELETE the Message

Using API Key SID and Secret for Authentication

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 at twilio.com/console
_16
// Provision API Keys at twilio.com/console/runtime/api-keys
_16
// and set the environment variables. See http://twil.io/secure
_16
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_16
const apiKey = process.env.TWILIO_API_KEY;
_16
const apiSecret = process.env.TWILIO_API_SECRET;
_16
const client = twilio(apiKey, apiSecret, { accountSid: accountSid });
_16
_16
async function deleteMessage() {
_16
await client.messages("SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").remove();
_16
}
_16
_16
deleteMessage();

Status Codes

As you make these requests, you'll see responses indicating what's happening on the other side of the server. The following is a list of common status codes you'll see in response from the Twilio API, and what they mean.

Status CodeMeaningDescription
200OKThe request was successful and the response body contains the representation requested.
201CREATEDThe request was successful, we created a new resource and the response body contains the representation. This should only appear for POST requests.
202ACCEPTEDThe request has been accepted for processing, but the processing has not been completed.
204OKUsed with the DELETE method. The request was successful; the resource was deleted.
302FOUNDA common redirect response; you can GET the representation at the URI in the Location response header.
304NOT MODIFIEDYour client's cached version of the representation is still up to date.
401UNAUTHORIZEDThe supplied credentials, if any, are not sufficient to access the resource.
404NOT FOUNDThe request resource wasn't found.
405NOT ALLOWEDTypically means you can't DELETE the resource.
429TOO MANY REQUESTSYour application is sending too many requests too quickly, and you are reaching the concurrency limit of the Twilio API.
500SERVER ERRORWe couldn't return the representation due to an internal server error - this one is Twilio's fault!
503SERVICE UNAVAILABLEWe are temporarily unable to return the representation. Please wait for a bit and try again.

Rate this page: