Sending messages
There are two ways to send:
POST /v1/messages: one SMS to one number. Best for one-time codes, receipts and alerts.POST /v1/batches: up to 1,000 numbers in one request, with the same text or a personal text for each.
Both answer 202 Accepted straight away. The message is on its way; webhooks tell you when it's delivered.
Send one message
curl https://api.semalink.africa/v1/messages \
-H "Authorization: Bearer $SEMALINK_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{
"to": "+254712345678",
"from": "GREENHILL",
"text": "Hi Achieng, Term 1 fees are due on 9 January. Pay by M-Pesa to avoid a late fee. Greenhill Academy",
"reference": "invoice-1042"
}'2
3
4
5
6
7
8
9
10
const response = await fetch('https://api.semalink.africa/v1/messages', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SEMALINK_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': crypto.randomUUID(),
},
body: JSON.stringify({
to: '+254712345678',
from: 'GREENHILL',
text: 'Hi Achieng, Term 1 fees are due on 9 January. Pay by M-Pesa to avoid a late fee. Greenhill Academy',
reference: 'invoice-1042',
}),
});
const message = await response.json();
if (!response.ok) throw new Error(`${message.error.code}: ${message.error.message}`);
console.log(message.id, message.status);2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import os
import uuid
import requests
response = requests.post(
"https://api.semalink.africa/v1/messages",
headers={
"Authorization": f"Bearer {os.environ['SEMALINK_API_KEY']}",
"Idempotency-Key": str(uuid.uuid4()),
},
json={
"to": "+254712345678",
"from": "GREENHILL",
"text": "Hi Achieng, Term 1 fees are due on 9 January. Pay by M-Pesa to avoid a late fee. Greenhill Academy",
"reference": "invoice-1042",
},
timeout=30,
)
message = response.json()
if not response.ok:
raise RuntimeError(f"{message['error']['code']}: {message['error']['message']}")
print(message["id"], message["status"])2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
$ch = curl_init('https://api.semalink.africa/v1/messages');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('SEMALINK_API_KEY'),
'Content-Type: application/json',
'Idempotency-Key: ' . bin2hex(random_bytes(16)),
],
CURLOPT_POSTFIELDS => json_encode([
'to' => '+254712345678',
'from' => 'GREENHILL',
'text' => 'Hi Achieng, Term 1 fees are due on 9 January. Pay by M-Pesa to avoid a late fee. Greenhill Academy',
'reference' => 'invoice-1042',
]),
]);
$message = json_decode(curl_exec($ch), true);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status >= 400) {
throw new RuntimeException($message['error']['code'] . ': ' . $message['error']['message']);
}
echo $message['id'], ' ', $message['status'], PHP_EOL;2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"id": "msg_3b9f6c2a1d4e4f5a8b7c6d5e4f3a2b1c",
"object": "message",
"livemode": true,
"to": "+254712345678",
"from": "GREENHILL",
"text": "Hi Achieng, Term 1 fees are due on 9 January. Pay by M-Pesa to avoid a late fee. Greenhill Academy",
"type": "service",
"status": "queued",
"network": "safaricom",
"parts": 1,
"encoding": "gsm7",
"cost": { "amount": 80, "currency": "KES", "display": "KES 0.80" },
"reference": "invoice-1042",
"batch": null,
"send_at": null,
"created_at": "2026-12-02T07:15:22.000Z",
"sent_at": null,
"delivered_at": null,
"failure": null,
"refunded": false
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Store the id. You'll use it to look the message up, and it's in every webhook about the message.
from takes your sender ID's name (GREENHILL) or its ID (snd_..., from GET /v1/sender-ids).
What SemaLink checks first
Before it answers, SemaLink checks the request with the same rules the app uses for a campaign, in this order:
- The key: its scope, its allowed addresses and your organisation's standing.
- Your organisation can send: sending is on (
sending_disabled), the account is verified (account_not_active, live only) and not paused (sending_paused). - The sender ID in
fromis approved (sender_id_not_approved). In the sandbox any of yours works. - The type: promotions are available in the sender ID's country (
promotional_not_available). - The text: one SMS of GSM-7 characters, the opt-out line of a promotion included.
- The time:
send_atis 5 minutes to 90 days ahead, and inside the sending hours (outside_sending_hours). - The number: valid, in the sender ID's country, priced, and not opted out.
- Your limits: the daily limit (
daily_limit_reached) and the most people per send (recipient_limit_exceeded). - The content: a kind SemaLink doesn't carry is refused (
content_blocked); some wording is held for a short check. - Your balance: the cost is reserved now (
insufficient_balanceif it can't be) and charged when the network takes the message.
If anything fails, nothing is sent and nothing is charged, and the error says which check failed.
Some messages are held for a short check by our team, for example when wording looks promotional but was sent as service. A held message has status held and moves on to queued when it's released. Webhooks tell you both steps (message.held, message.released).
Service or promotional
Every message has a type:
service(the default): messages a person expects, such as codes, receipts, reminders and alerts.promotional: marketing and offers. In Kenya these can only go between 07:00 and 19:00 East Africa Time, and SemaLink adds the opt-out lineSTOP*456*9*5#on a new line at the end (unless your text already has it).
The opt-out line counts towards the 160 characters: with its line break it takes 14, so a promotion's own text can be up to 146. POST /v1/messages/estimate with "type": "promotional" shows the footer and the length with it.
Set "type": "promotional" for marketing. Outside the hours you get outside_sending_hours with next_window_at, the time you can send again (or pass as send_at). Marketing sent as service may be held for a check.
Send a batch with the same text
Give to (up to 1,000 numbers) and one text:
curl https://api.semalink.africa/v1/batches \
-H "Authorization: Bearer $SEMALINK_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{
"from": "GREENHILL",
"text": "Reminder: Sports Day is on Friday 6 December. Pupils come in PE kit. Greenhill Academy",
"to": ["+254712345678", "0733123456", "0722000111"],
"reference": "sports-day"
}'2
3
4
5
6
7
8
9
10
const response = await fetch('https://api.semalink.africa/v1/batches', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SEMALINK_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': crypto.randomUUID(),
},
body: JSON.stringify({
from: 'GREENHILL',
text: 'Reminder: Sports Day is on Friday 6 December. Pupils come in PE kit. Greenhill Academy',
to: ['+254712345678', '0733123456', '0722000111'],
reference: 'sports-day',
}),
});
const batch = await response.json();
const rejected = batch.recipients.filter((r) => r.status === 'rejected');
console.log(batch.id, `${batch.totals.accepted} accepted`, rejected);2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import os
import uuid
import requests
response = requests.post(
"https://api.semalink.africa/v1/batches",
headers={
"Authorization": f"Bearer {os.environ['SEMALINK_API_KEY']}",
"Idempotency-Key": str(uuid.uuid4()),
},
json={
"from": "GREENHILL",
"text": "Reminder: Sports Day is on Friday 6 December. Pupils come in PE kit. Greenhill Academy",
"to": ["+254712345678", "0733123456", "0722000111"],
"reference": "sports-day",
},
timeout=30,
)
batch = response.json()
rejected = [r for r in batch["recipients"] if r["status"] == "rejected"]
print(batch["id"], batch["totals"]["accepted"], "accepted", rejected)2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
$ch = curl_init('https://api.semalink.africa/v1/batches');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('SEMALINK_API_KEY'),
'Content-Type: application/json',
'Idempotency-Key: ' . bin2hex(random_bytes(16)),
],
CURLOPT_POSTFIELDS => json_encode([
'from' => 'GREENHILL',
'text' => 'Reminder: Sports Day is on Friday 6 December. Pupils come in PE kit. Greenhill Academy',
'to' => ['+254712345678', '0733123456', '0722000111'],
'reference' => 'sports-day',
]),
]);
$batch = json_decode(curl_exec($ch), true);
$rejected = array_filter($batch['recipients'], fn ($r) => $r['status'] === 'rejected');
echo $batch['id'], ' ', $batch['totals']['accepted'], ' accepted', PHP_EOL;
print_r($rejected);2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"id": "bat_7a1c2e3f4b5d4c6e8f9a0b1c2d3e4f5a",
"object": "batch",
"livemode": true,
"status": "sending",
"from": "GREENHILL",
"type": "service",
"text": "Reminder: Sports Day is on Friday 6 December. Pupils come in PE kit. Greenhill Academy",
"reference": "sports-day",
"send_at": null,
"created_at": "2026-12-02T07:20:00.000Z",
"completed_at": null,
"totals": {
"recipients": 3, "accepted": 2, "rejected": 1,
"scheduled": 0, "queued": 2, "held": 0, "sent": 0, "delivered": 0, "failed": 0, "expired": 0, "cancelled": 0
},
"cost": { "amount": 160, "currency": "KES", "display": "KES 1.60" },
"refunded": { "amount": 0, "currency": "KES", "display": "KES 0.00" },
"recipients": [
{ "index": 0, "input": "+254712345678", "to": "+254712345678", "status": "accepted", "message": "msg_3b9f6c2a1d4e4f5a8b7c6d5e4f3a2b1c", "rejection": null },
{ "index": 1, "input": "0733123456", "to": "+254733123456", "status": "accepted", "message": "msg_4c0a7d3b2e5f4a6b9c8d7e6f5a4b3c2d", "rejection": null },
{
"index": 2, "input": "0722000111", "to": "+254722000111", "status": "rejected", "message": null,
"rejection": { "code": "opted_out", "message": "This number opted out of your messages." }
}
]
}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
A batch doesn't fail because one number is bad. recipients has one entry per number, in the order you sent them: accepted (with its message ID) or rejected with a rejection code. A rejected number is never stored as a message and never charged:
| Code | Why |
|---|---|
invalid_number | Not a valid mobile number |
unsupported_country | A country we don't send to yet |
opted_out | The person opted out of your messages |
duplicate_in_request | The same number appears twice in this batch (the first one is sent) |
no_price | There's no price for this number yet |
Problems with the whole request (text too long, sender ID not approved, not enough balance for every accepted number) fail the whole batch with an error, and nothing is sent. If every number is rejected, the batch answers 422 no_valid_recipients.
Send personalised messages
Give messages, a list of up to 1,000 { to, text, reference } items, instead of to and text. Each text is checked on its own; a text that can't be sent fails the whole batch, with param naming it (such as messages[3].text).
curl https://api.semalink.africa/v1/batches \
-H "Authorization: Bearer $SEMALINK_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{
"from": "GREENHILL",
"reference": "term1-fees",
"messages": [
{ "to": "+254712345678", "text": "Hi Achieng, Term 1 fees of KES 12,500 are due on 9 January.", "reference": "GA-1042" },
{ "to": "+254733123456", "text": "Hi Brian, Term 1 fees of KES 9,800 are due on 9 January.", "reference": "GA-1043" }
]
}'2
3
4
5
6
7
8
9
10
11
12
const parents = [
{ phone: '+254712345678', name: 'Achieng', balance: '12,500', account: 'GA-1042' },
{ phone: '+254733123456', name: 'Brian', balance: '9,800', account: 'GA-1043' },
];
const response = await fetch('https://api.semalink.africa/v1/batches', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SEMALINK_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': crypto.randomUUID(),
},
body: JSON.stringify({
from: 'GREENHILL',
reference: 'term1-fees',
messages: parents.map((p) => ({
to: p.phone,
text: `Hi ${p.name}, Term 1 fees of KES ${p.balance} are due on 9 January.`,
reference: p.account,
})),
}),
});
const batch = await response.json();
console.log(batch.id, batch.totals);2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import os
import uuid
import requests
parents = [
{"phone": "+254712345678", "name": "Achieng", "balance": "12,500", "account": "GA-1042"},
{"phone": "+254733123456", "name": "Brian", "balance": "9,800", "account": "GA-1043"},
]
response = requests.post(
"https://api.semalink.africa/v1/batches",
headers={
"Authorization": f"Bearer {os.environ['SEMALINK_API_KEY']}",
"Idempotency-Key": str(uuid.uuid4()),
},
json={
"from": "GREENHILL",
"reference": "term1-fees",
"messages": [
{
"to": p["phone"],
"text": f"Hi {p['name']}, Term 1 fees of KES {p['balance']} are due on 9 January.",
"reference": p["account"],
}
for p in parents
],
},
timeout=30,
)
batch = response.json()
print(batch["id"], batch["totals"])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
<?php
$parents = [
['phone' => '+254712345678', 'name' => 'Achieng', 'balance' => '12,500', 'account' => 'GA-1042'],
['phone' => '+254733123456', 'name' => 'Brian', 'balance' => '9,800', 'account' => 'GA-1043'],
];
$messages = array_map(fn ($p) => [
'to' => $p['phone'],
'text' => "Hi {$p['name']}, Term 1 fees of KES {$p['balance']} are due on 9 January.",
'reference' => $p['account'],
], $parents);
$ch = curl_init('https://api.semalink.africa/v1/batches');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('SEMALINK_API_KEY'),
'Content-Type: application/json',
'Idempotency-Key: ' . bin2hex(random_bytes(16)),
],
CURLOPT_POSTFIELDS => json_encode([
'from' => 'GREENHILL',
'reference' => 'term1-fees',
'messages' => $messages,
]),
]);
$batch = json_decode(curl_exec($ch), true);
echo $batch['id'], PHP_EOL;
print_r($batch['totals']);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
The response has the same shape as a same-text batch, with "text": null: recipients lists each number, accepted or rejected.
Schedule for later
Add send_at, an ISO 8601 time from 5 minutes to 90 days ahead, to a message or a batch. Use UTC, or add an offset: 2026-12-05T09:00:00+03:00 is 09:00 in Nairobi. A time outside that range is 400 parameter_invalid.
{
"from": "GREENHILL",
"text": "Good morning! Sports Day starts at 10:00 today. Greenhill Academy",
"to": ["+254712345678", "+254733123456"],
"send_at": "2026-12-05T06:00:00Z"
}2
3
4
5
6
The batch comes back with "status": "scheduled" and its messages are scheduled until the time comes. The cost is reserved when you schedule, so the balance is there when it's time to send.
To call it off, cancel the batch while it is scheduled (or held for a check). Its messages become cancelled and what was set aside comes back to your balance at once. Once sending has started, a batch can't be cancelled (batch_not_cancellable).
Schedule as a batch if you might cancel
Cancelling works on batches. If a single scheduled message might need cancelling, send it as a batch of one.
Label messages with references
reference is your own label: an invoice number, an order ID, a user ID. It comes back on the message, in every webhook about it, and you can search by it:
curl "https://api.semalink.africa/v1/messages?reference=invoice-1042" \
-H "Authorization: Bearer $SEMALINK_API_KEY"2
A batch has its own reference too, and in a personalised batch each message can have one.
Retry safely with idempotency keys
Networks drop connections. If a request times out you can't tell whether SemaLink got it. Send an Idempotency-Key header with every POST and retry with the same key: you get the first answer back instead of a second message. Keys last 24 hours. Read Idempotency and retries.
Find out what happened
- Webhooks (best): SemaLink posts
message.sent,message.deliveredandmessage.failedto your server. See Webhooks. - Fetch a message:
GET /v1/messages/{id}returns its status andstatus_history. - List messages:
GET /v1/messageswith filters such asstatus=failedorbatch=bat_.... It lists all your organisation's messages, the ones sent from the app included. - Fetch a batch:
GET /v1/batches/{id}returns itstotalsby status, itscostand what wasrefunded.