HTTP methods for API interactions
To build secure and reliable integrations, you must understand REST API methods. Each HTTP method has a specific purpose. Correct usage ensures predictable behavior and proper error handling for your app.
Twilio's REST API uses standard HTTP methods to perform operations on resources. Each method maps to a specific action that determines how your request is processed:
| Purpose | HTTP method | Action | Never use |
|---|---|---|---|
| Retrieve collection | GET | Retrieves a collection of resources of a specific type. | |
| Retrieve resources | GET | Retrieves a specific resource using its SID. | |
| Create collection | POST | Creates a new resource by sending data to a collection endpoint. | |
| Update resources | POST | Updates an existing resource using its SID. | GET,PUT,PATCH |
| Remove resources | DELETE | Deletes an existing resource. |
The following examples demonstrate each HTTP method. Select your method to see the request structure:
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 at twilio.com/console5// Provision API Keys at twilio.com/console/runtime/api-keys6// and set the environment variables. See http://twil.io/secure7// For local testing, you can use your Account SID and Auth token8const accountSid = process.env.TWILIO_ACCOUNT_SID;9const apiKey = process.env.TWILIO_API_KEY;10const apiSecret = process.env.TWILIO_API_SECRET;11const client = twilio(apiKey, apiSecret, { accountSid: accountSid });1213async function listMessage() {14const messages = await client.messages.list({ limit: 20 });1516messages.forEach((m) => console.log(m.end));17}1819listMessage();
Response
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"to": "+18182008801",32"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMded05904ccb347238880ca9264e8fe1c.json"33},34{35"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",36"api_version": "2010-04-01",37"body": "look mom I have media!",38"date_created": "Fri, 24 May 2019 17:44:46 +0000",39"date_sent": "Fri, 24 May 2019 17:44:49 +0000",40"date_updated": "Fri, 24 May 2019 17:44:49 +0000",41"direction": "inbound",42"error_code": 30004,43"error_message": "Message blocked",44"from": "+12019235161",45"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",46"num_media": "3",47"num_segments": "1",48"price": "-0.00750",49"price_unit": "USD",50"sid": "MMc26223853f8c46b4ab7dfaa6abba0a26",51"status": "received",52"subresource_uris": {53"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26/Media.json",54"feedback": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26/Feedback.json"55},56"to": "+18182008801",57"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/MMc26223853f8c46b4ab7dfaa6abba0a26.json"58}59],60"start": 0,61"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages.json?To=%2B123456789&From=%2B987654321&DateSent%3E=2008-01-02&PageSize=2&Page=0"62}
Twilio returns standardized HTTP status codes that indicate the success or failure of your request. Understanding these codes helps you implement proper error handling and detect security issues:
| Code | Error | Reason |
|---|---|---|
400 | Bad Request | Malformed request syntax or invalid parameters |
401 | Unauthorized | Missing, invalid, or expired credentials |
403 | Forbidden | Credentials are valid but lack permission for this action |
404 | Not Found | Resource does not exist or belongs to a different account |
429 | Too Many Requests | Rate limit or concurrency limit exceeded |
HTTP status codes provide critical information for detecting security issues and API problems.
Certain responses indicate authentication or authorization problems: 401 (Unauthorized) and 403 (Forbidden).
| Code | Error | Reason | Security implication | Remedy |
|---|---|---|---|---|
401 | Unauthorized | Missing, invalid, or expired credentials | Sudden spikes in 401 errors may indicate credential compromise or token expiration issues. | Verify credentials remain correct and haven't expired; rotate if suspicious. |
403 | Forbidden | Credentials lack permission for the requested action | Access to endpoints your integration doesn't normally use may indicate unauthorized changes to access policies. | Verify API key scopes and permissions; investigate unusual access patterns. |
Rate limiting at Twilio refers to the controls Twilio uses to govern how many requests an application can make, how fast outbound communications can be sent, and how services are protected from misuse.
429 Too Many Requests indicates you've exceeded rate limits or concurrency limits. This may indicate a legitimate spike in usage or an unauthorized user making rapid requests.
- Implement exponential backoff to retry after the rate limit resets.
- Monitor for sudden
429spikes that could indicate compromise or abuse.
For retry guidance and the response headers to monitor, see Know your rate limits.
500 and 503 errors indicate server-side issues.
- Implement retry logic with exponential backoff. Don't retry indefinitely.
5xxerrors should be rare; frequent 5xx responses may indicate a systemic problem.- Distinguish between transient failures, where you would retry, and systemic problems, where you escalate to Twilio Support.
Set up monitoring and alerts for these security-relevant patterns:
- Sudden spikes in
401or403responses from possible credential compromise or permission changes - Repeated
429responses from rate limiting, possible abuse, or Denial of Service (DoS) - Access to new endpoints or resources not previously used
- Requests with unusual geographic origin or timing patterns
- Mixing of expected and unexpected HTTP methods like unexpected
DELETErequests
Implement these practices when making HTTP requests to Twilio APIs:
To perform certain actions, use the following HTTP methods with the Twilio REST API:
| Purpose | HTTP method | Never use |
|---|---|---|
| Retrieve resources | GET | |
| Create resources | POST | |
| Update resources | POST | GET, PUT, PATCH |
| Remove resources | DELETE |
- Implement proper error handling for all status codes, not just successful responses.
- Implement exponential backoff when retrying errors (
5xx, occasional429).. - Log all error responses with status code, timestamp, and endpoint for audit trails.
- Monitor specific status codes to detect security or operational issues.
- Don't retry
4xxerrors indefinitely; investigate the root cause.
- Monitor
401and403responses for patterns that could indicate compromise. - Alert on sudden changes in request patterns or unusual geographic origins.
- Implement rate limiting on your app side to prevent hitting Twilio limits.
- Test error handling during development; don't skip error scenarios.
Secure your API interactions and error handling:
- Defense-in-depth strategy: comprehensive security approach including monitoring
- Incident response: responding to detected security issues
- Webhook security: validating inbound requests and responses
- API in transit security: encryption and secure communication
- Twilio REST API reference: detailed API documentation