Switchmast

Twilio error 21211: invalid 'To' phone number

Updated

Short answer

21211 (Invalid 'To' Phone Number) means Twilio can't use the number in your To parameter. It is usually not in E.164 format (a +, the country code, then digits only), has a typo or the wrong country code, or is the same number as From. Reformat it, or validate it first with a free Lookup request.

What Twilio says it means

Twilio's error reference names 21211 Invalid 'To' Phone Number, log type APPLICATION, level ERROR. It's a problem with your request, not with a carrier: the message never reached a network. Twilio requires the To number in E.164: a plus sign, the country calling code and the subscriber number, with no spaces, dashes or brackets.

Common causes, most likely first

  1. Not E.164. A missing +, a missing country code, punctuation, or a number in local format such as 07911 123456.
  2. A typo or the wrong length for the destination country: a digit missing or added when the number was typed into a form.
  3. The wrong country code. A UK number sent with +1 instead of +44 is a common one when a form defaults to the US.
  4. To and From are the same Twilio number.
  5. To is a short code or alphanumeric sender ID. Those can send messages but can't receive them.
  6. A prefix added on a Messaging Service request. Pass To as plain E.164, with no channel prefix.

Reformatting examples

What you haveCountryE.164
(555) 123-4567US+15551234567
555-123-4567US+15551234567
07911 123456UK+447911123456

Note the UK case: the leading 0 of the national number is dropped when you add +44.

How to check and fix it

  1. Log the exact value you sent. Twilio's error response carries a code (21211), a message and a more_info link. Print the To string next to it; stray spaces or a hidden character often show up straight away.
  2. Check the Console logs. In the Twilio Console, Monitor › Logs › Error Logs lists errors; click one for the request and response. Messages that were created appear under Monitor › Logs › Messaging, where you can search by To and From.
  3. Reformat the number in E.164 with the right country calling code, and look for missing or extra digits.
  4. Make sure To and From differ, and that To isn't a short code or sender ID.
  5. Using a plugin or third-party tool? Send the same number from the Console's "Send an SMS" try-it page. If it works there, the integration is probably changing the value before it reaches Twilio.

How to prevent it

CodeMeaning
21614'To' number is not a valid mobile number (usually a landline)
21606The 'From' number isn't a valid message-capable Twilio number for this account
21408The destination country is disabled in your Geo Permissions; a wrong country code can cause it too
30005A well-formed number the carrier doesn't know

How Switchmast helps

Switchmast's message log on iPhone with status filters
Switchmast's message log, on sample data.

Switchmast's message log has a Failed filter and searches by error code, and explains 21211 in plain English with the E.164 fix. When one form on your site starts sending badly formatted numbers, you see the failures together on your phone instead of in a weekly report. For more codes, see the Twilio error codes hub.

Questions

What does Twilio error 21211 mean?

Invalid 'To' Phone Number: the number you passed in the To parameter isn't a valid phone number, or it isn't formatted the way Twilio expects. Twilio wants E.164: a + then the country code and number, with no spaces, dashes or brackets.

What is E.164 format?

The international format Twilio uses for phone numbers: a plus sign, the country calling code and the subscriber number, digits only. (555) 123-4567 in the US becomes +15551234567; the UK mobile 07911 123456 becomes +447911123456.

Can I get 21211 if the number looks right?

Yes. Twilio also returns it when To and From are the same Twilio number, when To is a short code or alphanumeric sender ID (they can send but not receive), or when a channel prefix is added to To on a Messaging Service request.

How do I check a number before sending?

Use a basic Twilio Lookup v2 request, which is free. It returns the number in E.164, a valid flag and validation errors such as TOO_SHORT or INVALID_COUNTRY_CODE.

Sources