Format phone numbers in E.164
Why to store phone numbers in E.164, how to convert what people type with the phone endpoint and a default country, and how to display numbers back to them.
People type phone numbers in dozens of ways: (415) 555-0123, 415.555.0123, +1 415 555 0123, 001 415 555 0123. Your database should hold one of them. This guide explains why that one should be E.164, and how to get there from whatever was typed.
What E.164 is
E.164 is the ITU-T recommendation that defines the international public telecommunication numbering plan. A number in E.164 format is a +, the country calling code, then the national number, with no spaces or punctuation, and at most 15 digits in total.
| Country | As typed at home | E.164 |
|---|---|---|
| United States | (415) 555-0123 |
+14155550123 |
| United Kingdom | 07400 123456 |
+447400123456 |
| France | 06 12 34 56 78 |
+33612345678 |
| Germany | 01512 3456789 |
+4915123456789 |
Notice that the leading 0 of the British, French and German numbers disappears: it is a trunk prefix used for calls inside the country, not part of the number.
Why store E.164
- One number, one string. Deduplication, lookups and unique constraints work. With raw input,
(415) 555-0123and+1 415 555 0123are two different customers. - No guessing the country.
06 12 34 56 78means nothing without knowing it was typed in France.+33612345678carries its country with it. - Messaging and voice APIs expect it. Many of them require numbers in E.164, so storing that form saves a conversion at every send.
- Formatting for display is easy from E.164. Going the other way, from a display format back to a number, is where the errors come from.
Store E.164 for machines and format for humans when you display.
Convert with the phone endpoint
Send what the person typed, plus a default country for numbers typed without a +:
curl https://api.qelka.com/v1/phone \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{"phone": "06 12 34 56 78", "country": "FR"}'
The result carries e164: "+33612345678", national: "06 12 34 56 78", valid: true and line_type: "mobile". The basic level costs 1 credit and is all you need for formatting.
| Input | country |
Result |
|---|---|---|
(415) 555-0123 |
US |
e164: "+14155550123" |
07400 123456 |
GB |
e164: "+447400123456" |
+49 1512 3456789 |
any, or none | e164: "+4915123456789": the + makes country unnecessary |
415 555 0123 |
none | status: "unknown", e164: null: without a + or a country, the number cannot be read |
Choose the default country well
The country field only matters for numbers typed without a +. In order of reliability:
- A country selector next to the phone field. The person tells you.
- A country you already know, such as the billing or shipping address.
- The country of the request, as your CDN or load balancer reports it, such as Cloudflare's
CF-IPCountryheader. Travelers and VPN users make this a guess, so let people change it.
Avoid a fixed default such as US for everyone. A French number typed as 06 12 34 56 78 read as a US number is invalid, and a UK number typed with its leading 0 does not fit the US plan either.
In Node.js
export async function toE164(phone, country) {
const response = await fetch('https://api.qelka.com/v1/phone', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(country ? { phone, country } : { phone }),
})
if (!response.ok) throw new Error(`Phone check failed with HTTP ${response.status}`)
const result = await response.json()
if (result.status === 'unknown') return { e164: null, problem: 'missing_country' }
if (!result.valid) return { e164: null, problem: 'invalid' }
return { e164: result.e164, problem: null }
}
In Python
import os
import requests
def to_e164(phone, country=None):
body = {"phone": phone, "country": country} if country else {"phone": phone}
response = requests.post(
"https://api.qelka.com/v1/phone",
headers={"Authorization": f"Bearer {os.environ['API_KEY']}"},
json=body,
timeout=10,
)
response.raise_for_status()
result = response.json()
if result["status"] == "unknown":
return None, "missing_country"
if not result["valid"]:
return None, "invalid"
return result["e164"], None
Store e164 only when valid is true. For anything else, ask the person to check the number rather than saving a value you cannot dial.
Convert numbers you already have
For a table of existing numbers, use batch calls of up to 100 numbers, each with its own country:
import { Client } from '@qelka/sdk'
const client = new Client({ apiKey: process.env.API_KEY })
export async function normalizeChunk(rows) {
const { data } = await client.batch(
rows.map((row) => (row.country ? { type: 'phone', phone: row.phone, country: row.country } : { type: 'phone', phone: row.phone })),
)
return rows.map((row, index) => {
const result = data[index]
const usable = result.object === 'phone' && result.valid
return { id: row.id, e164: usable ? result.e164 : null, status: result.status ?? result.error.type }
})
}
Call it with up to 100 rows at a time, each with an id, the stored phone and the best country you have for it, then write e164 back. Rows that come back with e164: null need a human look.
For a whole file, a bulk job with type=phone, column=phone and a default country does the same in the background; the result file adds the fields of each check next to your columns.
Display numbers back
- Inside the number's country, show
national, which the API returns ready to display:(415) 555-0123. - To an international audience, format the stored E.164 number with a library such as libphonenumber-js, which the API itself relies on:
parsePhoneNumber('+14155550123').formatInternational()gives+1 415 555 0123. - For links, use the
tel:URI scheme of RFC 3966 with the E.164 number:tel:+14155550123. Phones dial it correctly from anywhere.
Edge cases
- Extensions. E.164 has no room for an extension. Store it in a separate field.
- Short codes such as five-digit SMS numbers, and emergency numbers, are not E.164 subscriber numbers. Do not run them through this flow.
- Numbers change hands. A valid number is one that exists in the numbering plan, not one that still belongs to the person who gave it to you. The basic level cannot see ported or disconnected numbers. When the current carrier matters, use the live level at 3 credits; see Phone validation.
To try a few numbers by hand, the free phone number formatter shows the E.164, international, national and RFC 3966 forms of any number.