Errors
When a request fails, SemaLink answers with an HTTP status of 400 or more and one JSON shape. Every error has a stable code you can branch on, and a link back to its entry on this page.
{
"error": {
"type": "invalid_request_error",
"code": "message_too_long",
"message": "The text is 172 characters: one SMS holds 160. Shorten it by 12.",
"param": "text",
"characters": 172,
"max_characters": 160,
"doc_url": "https://docs.semalink.africa/errors#message_too_long",
"request_id": "req_5f1d0c2e8a9b4c3d9e7f6a5b4c3d2e1f"
}
}2
3
4
5
6
7
8
9
10
11
12
| Field | What it is |
|---|---|
type | The kind of error (below). |
code | What went wrong, such as message_too_long. Codes don't change; branch on this. |
message | What went wrong and how to fix it, in a sentence. It may be reworded over time, so don't match on it. |
param | The request field the error is about (text, to[3]), when there is one. Left out otherwise. |
doc_url | This page, at the code's entry. |
request_id | The request's ID, also in the Request-Id header. Quote it when you contact support. |
Some codes add a field:
| Code | Adds |
|---|---|
insufficient_balance | shortfall: what a top-up must cover, as Money |
outside_sending_hours | next_window_at: when sending hours next open (ISO 8601) |
message_too_long | characters and max_characters |
unsupported_characters | unsupported_characters: each character, code_point, name and replacement |
content_blocked | category: the kind of content SemaLink doesn't carry |
Types
type | HTTP status | Meaning |
|---|---|---|
authentication_error | 401 | The key is missing, unknown, expired or revoked. |
permission_error | 403 | The key can't do this, or the account can't send right now. |
invalid_request_error | 400, 404, 409, 413, 415, 422 | Something in the request needs changing. |
insufficient_balance | 402 | Not enough balance for the whole request. |
rate_limit_error | 429 | Too many requests. Slow down. |
api_error | 500, 503 | Something went wrong at SemaLink, or the request took too long. |
Handle errors in your code
Check the status, then branch on error.code. Log request_id with every failure.
# -w prints the HTTP status after the body.
curl -s -w "\nHTTP %{http_code}\n" https://api.semalink.africa/v1/messages \
-H "Authorization: Bearer $SEMALINK_API_KEY" \
-H "Content-Type: application/json" \
-d '{"to": "+254712345678", "from": "GREENHILL", "text": "Hi"}'2
3
4
5
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' }),
});
if (!response.ok) {
const { error } = await response.json();
switch (error.code) {
case 'insufficient_balance':
console.warn(`Top up ${error.shortfall.display} and retry`);
break;
case 'opted_out':
console.info('Skipping: the person opted out');
break;
default:
console.error(`${error.code}: ${error.message} (${error.request_id})`);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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"},
timeout=30,
)
if not response.ok:
error = response.json()["error"]
if error["code"] == "insufficient_balance":
print(f"Top up {error['shortfall']['display']} and retry")
elif error["code"] == "opted_out":
print("Skipping: the person opted out")
else:
print(f"{error['code']}: {error['message']} ({error['request_id']})")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/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']),
]);
$body = json_decode(curl_exec($ch), true);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400) {
$error = $body['error'];
switch ($error['code']) {
case 'insufficient_balance':
error_log("Top up {$error['shortfall']['display']} and retry");
break;
case 'opted_out':
error_log('Skipping: the person opted out');
break;
default:
error_log("{$error['code']}: {$error['message']} ({$error['request_id']})");
}
}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
Which errors to retry
| Retry? | Errors |
|---|---|
| Yes, after waiting | 429 rate_limited (wait for Retry-After), 500 internal_error, 503 request_timeout, 409 idempotency_key_in_use, and dropped connections |
| Yes, after you fix something | 402 insufficient_balance after a top-up, 422 outside_sending_hours after next_window_at, 422 daily_limit_reached the next day |
| No | Other 4xx: the same request will fail the same way |
Always retry a POST with the same Idempotency-Key, so a retry can never send twice. See Idempotency and retries.
All codes
| Code | Status | Type |
|---|---|---|
invalid_api_key | 401 | authentication_error |
key_revoked | 401 | authentication_error |
key_expired | 401 | authentication_error |
scope_missing | 403 | permission_error |
ip_not_allowed | 403 | permission_error |
browser_requests_not_allowed | 403 | permission_error |
api_blocked | 403 | permission_error |
account_suspended | 403 | permission_error |
account_not_active | 403 | permission_error |
sending_paused | 403 | permission_error |
parameter_missing | 400 | invalid_request_error |
parameter_invalid | 400 | invalid_request_error |
parameter_unknown | 400 | invalid_request_error |
invalid_json | 400 | invalid_request_error |
invalid_body | 400 | invalid_request_error |
invalid_request | 400 | invalid_request_error |
idempotency_key_invalid | 400 | invalid_request_error |
resource_not_found | 404 | invalid_request_error |
route_not_found | 404 | invalid_request_error |
request_too_large | 413 | invalid_request_error |
unsupported_media_type | 415 | invalid_request_error |
sending_disabled | 409 | invalid_request_error |
idempotency_key_reused | 409 | invalid_request_error |
idempotency_key_in_use | 409 | invalid_request_error |
idempotency_answer_unavailable | 409 | invalid_request_error |
batch_not_cancellable | 409 | invalid_request_error |
message_too_long | 422 | invalid_request_error |
unsupported_characters | 422 | invalid_request_error |
sender_id_not_approved | 422 | invalid_request_error |
outside_sending_hours | 422 | invalid_request_error |
promotional_not_available | 422 | invalid_request_error |
business_details_missing | 422 | invalid_request_error |
daily_limit_reached | 422 | invalid_request_error |
recipient_limit_exceeded | 422 | invalid_request_error |
content_blocked | 422 | invalid_request_error |
no_valid_recipients | 422 | invalid_request_error |
invalid_number | 422 | invalid_request_error |
unsupported_country | 422 | invalid_request_error |
opted_out | 422 | invalid_request_error |
duplicate_in_request | 422 | invalid_request_error |
no_price | 422 | invalid_request_error |
url_not_https | 422 | invalid_request_error |
url_private_address | 422 | invalid_request_error |
insufficient_balance | 402 | insufficient_balance |
rate_limited | 429 | rate_limit_error |
internal_error | 500 | api_error |
request_timeout | 503 | api_error |
Authentication
The key couldn't be used. Nothing was done.
invalid_api_key
401authentication_error
What happened. The Authorization header is missing, isn't Bearer followed by a key, or the key doesn't exist.
How to fix it. Send Authorization: Bearer sk_test_... or sk_live_.... Check the key was copied whole, with no space or line break. Test and live keys are different keys.
key_revoked
401authentication_error
What happened. Someone in your organisation revoked the key, or SemaLink did (for example after it was posted publicly).
How to fix it. Create a new key in Developer, API keys. If you didn't revoke it, the dashboard says who did.
key_expired
401authentication_error
What happened. The key had an expiry date and it has passed.
How to fix it. Create a new key. Give long-lived servers a key without an expiry, protected by an IP allowlist.
Permission
The key works, but this request isn't allowed. Nothing was done.
scope_missing
403permission_error
What happened. The key doesn't have the scope this endpoint needs. The message names it, such as messages:send.
How to fix it. Use a key with that scope, or create one. See Scopes.
ip_not_allowed
403permission_error
What happened. The key has an IP allowlist and the request came from an address that isn't on it.
How to fix it. If the address is yours (a new server, a changed outbound address), add it to the key's allowlist. If it isn't, someone else has your key: revoke it now.
browser_requests_not_allowed
403permission_error
What happened. The request had an Origin header, so it came from a web browser.
How to fix it. Call SemaLink from your server, never from a web page or mobile app. See Keys belong on your server. Only GET /v1/openapi.json answers browsers.
api_blocked
403permission_error
What happened. API access is blocked for your organisation.
How to fix it. Email developers@semalink.africa with the request_id.
account_suspended
403permission_error
What happened. Your organisation is suspended.
How to fix it. Email developers@semalink.africa.
account_not_active
403permission_error
What happened. Live sending needs a verified, active organisation, and yours isn't yet.
How to fix it. Finish verifying your business in the dashboard. Test keys work meanwhile.
sending_paused
403permission_error
What happened. SemaLink has paused sending for your organisation.
How to fix it. Reply to our email about it, or contact support. Messages can be sent again once the pause is lifted.
Request
Something about the request itself. Fix it and send again.
parameter_missing
400invalid_request_error
What happened. A required field is missing. param names it.
How to fix it. Add the field. The API reference marks every required field.
parameter_invalid
400invalid_request_error
What happened. A field has the wrong type or value, such as limit=500, a batch of more than 1,000 numbers, or a send_at less than 5 minutes or more than 90 days away. param names it (to[3] for the fourth number).
How to fix it. Correct the value. The message says what's allowed.
parameter_unknown
400invalid_request_error
What happened. The body has a field the endpoint doesn't take, such as sender instead of from. param names it.
How to fix it. Remove or rename the field. Request bodies are strict, so a typo never goes unnoticed.
invalid_json
400invalid_request_error
What happened. The body isn't valid JSON, or it's empty while Content-Type says JSON.
How to fix it. Send a JSON object, or no Content-Type header when there's no body.
invalid_body
400invalid_request_error
What happened. The body is valid JSON but not an object (an array or a string, say).
How to fix it. Send a JSON object.
invalid_request
400invalid_request_error
What happened. The request couldn't be read at all.
How to fix it. Check the method, headers and body. If it keeps happening, contact support with the request_id.
idempotency_key_invalid
400invalid_request_error
What happened. The Idempotency-Key header is empty or longer than 255 characters.
How to fix it. Use 1 to 255 characters. A UUID is ideal.
resource_not_found
404invalid_request_error
What happened. Nothing with that ID exists for this key. Test keys only see the sandbox's objects and live keys only live ones.
How to fix it. Check the ID, and that the key is the same mode as the one that made the object.
route_not_found
404invalid_request_error
What happened. There is no endpoint at that method and path.
How to fix it. Check the path against the API reference, including the /v1 prefix.
request_too_large
413invalid_request_error
What happened. The body is larger than 1 MB.
How to fix it. Send fewer numbers per request. A batch of 1,000 fits comfortably.
unsupported_media_type
415invalid_request_error
What happened. The body isn't sent as JSON.
How to fix it. Send Content-Type: application/json.
Conflicts
The request clashes with the state of something: try again later, or differently.
sending_disabled
409invalid_request_error
What happened. Sending is switched off at SemaLink for now. Nothing was sent.
How to fix it. Try again later. The dashboard shows a notice while sending is off.
idempotency_key_reused
409invalid_request_error
What happened. This Idempotency-Key was used in the last 24 hours with a different request (another endpoint or body).
How to fix it. Use a new key for a new request. Reuse a key only to retry exactly the same request. See Idempotency.
idempotency_key_in_use
409invalid_request_error
What happened. A request with this Idempotency-Key is still running.
How to fix it. Wait a moment and retry with the same key: you'll get its answer once it has finished.
idempotency_answer_unavailable
409invalid_request_error
What happened. The request with this key already ran, but its answer was larger than 512 KB, so it wasn't kept for replays. The message says which status it answered.
How to fix it. Don't run it again: look up what it made instead, with GET /v1/messages or the batch.
batch_not_cancellable
409invalid_request_error
What happened. The batch has started sending, finished or failed, so it can't be cancelled.
How to fix it. Only a batch that is scheduled, or held for a check, can be cancelled.
Sending
The request was understood but the message can't go as asked. Nothing was sent or charged.
message_too_long
422invalid_request_error
What happened. The text is longer than one SMS: 160 GSM-7 characters, where the nine characters ^ { } \ [ ~ ] | € count as 2, and a promotion's opt-out line counts too. The error adds characters (the length as SMS count it) and max_characters.
How to fix it. Shorten it by characters minus max_characters. Check first with the counter or POST /v1/messages/estimate.
unsupported_characters
422invalid_request_error
What happened. The text has characters outside GSM-7, such as emoji, most accented letters or other alphabets. (Curly quotes, long dashes and the like are swapped for you, so they never cause this.) The error adds unsupported_characters: each character, its code_point, its name and a replacement ("" removes it, null when there's no safe one).
How to fix it. Use the replacements, or reword. See Message length and characters.
sender_id_not_approved
422invalid_request_error
What happened. The from sender ID isn't approved to send now, or isn't your organisation's.
How to fix it. Use an approved sender ID by name or snd_ ID; GET /v1/sender-ids lists yours. New ones usually take 4 to 5 days. In the sandbox any of yours works, or SANDBOX.
outside_sending_hours
422invalid_request_error
What happened. The message would go outside the sending hours for its type and country. In Kenya, promotions go between 07:00 and 19:00 East Africa Time. The error adds next_window_at, when the hours next open.
How to fix it. Send after next_window_at, or pass it as send_at. If the message isn't marketing, send it as service.
promotional_not_available
422invalid_request_error
What happened. Promotional messages aren't available yet to the country of your sender ID.
How to fix it. Send it as service if it is one; otherwise it can't go yet.
business_details_missing
422invalid_request_error
What happened. Messages to Uganda must name your business and give a way to reach it (a phone number, email or website).
How to fix it. Add your business name and a contact to the text.
daily_limit_reached
422invalid_request_error
What happened. The send would go over your organisation's daily limit. In the sandbox the limit is 500 messages a day. The message says how many are left today.
How to fix it. Send the rest tomorrow, or to fewer people. Contact us about a higher limit.
recipient_limit_exceeded
422invalid_request_error
What happened. The send has more recipients than your organisation may reach in one send.
How to fix it. Split it into smaller sends, or contact us about the limit.
content_blocked
422invalid_request_error
What happened. The text is of a kind SemaLink doesn't carry. The error adds category.
How to fix it. Change the message. If you think this is wrong, contact support with the request_id.
no_valid_recipients
422invalid_request_error
What happened. Batches only: every number was rejected, so there was nothing to send.
How to fix it. Check the numbers with POST /v1/messages/estimate, which says why each one is refused.
Recipients
About one phone number. A single message answers these as a 422 error. A batch lists them per number in recipients[].rejection and sends the rest. A rejected number is never stored as a message and never charged.
invalid_number
422invalid_request_error
What happened. Not a mobile number: too short, too long, a landline or special number, or not a number at all.
How to fix it. Check the number. See Phone numbers.
unsupported_country
422invalid_request_error
What happened. The number is in a country SemaLink doesn't send to, or a different country from your sender ID's.
How to fix it. Send to numbers in your sender ID's country. GET /v1/pricing lists where you can send.
opted_out
422invalid_request_error
What happened. The person opted out of your organisation's messages.
How to fix it. Don't send to them. Flag the number in your own system.
duplicate_in_request
422invalid_request_error
What happened. Batches only: the number is already in this batch, compared after normalising. It gets one message.
How to fix it. Nothing to do, unless the duplicate is a mistake in your list.
no_price
422invalid_request_error
What happened. There is no price for this number yet, so it can't be sent.
How to fix it. Email developers@semalink.africa with the number's country and network.
Webhook addresses
url_not_https
422invalid_request_error
What happened. A webhook endpoint's url must start with https://.
How to fix it. Serve your endpoint over HTTPS.
url_private_address
422invalid_request_error
What happened. The url points at a private or local address (such as localhost or 10.0.0.5) that SemaLink won't post to.
How to fix it. Use a public address. To test on your machine, put a tunnel in front of it.
Balance
insufficient_balance
402insufficient_balance
What happened. Your balance can't cover the whole send. The error adds shortfall, the amount missing, as Money. Nothing was sent.
How to fix it. Top up with M-Pesa in the dashboard, then send again with the same Idempotency-Key. See Pricing and billing.
Rate limits
rate_limited
429rate_limit_error
What happened. Too many requests: for the key (live 20 a second, bursts to 40; test 5), for your organisation across its keys (live 40, bursts to 80; test 10), or 20 webhook changes and tests a minute.
How to fix it. Wait for the seconds in the Retry-After header, then retry. Send many numbers in one batch instead of many requests. See Rate limits.
Server
internal_error
500api_error
What happened. Something went wrong at SemaLink. We're alerted to these.
How to fix it. Retry with the same Idempotency-Key, waiting longer each time. If it keeps happening, email developers@semalink.africa with the request_id.
request_timeout
503api_error
What happened. The request took more than 30 seconds and was stopped. Its work may still finish.
How to fix it. Retry with the same Idempotency-Key: you'll get idempotency_key_in_use while the first one finishes, then its answer, never a second send.