Skip to contentSkip to navigationSkip to topbar
Page tools
Useful for sharing or LLM

On this page
Looking for more inspiration?Visit the

Twilio API requests


Learn how to authenticate your requests, what content type to use for API requests, and how the Twilio APIs handle webhooks. You'll also see examples of how to make requests to the Twilio APIs.


Ways to make requests to the Twilio APIs

ways-to-make-requests-to-the-twilio-apis page anchor

You can make an HTTP request to the Twilio API in the following ways:


(information)

Environment variables

Always store your credentials in environment variables before sharing any code or deploying to production. Learn more about setting environment variables(link takes you to an external page).

To authenticate requests to the Twilio APIs, use HTTP Basic authentication(link takes you to an external page). Use your API key SID as the username and your API key secret as the password.

(information)

Regional API credentials

Twilio API credentials are region-specific resources. If your account uses Twilio Regions, see Manage Regional API credentials.

Use API keys

use-api-keys page anchor

An API key is a unique identifier that allows a client to access your Twilio account and create, read, update, or delete resources through the Twilio APIs. You can create multiple API keys for different purposes, such as for different developers or subsystems within your application. If a key is compromised or no longer used, you can revoke it to prevent unauthorized access.

Use API keys for all applications. If a key is compromised or no longer used, revoke it to prevent unauthorized access without affecting your other applications.

You can create an API key either in the Twilio Console or using the API.

The API key types are Main, Standard, and Restricted (Key resource v1 only). The following table describes each type:

Key typeAccess permissionsCreate in ConsoleCreate with REST API
MainFull access to all Twilio API resources. Equivalent to using your Account SID and Auth Token for API requests.YesNo
StandardAccess to all Twilio API resources, except for Accounts (/Accounts) or Keys (/Accounts/{SID}/Keys, /v1/Keys) resources.YesYes
RestrictedCustomized, fine-grained access to specific Twilio API resources. Learn more about Restricted API keys.YesYes (v1 only)

When making an API request, use your API key as the username and your API key secret as the password.

Note: In the following example, you must use a Main API key.

List accountsLink to code sample: List accounts
1
curl -X GET "https://api.twilio.com/2010-04-01/Accounts.json?PageSize=20" \
2
-u $TWILIO_API_KEY:$TWILIO_API_SECRET

The user remains logged in for the duration of the request. Learn more about how Twilio handles authentication.

Using your Account SID and Auth Token

using-your-account-sid-and-auth-token page anchor

For local testing, you can use your Account SID as the username and your Auth token as the password. You can find your Account SID and Auth Token in the Twilio Console(link takes you to an external page), under the Account Dashboard.

1
curl -X GET "https://api.twilio.com/2010-04-01/Accounts.json?PageSize=20" \
2
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

A Twilio SDK is a server-side SDK that helps you use Twilio's REST APIs, generate TwiML, and perform other common server-side programming tasks.

Twilio SDKs can handle credential management and simplify the authentication process. All Twilio SDKs come with a Utilities class that validates requests by passing your credentials to the library.


HTTP methods for API interactions

http-methods-for-api-interactions page anchor

Twilio APIs use standard RESTful HTTP methods to perform actions on resources. The following examples demonstrate the most common methods:

  • POST: Create or update a resource.
  • GET: Retrieve a resource.
  • DELETE: Delete a resource.
POSTGET: List messagesGET: Retrieve a messageDELETE
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID at twilio.com/console
5
// Provision API Keys at twilio.com/console/runtime/api-keys
6
// and set the environment variables. See http://twil.io/secure
7
// For local testing, you can use your Account SID and Auth token
8
const accountSid = process.env.TWILIO_ACCOUNT_SID;
9
const apiKey = process.env.TWILIO_API_KEY;
10
const apiSecret = process.env.TWILIO_API_SECRET;
11
const client = twilio(apiKey, apiSecret, { accountSid: accountSid });
12
13
async function createMessage() {
14
const message = await client.messages.create({
15
body: "Hello",
16
from: "+14155552344",
17
to: "+15558675310",
18
});
19
20
console.log(message.body);
21
}
22
23
createMessage();

Response

Note about this response
1
{
2
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
3
"api_version": "2010-04-01",
4
"body": "Hello",
5
"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
6
"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
7
"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
8
"direction": "outbound-api",
9
"error_code": null,
10
"error_message": null,
11
"from": "+14155552344",
12
"num_media": "0",
13
"num_segments": "1",
14
"price": null,
15
"price_unit": null,
16
"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
17
"sid": "SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
18
"status": "queued",
19
"subresource_uris": {
20
"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Media.json"
21
},
22
"to": "+15558675310",
23
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"
24
}

Content type requirements

content-type-requirements page anchor

Twilio APIs expect the API request content type to be application/x-www-form-urlencoded or multipart/form-data. Using an unsupported content type might cause unexpected behavior or errors.


Global Edge Locations and network entry

global-edge-locations-and-network-entry page anchor

Twilio global infrastructure allows you to optimize request routing for better performance. You can specify which Twilio network edge your API request uses to ingress Twilio network, and the processing region for your request. Learn more about Edge Locations.


Vanity URLs and security requirements

vanity-urls-and-security-requirements page anchor

Twilio has specific security requirements for accessing voice recording media files. Twilio doesn't support unauthenticated access to HTTP Voice recording media URLs using Canonical Name (CNAME) records. Use HTTPS endpoints and Transport-Layer-Security (TLS) protocols when accessing voice recordings media files from your account. For more information, see the Changelog(link takes you to an external page).


How the Twilio APIs handle webhooks

how-the-twilio-apis-handle-webhooks page anchor

Webhooks are user-defined HTTP callbacks triggered by an event in a web application. Twilio uses webhooks to let your application know when events happen, like getting an incoming call or receiving an SMS message. Webhooks are triggered asynchronously.

When a webhook event occurs, Twilio makes an HTTP request, such as POST or GET, to the URL you configured for your webhook. Twilio's request to your application includes details of the event like the body of an incoming message or an incoming phone number. Your application can then process the event and reply to Twilio with a response containing the instructions you'd like Twilio to perform.

To handle a webhook when you use Twilio, you need to build a web application that can accept HTTP requests. Check out the Twilio SDKs to get up and running quickly.


Explore these resources to keep building with the Twilio APIs: