Cleaning the phone numbers in your customer database before sending transactional SMS

WhatsApp & APIs·· 64 views· 3 min read

The short answer: before you connect your system to SMS, tidy up the phone numbers in your customer database: one format, no spaces or symbols, no landlines, no duplicates, and one main number per customer. The API takes numbers with or without the country code, because the system can tell the country from the first digit: 841234567 goes to Mozambique and 923456789 to Angola. So +258 84 123 4567 and 258841234567 are the same number as 841234567. Even so, a clean database saves invalid_number errors, credits, and notices that end up with the wrong person.

What the API accepts

Number What happens
A Mozambican or Angolan mobile, with or without the country code, with or without spaces Recognised on its own and sent.
Any other number without a country code Treated as being in your account's country.
A number in a country the service does not send to The API answers 422 with country_not_available.
An incomplete number, or one with letters The API answers 422 with invalid_number.
A number on your account's do-not-send list The API answers 422 with number_opted_out.

Tidying the database, step by step

1 Strip out everything that is not a digit: spaces, dots, dashes, brackets and the + sign.
2 Pick one format and store every number that way, ideally with the country code. A number with its code leaves no doubt, wherever it came from.
3 Separate landlines from mobiles. A landline cannot receive SMS: keep it in another field, for calls.
4 Merge duplicates. The same customer with the number written three ways gets three texts.
5 Mark one main number per customer, so notices always go to the same one.

An example in Python

import re

def clean(number):
n = re.sub(r"\D", "", number or "")
if n.startswith("00"):
n = n[2:]
if len(n) == 9 and n[0] in "89":
return n
if len(n) == 12 and n[:3] in ("258", "244"):
return n
return None # for a person to review

The example accepts nine-digit mobiles, with or without the Mozambican or Angolan country code, and sets the rest aside for someone to review. Adapt it to your data: what matters is that no doubtful number reaches the API unseen.

Numbers change hands. A former customer may have given up the number, and the next notice reaches someone else. Ask customers to keep their phone number up to date and, in any notice with personal data, keep it to the minimum: what never to put in an SMS.
For SMS Center contacts, the CSV import takes lists from your database, and a number is never duplicated within the account. See contacts and groups.

Clean data, the right notices: see how Transactional SMS works.

See Transactional SMS

SEE ALSO

SMS contacts and groups: importing a CSV list and keeping it clean

SMS API errors and message statuses: what each one means and what to do

What never to put in a transactional SMS: passwords, full card numbers and private details

SMS statistics and reports through the API: totals by status, credits spent and days

SMS good practices and consent: who you may send to and how to let people opt out

Test cases for transactional SMS in the Sandbox: wrong numbers, long texts, no credits, repeats

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from KSh858.00/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.