<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Packet Exchange</title>
    <description>The latest articles on DEV Community by Packet Exchange (packetexchangeio).</description>
    <link>https://dev.to/packetexchangeio</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F16476%2Fbf2e1068-81e8-4287-8be9-6a13eec18e2d.png</url>
      <title>DEV Community: Packet Exchange</title>
      <link>https://dev.to/packetexchangeio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/packetexchangeio"/>
    <language>en</language>
    <item>
      <title>Phone numbers in E.164: the formatting bugs that break SMS and calls</title>
      <dc:creator>Packet Exchange</dc:creator>
      <pubDate>Fri, 27 Nov 2026 15:00:01 +0000</pubDate>
      <link>https://dev.to/packetexchangeio/phone-numbers-in-e164-the-formatting-bugs-that-break-sms-and-calls-4de0</link>
      <guid>https://dev.to/packetexchangeio/phone-numbers-in-e164-the-formatting-bugs-that-break-sms-and-calls-4de0</guid>
      <description>&lt;p&gt;&lt;em&gt;AI disclosure: this article was written with the help of AI and checked by a human before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most "SMS not delivered" and "call failed" tickets I have seen were not network problems. They were phone numbers stored in a shape the carrier could not route. The fix is boring and it works: store every number in E.164 and convert at the edges.&lt;/p&gt;

&lt;h2&gt;
  
  
  What E.164 is
&lt;/h2&gt;

&lt;p&gt;E.164 is the international numbering format. It is a plus sign, the country code, then the national number with no trunk prefix, no spaces and no punctuation. At most 15 digits after the plus.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UK mobile written locally: &lt;code&gt;07700 900123&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The same number in E.164: &lt;code&gt;+447700900123&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A Spanish mobile: &lt;code&gt;+34612345678&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A US number: &lt;code&gt;+12025550143&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note what happened to the UK number. The leading &lt;code&gt;0&lt;/code&gt; is a trunk prefix used for dialling inside the country. It is dropped when the country code goes on. Leaving it in (&lt;code&gt;+4407700900123&lt;/code&gt;) is the single most common bug I see.&lt;/p&gt;

&lt;h2&gt;
  
  
  Five bugs that come from not normalising
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The kept trunk zero.&lt;/strong&gt; &lt;code&gt;+4407700900123&lt;/code&gt; looks plausible and fails. A blanket "strip the zero" rule is not the answer either: Italian landlines keep their leading zero after &lt;code&gt;+39&lt;/code&gt;, so the same rule breaks them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Double country codes.&lt;/strong&gt; A form pre-fills &lt;code&gt;+44&lt;/code&gt;, the user types &lt;code&gt;447700900123&lt;/code&gt;, and you store &lt;code&gt;+44447700900123&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spreadsheet damage.&lt;/strong&gt; A CSV export turns &lt;code&gt;447700900123&lt;/code&gt; into &lt;code&gt;4.47701E+11&lt;/code&gt;, or strips the leading plus.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Duplicate users.&lt;/strong&gt; &lt;code&gt;07700900123&lt;/code&gt; and &lt;code&gt;+447700900123&lt;/code&gt; are the same person, but your unique index does not know that. One-time codes go to the wrong account record.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Formatting characters.&lt;/strong&gt; Spaces, dashes, brackets and the invisible characters that come with copy and paste from a web page.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Normalise on the way in
&lt;/h2&gt;

&lt;p&gt;Do not write your own parser. Use libphonenumber or one of its ports, and pass the user's country as the default region.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;parsePhoneNumberFromString&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'libphonenumber-js'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toE164&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;defaultCountry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'GB'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parsePhoneNumberFromString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;defaultCountry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// reject, do not guess&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// always E.164, for example +447700900123&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;phonenumbers&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_e164&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default_region&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"GB"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;phonenumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default_region&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;phonenumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NumberParseException&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;phonenumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_valid_number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;phonenumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format_number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;phonenumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PhoneNumberFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;E164&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Rules that keep it fixed
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Store E.164 only. Put a check constraint on the column that it starts with &lt;code&gt;+&lt;/code&gt; and has 8 to 15 digits.&lt;/li&gt;
&lt;li&gt;Make the unique index on the E.164 value, not on whatever the user typed.&lt;/li&gt;
&lt;li&gt;Format for humans only at display time.&lt;/li&gt;
&lt;li&gt;Send E.164 to every SMS and voice API, even when the docs say local formats are accepted.&lt;/li&gt;
&lt;li&gt;Treat CSV imports as untrusted input and run every row through the same function.&lt;/li&gt;
&lt;li&gt;Log the raw input next to the normalised value for a week after launch. It shows you which formats your users really type.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it yourself
&lt;/h2&gt;

&lt;p&gt;To check your normaliser against real numbers, &lt;a href="https://packetexchange.io" rel="noopener noreferrer"&gt;sign up&lt;/a&gt; and run the number lookup example on its output. A lookup returns the number type, the network and any risk flags, and it costs nothing.&lt;/p&gt;

&lt;p&gt;The code is open on GitHub:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/PacketExchangeIO/packetexchange-examples" rel="noopener noreferrer"&gt;Runnable examples in eight languages&lt;/a&gt;, including number lookup, SMS and verification&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/PacketExchangeIO/packetexchange-node" rel="noopener noreferrer"&gt;Node.js and TypeScript library&lt;/a&gt; and &lt;a href="https://github.com/PacketExchangeIO/packetexchange-python" rel="noopener noreferrer"&gt;Python library&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/PacketExchangeIO/packetexchange-mcp" rel="noopener noreferrer"&gt;MCP server&lt;/a&gt;: point Claude, Cursor or any MCP client at &lt;code&gt;https://packetexchange.io/mcp/http&lt;/code&gt; and your agent can look up and price a number before it sends. &lt;a href="https://packetexchange.io/mcp" rel="noopener noreferrer"&gt;Read the MCP overview&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What is the strangest phone number format you have found in a production database?&lt;/p&gt;

</description>
      <category>backend</category>
      <category>debugging</category>
      <category>software</category>
    </item>
  </channel>
</rss>
