Rate limits
Rate limits keep the API fast for everyone. They count requests, not messages: one batch request can carry 1,000 numbers.
The limits
Each limit is a bucket that refills steadily. A request takes from every bucket it belongs to at once, and goes ahead only if all of them have room.
| Bucket | Live keys | Test keys |
|---|---|---|
| Per key | 20 a second, bursts up to 40 | 5 a second |
| Per organisation, across all its keys of that mode | 40 a second, bursts up to 80 | 10 a second |
| Creating, changing and testing webhook endpoints | 20 a minute per organisation | 20 a minute per organisation |
Requests without a usable key (a mistyped key, say) share a bucket per IP address: 20 a second, bursts up to 40.
Big requests cost more. A batch or an estimate costs 1, plus 1 for every 100 numbers: a batch of 1,000 numbers costs 11. A single message costs 1.
Over a limit you get 429 with rate_limited. Separately, your messages count against your daily sending limit (daily_limit_reached); in the sandbox that's 500 messages a day per organisation.
The headers
Every response tells you where you stand:
| Header | Meaning |
|---|---|
RateLimit-Limit | The size of the tightest bucket (the one with least room left): the most requests at once |
RateLimit-Remaining | Requests left in that bucket now |
RateLimit-Reset | Seconds until that bucket is full again |
Retry-After | On a 429 only: seconds to wait before trying again |
Request-Id | This request's ID (also in error bodies as request_id) |
HTTP/1.1 429 Too Many Requests
Content-Type: application/json
Retry-After: 1
RateLimit-Limit: 40
RateLimit-Remaining: 0
RateLimit-Reset: 2
Request-Id: req_5f1d0c2e8a9b4c3d9e7f6a5b4c3d2e1f2
3
4
5
6
7
{
"error": {
"type": "rate_limit_error",
"code": "rate_limited",
"message": "Too many requests with this key. Retry after 1 second.",
"doc_url": "https://docs.semalink.africa/errors#rate_limited",
"request_id": "req_5f1d0c2e8a9b4c3d9e7f6a5b4c3d2e1f"
}
}2
3
4
5
6
7
8
9
Back off and retry
When you get a 429, wait for Retry-After and send the same request again, with the same Idempotency-Key:
# curl can retry for you: --retry honours Retry-After on 429 responses.
curl --retry 5 --retry-max-time 60 https://api.semalink.africa/v1/messages \
-H "Authorization: Bearer $SEMALINK_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order-1042-ready" \
-d '{"to": "+254712345678", "from": "GREENHILL", "text": "Your order GH-1042 is ready."}'2
3
4
5
6
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export async function semalink(path, { method = 'GET', body, idempotencyKey } = {}) {
for (let attempt = 1; ; attempt += 1) {
const response = await fetch(`https://api.semalink.africa/v1${path}`, {
method,
headers: {
Authorization: `Bearer ${process.env.SEMALINK_API_KEY}`,
...(body ? { 'Content-Type': 'application/json' } : {}),
...(idempotencyKey ? { 'Idempotency-Key': idempotencyKey } : {}),
},
body: body ? JSON.stringify(body) : undefined,
});
if (response.status !== 429 || attempt === 5) return response;
const wait = Number(response.headers.get('Retry-After') ?? 1);
await sleep(wait * 1000);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import os
import time
import requests
def semalink(method, path, body=None, idempotency_key=None):
headers = {"Authorization": f"Bearer {os.environ['SEMALINK_API_KEY']}"}
if idempotency_key:
headers["Idempotency-Key"] = idempotency_key
for attempt in range(1, 6):
response = requests.request(
method, f"https://api.semalink.africa/v1{path}", headers=headers, json=body, timeout=30
)
if response.status_code != 429 or attempt == 5:
return response
time.sleep(int(response.headers.get("Retry-After", "1")))2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
function semalink(string $method, string $path, ?array $body = null, ?string $idempotencyKey = null): array
{
$headers = ['Authorization: Bearer ' . getenv('SEMALINK_API_KEY')];
if ($body !== null) {
$headers[] = 'Content-Type: application/json';
}
if ($idempotencyKey !== null) {
$headers[] = 'Idempotency-Key: ' . $idempotencyKey;
}
for ($attempt = 1; ; $attempt++) {
$retryAfter = 1;
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADERFUNCTION => function ($ch, $line) use (&$retryAfter) {
if (stripos($line, 'Retry-After:') === 0) {
$retryAfter = (int) trim(substr($line, 12));
}
return strlen($line);
},
];
if ($body !== null) {
$options[CURLOPT_POSTFIELDS] = json_encode($body);
}
$ch = curl_init('https://api.semalink.africa/v1' . $path);
curl_setopt_array($ch, $options);
$result = json_decode(curl_exec($ch), true);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status !== 429 || $attempt === 5) {
return [$status, $result];
}
sleep(max(1, $retryAfter));
}
}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
Stay under the limit
- Batch instead of looping. Sending to 1,000 people is one
POST /v1/batchesrequest costing 11, not 1,000 requests. - Use webhooks instead of polling. Let SemaLink tell you about delivery instead of asking for every message's status.
- Spread background jobs out. A nightly job that sends one request at a time, as fast as it can, will hit the limit. Run a few requests in parallel at most. Several servers sharing one organisation share its bucket too.
- Watch
RateLimit-Remaining. Slow down when it gets close to zero.