Skip to contentSkip to navigationSkip to topbar
On this page

How to use the Twilio APIs


The Twilio APIs are organized around REST. Behind these APIs is a software layer connecting and optimizing communications networks around the world. With the Twilio APIs, you can build applications that help your users send messages, make phone calls, look up phone numbers, and more.

In Twilio's ecosystem, each product corresponds to an API. For example, Twilio Messaging corresponds to the Messaging API. Although each API has its own unique features, they all share a common set of principles and practices. This consistency allows you to work with different Twilio APIs in a similar way. You can use the APIs directly over HTTPS or through Twilio helper libraries available for several programming languages.

In this guide, you'll learn about how to authenticate your requests to the Twilio APIs, what content type to use for API requests, and how the Twilio APIs handle webhooks.


Twilio API authentication

twilio-api-authentication page anchor

To authenticate requests to the Twilio APIs, Twilio supports HTTP Basic authentication(link takes you to an external page). You can use the following credentials:

UsernamePasswordBest practice
API KeyAPI Key SecretThis is the recommended way to authenticate with the Twilio APIs. When a key is compromised or no longer used, revoke it to prevent unauthorized access.
Account SIDAuthTokenLimit your use to local testing.
(information)

Info

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

Using your Account SID and Auth Token

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

Twilio doesn't recommend using your Account SID and Auth Token in production. If a bad actor gains access to your Account SID and Auth Token, then your Twilio Account is compromised.

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 -G https://api.twilio.com/2010-04-01/Accounts \
2
-u $YOUR_ACCOUNT_SID:$YOUR_AUTH_TOKEN

A Twilio helper library is a server-side SDK that helps you use Twilio's REST APIs, generate TwiML, and perform other common server-side programming tasks. All Twilio helper libraries come with a Utilities class that validates requests by passing your credentials to the library.

(error)

Danger

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


Content type for API Requests

content-type-for-api-requests page anchor

Twilio's 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.


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 officially supported helper libraries to get up and running quickly.