Back to Blog
Error-Code Fixes

Twilio Error 21211: Invalid To Phone Number: Causes and How to Fix It

The To number you passed is not a valid phone number. Error 21211 is one of the most common Twilio API errors and is always fixable in your code.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 21, 2026
6 min read
Twilio
Error
API
Troubleshooting
Twilio Error 21211: Invalid To Phone Number: Causes and How to Fix It

Twilio error 21211 is thrown synchronously at the API request level, before any carrier interaction takes place, because the To parameter you submitted does not parse as a recognizable phone number. This means the error is always in your application code or data layer, never in the carrier network. Fixing it is entirely within your control once you identify where the malformed number originated.

What Causes This Error

The most common cause is passing a number that lacks the leading plus sign required by the E.164 international format, such as submitting 12125551234 instead of +12125551234. A second cause is passing a number that contains non-numeric characters such as parentheses, dashes, or spaces, for example (212) 555-1234, which Twilio does not accept as-is in the To parameter. Numbers pulled from user input fields without sanitization are a frequent source of this error, because users enter phone numbers in many different regional formats that must be normalized before reaching the API. Passing an empty string, null, or undefined as the To value also triggers 21211, which often happens when a variable is not properly initialized before the API call is constructed.

How to Fix It Step by Step

First, log the exact value of the To parameter immediately before your Twilio API call and inspect it for the issues listed above: missing plus sign, non-numeric characters, or empty value. Apply E.164 normalization to every phone number in your codebase by stripping all non-digit characters, then prepending the correct country code and a plus sign, for example transforming (212) 555-1234 into +12125551234 for a US number. After normalization, validate the result against the E.164 regex pattern ^+[1-9]d{1,14}$ before passing it to the API, and throw an application-level error if the pattern does not match so the problem is caught before Twilio is called. Use the Twilio Lookup API (GET v1/PhoneNumbers/{number}) to confirm the number is not only syntactically valid but also recognized by carrier databases before sending any messages in production.

How to Prevent It from Recurring

Integrate E.164 normalization as a required step in your data pipeline at the point where phone numbers are first collected, using a library like libphonenumber-js in Node.js or phonenumbers in Python to parse and reformat numbers into the correct international standard regardless of how users enter them. Add a database constraint or column-level validation that rejects storing any phone number that does not match the E.164 pattern, ensuring that malformed numbers cannot propagate from your database into your messaging logic. Build a unit test suite that covers edge cases for your phone number normalization function, including numbers entered with country codes, without country codes, with formatting characters, and with leading zeros, and run these tests on every deployment. Set up monitoring on your Twilio error logs using the Console under Monitor, then Alerts, with a notification triggered any time 21211 errors appear, since even a single occurrence in production indicates a validation gap that needs immediate attention.

When to Call a Specialist

If 21211 errors persist after you have implemented E.164 normalization and the numbers look correctly formatted in your logs, the issue may be in how your framework or HTTP client is encoding the parameter before it reaches Twilio's API gateway, such as URL-encoding a plus sign as %2B and then double-encoding it. A specialist can trace the raw HTTP request your application sends to the Twilio API and identify encoding or serialization issues that are invisible in your application-layer logs. You should also seek specialist help if 21211 errors are appearing on numbers that were previously working, as this can indicate a data migration or schema change that stripped plus signs or country codes from stored records. A pattern of 21211 errors concentrated on numbers from a specific country or region often signals a country code normalization bug that only surfaces for that locale.

Conclusion

Error 21211 is a data quality error that is fixed entirely in your application layer by normalizing phone numbers to E.164 before every API call. If this error is blocking your production system, contact our team and we will diagnose and fix it within the hour.

Share this article:
0 views

Ready to Transform Your Business Communications?

Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.

Comments (0)

Join the discussion and share your thoughts (AI-moderated for quality)

Protected by AI moderation

Be the first to comment

No comments yet. Share your thoughts below.