Phone numbers
Send numbers in international form (E.164) or in any common Kenyan form. SemaLink reads them, works out the network, and always answers with the E.164 form.
Formats that work
| You send | SemaLink reads it as |
|---|---|
+254712345678 | +254712345678 (E.164, the best choice) |
254712345678 | +254712345678 |
0712345678 | +254712345678 |
712345678 | +254712345678 |
0110345678 | +254110345678 |
Numbers starting 01 work the same way as 07. The to field in every response, list and webhook is the E.164 form, so match on that in your own database.
Store numbers in E.164
Keep +254... in your database. It is one form for every number, it sorts and compares cleanly, and it is what SemaLink returns.
Networks
SemaLink works out each number's network from its prefix and returns it in network, such as safaricom or airtel_ke, when it knows it. The price is the same on Safaricom, Airtel and Telkom (KES 0.80 per SMS in Kenya), so you don't need to know the network to send.
Countries
| Country | Status |
|---|---|
| Kenya (+254) | Sending now on Safaricom, Airtel and Telkom |
| Tanzania (+255) | Coming |
| Uganda (+256) | Coming |
A message goes to numbers in its sender ID's country. A number in another country, or a country SemaLink doesn't send to, gets unsupported_country. GET /v1/pricing lists where your organisation can send.
When a number is refused
| Code | What it means |
|---|---|
invalid_number | Not a mobile number: too short, too long, a landline or special number |
unsupported_country | Another country than your sender ID's, or one SemaLink doesn't send to |
opted_out | The person opted out of your organisation's messages |
duplicate_in_request | The number is already in this batch (compared after normalising, so 0712345678 and +254712345678 are the same) |
no_price | There is no price for this number yet |
For a single message these come back as a 422 error. In a batch, the number is listed in recipients with its rejection, and the rest are sent. A refused number is never stored as a message and never charged.
Check numbers before you send
POST /v1/messages/estimate sends nothing. It returns each number in E.164 with its country and network, or why it would be refused (invalid_number, unsupported_country, duplicate_in_request or no_price), and the cost. Opt-outs are checked when you send, not here.
curl https://api.semalink.africa/v1/messages/estimate \
-H "Authorization: Bearer $SEMALINK_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": ["0712345678", "733123456", "0712 34"],
"text": "Your order GH-1042 is ready for collection."
}'2
3
4
5
6
7
const response = await fetch('https://api.semalink.africa/v1/messages/estimate', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SEMALINK_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
to: ['0712345678', '733123456', '0712 34'],
text: 'Your order GH-1042 is ready for collection.',
}),
});
const estimate = await response.json();
for (const r of estimate.recipients) {
console.log(r.input, r.valid ? r.to : r.rejection.code);
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import os
import requests
response = requests.post(
"https://api.semalink.africa/v1/messages/estimate",
headers={"Authorization": f"Bearer {os.environ['SEMALINK_API_KEY']}"},
json={
"to": ["0712345678", "733123456", "0712 34"],
"text": "Your order GH-1042 is ready for collection.",
},
timeout=30,
)
estimate = response.json()
for r in estimate["recipients"]:
print(r["input"], r["to"] if r["valid"] else r["rejection"]["code"])2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$ch = curl_init('https://api.semalink.africa/v1/messages/estimate');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('SEMALINK_API_KEY'),
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'to' => ['0712345678', '733123456', '0712 34'],
'text' => 'Your order GH-1042 is ready for collection.',
]),
]);
$estimate = json_decode(curl_exec($ch), true);
foreach ($estimate['recipients'] as $r) {
echo $r['input'], ' ', $r['valid'] ? $r['to'] : $r['rejection']['code'], PHP_EOL;
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"object": "estimate",
"livemode": true,
"type": "service",
"country": "KE",
"message": {
"encoding": "gsm7",
"characters": 43,
"characters_left": 117,
"parts": 1,
"max_parts": 1,
"fits": true,
"problems": [],
"unsupported_characters": [],
"footer": null
},
"cost_per_message": { "amount": 80, "currency": "KES", "display": "KES 0.80" },
"recipients": [
{
"input": "0712345678", "to": "+254712345678", "valid": true, "country": "KE", "network": "safaricom",
"cost": { "amount": 80, "currency": "KES", "display": "KES 0.80" }, "rejection": null
},
{
"input": "733123456", "to": "+254733123456", "valid": true, "country": "KE", "network": "airtel_ke",
"cost": { "amount": 80, "currency": "KES", "display": "KES 0.80" }, "rejection": null
},
{
"input": "0712 34", "to": null, "valid": false, "country": null, "network": null, "cost": null,
"rejection": { "code": "invalid_number", "message": "This number is too short." }
}
],
"totals": {
"recipients": 3,
"valid": 2,
"rejected": 1,
"sms": 2,
"cost": { "amount": 160, "currency": "KES", "display": "KES 1.60" }
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Clean numbers in your own code
If you collect numbers in a form, normalise them when you save them. This turns the Kenyan forms above into E.164 and returns nothing for anything else:
# Normalising happens on SemaLink's side: send any accepted form and read
# the E.164 number back from "to" in the response.2
export function toE164(input) {
const s = String(input).replace(/[\s()-]/g, '');
const kenyan = /^(?:\+?254|0)?([17]\d{8})$/.exec(s);
return kenyan ? `+254${kenyan[1]}` : null;
}
toE164('0712 345 678'); // '+254712345678'
toE164('254110345678'); // '+254110345678'
toE164('12345'); // null2
3
4
5
6
7
8
9
import re
KENYAN = re.compile(r"^(?:\+?254|0)?([17]\d{8})$")
def to_e164(value):
s = re.sub(r"[\s()-]", "", str(value))
match = KENYAN.match(s)
return f"+254{match.group(1)}" if match else None
to_e164("0712 345 678") # '+254712345678'
to_e164("254110345678") # '+254110345678'
to_e164("12345") # None2
3
4
5
6
7
8
9
10
11
12
<?php
function toE164(string $value): ?string
{
$s = preg_replace('/[\s()-]/', '', $value);
return preg_match('/^(?:\+?254|0)?([17]\d{8})$/', $s, $m) ? '+254' . $m[1] : null;
}
toE164('0712 345 678'); // '+254712345678'
toE164('254110345678'); // '+254110345678'
toE164('12345'); // null2
3
4
5
6
7
8
9
10
SemaLink still checks every number itself, so this is a convenience for your database, not a requirement.