Authentication and API keys
Every request carries an API key. The key says who you are, whether you're in the sandbox or live, and what you're allowed to do.
Test keys and live keys
| Test key | Live key | |
|---|---|---|
| Starts with | sk_test_ | sk_live_ |
| Talks to | The sandbox | Real phones on real networks |
| Costs | Nothing (a virtual balance) | KES 0.80 per SMS in Kenya |
| Available | From sign-up | Once your account is active |
livemode in responses | false | true |
There is one base URL, https://api.semalink.africa/v1. The key decides sandbox or live, so the same code runs in both: you only swap the key.
An organisation can have up to 10 active keys of each mode. Live keys can only be made once SemaLink has verified your business; until then every new key is a test key.
Create a key
In the dashboard, open Developer, then API keys, then Create key. Choose:
- Mode: test or live.
- Label: a name you'll recognise, such as "Billing server".
- Scopes: what the key may do (below). Give each key only what it needs.
- Allowed addresses (optional): the IP addresses the key works from.
- Expiry (optional): a date after which the key stops working.
You confirm with your two-factor code. The key is shown once: copy it straight into your secret store. SemaLink keeps only a hash and the first 16 characters (the prefix, such as sk_test_4f9a1c2e) so you can tell keys apart.
Owners and admins create, roll and revoke keys. Members can see the list. Owners get an email every time a key is created or revoked.
Send the key
Put the key in the Authorization header as a bearer token:
Authorization: Bearer sk_test_...curl https://api.semalink.africa/v1/balance \
-H "Authorization: Bearer $SEMALINK_API_KEY"2
const response = await fetch('https://api.semalink.africa/v1/balance', {
headers: { Authorization: `Bearer ${process.env.SEMALINK_API_KEY}` },
});
console.log(response.status, await response.json());2
3
4
import os
import requests
response = requests.get(
"https://api.semalink.africa/v1/balance",
headers={"Authorization": f"Bearer {os.environ['SEMALINK_API_KEY']}"},
timeout=30,
)
print(response.status_code, response.json())2
3
4
5
6
7
8
9
<?php
$ch = curl_init('https://api.semalink.africa/v1/balance');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . getenv('SEMALINK_API_KEY')],
]);
$balance = json_decode(curl_exec($ch), true);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE), PHP_EOL;
print_r($balance);2
3
4
5
6
7
8
9
{
"object": "balance",
"livemode": false,
"available": { "amount": 1000000, "currency": "KES", "display": "KES 10,000.00" },
"reserved": { "amount": 0, "currency": "KES", "display": "KES 0.00" }
}2
3
4
5
6
{
"error": {
"type": "authentication_error",
"code": "invalid_api_key",
"message": "This API key isn't one we know. Check it was copied whole, or make a new one in Developer > API keys.",
"doc_url": "https://docs.semalink.africa/errors#invalid_api_key",
"request_id": "req_5f1d0c2e8a9b4c3d9e7f6a5b4c3d2e1f"
}
}2
3
4
5
6
7
8
9
Scopes
A scope is one thing a key may do. A request without the scope it needs gets 403 with scope_missing.
| Scope | Lets the key | Endpoints |
|---|---|---|
messages:send | Send, estimate and cancel | POST /v1/messages, POST /v1/messages/estimate, POST /v1/batches, POST /v1/batches/{id}/cancel |
messages:read | Read messages and batches | GET /v1/messages, GET /v1/messages/{id}, GET /v1/batches/{id} |
balance:read | Read the balance | GET /v1/balance |
sender_ids:read | Read sender IDs | GET /v1/sender-ids |
pricing:read | Read prices | GET /v1/pricing |
webhooks:manage | Manage webhook endpoints and read events | /v1/webhooks, GET /v1/events/{id} |
GET /v1/account works with any key, whatever its scopes. It's the easiest way to check a key: it shows the key's mode, scopes, allowed_ips and expires_at.
A server that only sends one-time codes needs messages:send and nothing else. A reporting job needs messages:read. Small scopes make a leaked key less useful to anyone else.
Allowed addresses
Add the public IP addresses of your servers to a key and it works from nowhere else. A request from another address gets 403 with ip_not_allowed, and the message names the address we saw, so you can add it if it's yours:
This API key can't be used from 196.201.214.10: it isn't on the key's list of allowed addresses.Use an allowlist for any live key on a server with a fixed address.
Expiry
A key can have an expiry date. After it, requests get 401 with key_expired. Use expiry for keys given to a contractor or used for a one-off job.
Roll a key
Rolling makes a new key to replace an old one without downtime. In Developer, API keys, choose Roll on the key and pick how long the old key keeps working:
- Now: the old key stops at once (use this if it leaked).
- 1 hour
- 24 hours (the usual choice)
- 7 days
Deploy the new key, check your logs show it in use, and let the old one run out.
Revoke a key
Revoke stops a key within seconds. Requests with it get 401 with key_revoked. Revoking can't be undone.
SemaLink staff can also revoke a key, for example one that was posted publicly. You'll be told why.
Keys belong on your server
Never put a key in a browser, a mobile app or a public repository
Anyone who can read your web page or unpack your app can copy the key and send SMS on your account. The API refuses requests from browsers: any request with an Origin header gets 403 with browser_requests_not_allowed, and CORS is off for /v1. The one exception is GET /v1/openapi.json, which needs no key.
The safe pattern: your website or app calls your server, and your server calls SemaLink with the key.
- Keep keys in environment variables or a secret manager, never in source code.
- Use a separate key for each server or service, so you can revoke one without touching the others.
- If a key leaks, revoke it (or roll it with Now) straight away, then check Developer for requests you don't recognise.
More in Security best practices.