Learning path: Your first WhatsApp Cloud API integrationStep 5 of 7
WhatsApp phone number format: normalizing to E.164
Store every phone number in E.164: a plus sign, the country code and the national number, with no spaces or national prefixes, at most 15 digits. Keep WhatsApp’s own identifier for the user in a separate column, because Meta says the two do not always match. Normalize once, on the way in, with libphonenumber. Then match contacts on the stored values, never on whatever string a form or a spreadsheet handed you.
What E.164 fixes, and what it does not
E.164 is the ITU’s international numbering plan. Its Annex A gives international numbers for geographic areas “a maximum length of 15 digits”. The national significant number gets 15 digits minus the length of the country code (ITU-T E.164 (02/2026), Annex A.3). The plus sign stands in for whatever international prefix the caller would dial. A number written as +5215512345678, 0052 55 1234 5678 and (55) 1234-5678 can be the same line, and only one canonical string should reach your database.
E.164 settles the shape. It does not tell you which of two valid shapes a country now uses, and that is where the three countries below cause trouble.
What WhatsApp gives you back
The Cloud API accepts loose input. “Plus signs (+), hyphens (-), parenthesis ((,)), and spaces are supported in send message requests” (Meta phone numbers guide). The same page warns about the plus sign. Without it, “your business phone number’s country calling code is prepended to the customer’s phone number”. Meta’s example is an Indian business number sending to (631) 555-1234, which goes to +916315551234. Always send the plus sign.
Every send response returns a contacts array with two values. input echoes what you sent, and wa_id is the user’s WhatsApp ID (image messages). In Meta’s examples, input is +16505551234 and wa_id is 16505551234, digits only. Inbound webhooks carry the same digits-only wa_id in contacts and in the message’s from field. Meta’s reference adds one sentence that should shape your schema: “Note that a WhatsApp user’s ID and phone number may not always match” (image messages webhook).
Country rules that break matching
Mexico dropped the 1 for mobiles
Since 3 August 2019, a call from abroad to Mexico dials 52 plus ten digits, for landlines and mobiles alike. The regulator’s wording is “52 (código de México) + número a 10 dígitos (no importando si es fijo o móvil)” (IFT, Marcación a 10 dígitos). The old +52 1 prefix for mobiles is gone. Google’s libphonenumber metadata records the change: “Dialling tokens 01, 02, 044, 045 and 1 are removed as they are no longer valid since August 2019” (PhoneNumberMetadata.xml).
Old CRMs still hold +521... numbers, 13 digits long. The Python port of libphonenumber (phonenumbers 9.0.42) rejects them as invalid instead of repairing them. Strip the 1 before parsing.
Brazil added a ninth digit to mobiles
Brazil prefixed every mobile number with a 9. Anatel’s page says the migration was completed on 14 February 2017, and mobiles now take the form 9XXXX-XXXX after the two-digit area code (Anatel, Nono dígito). In libphonenumber’s metadata, Brazilian landlines start with 2 to 5 after the area code. Mobile radio numbers starting with 7 “were not migrated and remain 10 digits long” (same metadata file). A 10-digit national number whose first local digit is 6, 8 or 9 is a pre-2017 mobile missing its 9.
Meta also has a note that covers both countries. “For Brazil and Mexico, the extra added prefix of the phone number may be modified by the Cloud API” (Meta phone numbers guide). Meta does not say which prefix or when. So the wa_id that comes back for a Brazilian or Mexican number can differ from the E.164 you sent. Store it from the first send response or inbound webhook, and match later webhooks on it.
Argentina puts a 9 in front of mobiles
Inside Argentina, a mobile is dialed with 15 before the local number. From abroad, the 15 disappears and a 9 goes after the country code. Argentina’s foreign ministry writes Buenos Aires landlines as +54 11. Mobiles take +54 9 11 “si es un teléfono celular o móvil” (Cancillería, telecomunicaciones). libphonenumber converts a national 15 into that 9 when you parse with region AR.
The trap is the number stored without the 9. +54 11 2345 6789 is a valid landline in libphonenumber, so validation passes and the contact never matches the mobile wa_id that starts 54911. Check the number type, and treat an Argentine FIXED_LINE result on a WhatsApp contact as suspect.
Normalize with libphonenumber
The snippet below runs on Python 3.9 and needs one package: pip install phonenumbers (the Python port of Google’s libphonenumber). It repairs the two legacy shapes above, parses, validates, and returns the E.164 string, the digits-only form and the number type.
import re
import phonenumbers
from phonenumbers import NumberParseException, PhoneNumberFormat, PhoneNumberType
def legacy_fix(digits):
"""Rewrite two pre-reform shapes still found in old contact lists."""
# Mexico: 52 1 + 10 digits (13 digits) -> 52 + 10 digits
if digits.startswith("521") and len(digits) == 13:
return "52" + digits[3:]
# Brazil: 55 + area code + 8 digits starting 6, 8 or 9 -> add the ninth digit
if digits.startswith("55") and len(digits) == 12 and digits[4] in "689":
return digits[:4] + "9" + digits[4:]
return digits
def normalize(raw, default_region=None):
"""Return (e164, wa_digits, number_type), or None when the input is not valid."""
raw = raw.strip()
if raw.startswith("+"):
raw = "+" + legacy_fix(re.sub(r"\D", "", raw))
try:
num = phonenumbers.parse(raw, default_region)
except NumberParseException:
return None
if not phonenumbers.is_valid_number(num):
return None
e164 = phonenumbers.format_number(num, PhoneNumberFormat.E164)
return e164, e164[1:], phonenumbers.number_type(num)
for raw, region in [
("+52 1 55 1234 5678", None),
("+55 11 6123-4567", None),
("011 15 2345-6789", "AR"),
("+54 11 2345 6789", None),
]:
result = normalize(raw, region)
if result and result[2] == PhoneNumberType.FIXED_LINE:
print(raw, "->", result[0], "(fixed line: check before sending)")
else:
print(raw, "->", result)
Pass default_region only when you know where a number without a plus sign came from, such as a form with a country selector. Without it, a national number fails to parse, which is the outcome you want.
Store three identifiers per contact
Keep the E.164 you normalized, the wa_id Meta returned, and a third column for users who hide their number. Meta began sending a business-scoped user ID, user_id, in webhooks in April 2026 to support WhatsApp usernames. When a user adopts a username, “their phone number will not be included in webhooks” by default. The exceptions are a message or call with that number in the last 30 days, or the user being in your contact book. In those payloads wa_id is missing (Business-scoped user IDs).
CREATE TABLE whatsapp_contacts (
id bigserial PRIMARY KEY,
e164 text UNIQUE, -- '+5511961234567', from normalize()
wa_id text UNIQUE, -- digits only, as Meta returns it
bsuid text UNIQUE, -- webhook user_id, e.g. 'US.13491208655302741918'
updated_at timestamptz NOT NULL DEFAULT now()
);
Look a webhook up by wa_id first and by bsuid when wa_id is absent. Write any new identifier back to the same row. On a QR connection the identifiers are JIDs and LIDs instead, covered in WhatsApp LID vs JID. The normalization step is the same.
Z-API Omni team