# Sendrail Messaging API

Sendrail sends SMS over its own network of GSM gateways. This page is the whole documentation in one Markdown file, meant for language models and tools. The same content for people is at https://sendrail.cz/docs, the OpenAPI description at https://api.sendrail.cz/v1/openapi.json.

- Base URL: `https://api.sendrail.cz`
- Requests and answers are JSON (`Content-Type: application/json`), UTF-8.
- Times are UTC, written `YYYY-MM-DD HH:MM:SS`.
- Prices are in CZK without VAT, per segment.

## Authentication

Every request carries the API key: `Authorization: Bearer sr_live_…` (the header `X-API-Key: sr_live_…` works too). Keys are created in the dashboard at https://sendrail.cz/app. An account can have up to 20 keys; each has its own plan, rules, limits and webhook. A missing, unknown or paused key answers `401 invalid_api_key`. Live keys start with `sr_live_`, test keys (sandbox, nothing is sent or charged) with `sr_test_`.

Libraries: PHP `composer require sendrail/sendrail-php` (https://github.com/sendrailcz/sendrail-php), Nette `sendrail/nette`, Laravel `sendrail/laravel`, n8n community node `n8n-nodes-sendrail`. Everything else uses plain HTTP; the OpenAPI file at https://api.sendrail.cz/v1/openapi.json generates a client for any language.

## Send a message

`POST /v1/messages`

| Field | Type | |
|---|---|---|
| `to` | string, required | Recipient in international format, `+420777123456`. A nine-digit number without a prefix is taken as Czech. |
| `message` | string | The text. At most 10 segments (1530 characters in GSM-7). Required unless `template` is given. |
| `template` | string, optional | Name of a stored template to send instead of `message`. |
| `variables` | object, optional | Values for the `{{name}}` places of the template. |
| `idempotency_key` | string, optional | Up to 120 characters; also accepted as the header `Idempotency-Key`. |
| `dry_run` | boolean, optional | `true` = check and price the message, send nothing, charge nothing. |
| `type` | string, optional | `"bulk"` for a mass mailing, see below. |

```
curl -X POST https://api.sendrail.cz/v1/messages \
  -H "Authorization: Bearer $SENDRAIL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"to": "+420777123456", "message": "Your code is 482913"}'
```

Answer `202 Accepted`:

```
{
  "id": "msg_5f0c9a1e7b2d4c8a9e01",
  "status": "queued",
  "to": "+420777123456",
  "country": "CZ",
  "segments": 1,
  "encoding": "GSM-7",
  "tier": "standard",
  "type": "normal",
  "price": 0.5,
  "currency": "CZK",
  "trial": false,
  "via_backup_route": false,
  "error": null,
  "test": false,
  "created_at": "2026-11-25 10:15:02",
  "final_at": null
}
```

Python:

```
import os, requests
r = requests.post("https://api.sendrail.cz/v1/messages",
    headers={"Authorization": f"Bearer {os.environ['SENDRAIL_API_KEY']}"},
    json={"to": "+420777123456", "message": "Your code is 482913", "idempotency_key": "order-1042-code"},
    timeout=20)
r.raise_for_status()
message = r.json()
```

Node.js:

```
const r = await fetch("https://api.sendrail.cz/v1/messages", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.SENDRAIL_API_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({ to: "+420777123456", message: "Your code is 482913", idempotency_key: "order-1042-code" }),
});
const message = await r.json();
```

PHP:

```
$ch = curl_init("https://api.sendrail.cz/v1/messages");
curl_setopt_array($ch, [
  CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 20,
  CURLOPT_HTTPHEADER => ["Authorization: Bearer " . getenv("SENDRAIL_API_KEY"), "Content-Type: application/json"],
  CURLOPT_POSTFIELDS => json_encode(["to" => "+420777123456", "message" => "Your code is 482913"]),
]);
$message = json_decode(curl_exec($ch), true);
```

## Segments and price

A message is charged per segment. With only GSM-7 characters one segment holds 160 characters, a longer text is split into parts of 153. One character outside GSM-7 (Czech diacritics such as `ě š č ř`, emoji) switches the message to UCS-2: 70 characters per segment, 67 when split. The answer says which `encoding` was used and how many `segments` were charged. The price of a segment depends on the destination country and the plan of the key: `GET /v1/prices` (no key needed).

## Idempotency and repeats

If a request times out you cannot know whether it was accepted. Send an `idempotency_key` with every message and repeat the same request: the same key on the same API key returns the first message with `"duplicate": true` and `200`, and nothing is sent or charged again.

Independently of that, the same text to the same number from the same key within five minutes is not sent again. The answer is the first message with `"duplicate": true`. A message that failed or was cancelled does not count, so it can be sent again right away.

## Message status

`GET /v1/messages/{id}` returns the message in the same shape as above.

`GET /v1/messages` lists the messages sent with the key, newest first, each with its text: `{"items": [ … ], "next_before": "msg_…"}`. Parameters: `limit` (1 to 100, default 20), `status`, `to` and `before`; pass `next_before` as `before` for the next, older page.

| `status` | Meaning |
|---|---|
| `scheduled` | A bulk message waiting for its turn. |
| `queued` | Accepted, on its way to a gateway. |
| `sent` | Handed to the mobile network. Final. |
| `failed` | Could not be sent; refunded. `error` says why. Final. |
| `cancelled` | Cancelled before it left; refunded. Final. |
| `rejected` | Refused by the gateway; refunded. Final. |
| `dry_run` | Only in the answer to a request with `"dry_run": true`: checked and priced, nothing was sent or stored. |

Typical time from request to `sent` is around ten seconds. `via_backup_route` is `true` when a Garance message left through the backup operator.

## Webhooks

Set a webhook URL on the key in the dashboard. When a message reaches a final status we `POST` to it:

```
{"id":"evt_msg_5f0c9a1e7b2d4c8a9e01","timestamp":1795867200,"event":"message.final","message":{"id":"msg_5f0c9a1e7b2d4c8a9e01","status":"sent", …}}
```

An integration can set the address itself: `GET /v1/webhook` returns `{"url", "secret", "events"}`, `PUT /v1/webhook` with `{"url": "https://…"}` sets it and `DELETE /v1/webhook` removes it. This works only after the owner ticks "Povolit správu webhooku přes API" at the key in the dashboard; otherwise the answer is `403 webhook_api_disabled`. A key has one webhook address of its own.

More addresses: an integration that subscribes by itself (Make, Zapier, your own services) adds its own address instead of taking the key's one. `POST /v1/webhooks` with `{"url": "https://…", "events": ["message.final"]}` answers `201` with `{"id": "wh_…", "url", "secret", "events", "created_at"}`; `events` is optional and means all when left out. `GET /v1/webhooks` lists them as `{"items": [ … ]}` and `DELETE /v1/webhooks/{id}` removes one. A key can have up to 10, each with its own signing secret, and every address gets every chosen event once. Adding an address the key already has changes its events and answers `200`. An address that answers `410 Gone` is removed; other failures are retried. The same owner's switch applies (`403 webhook_api_disabled` otherwise), and the owner sees and can remove these addresses in the dashboard.

The header `X-Sendrail-Signature` is `sha256=` followed by the hex HMAC-SHA256 of the raw request body, keyed with the signing secret shown next to the key in the dashboard:

```
expected = hmac.new(secret.encode(), raw_body, hashlib.sha256).hexdigest()
ok = hmac.compare_digest("sha256=" + expected, request.headers["X-Sendrail-Signature"])
```

Answer with any `2xx` within 5 seconds. Otherwise we retry after 1 minute, 5 minutes, 15 minutes, 1 hour and 6 hours, then give up. The URL must be public `https`; redirects are not followed.

Repeats and replays: every body starts with `id` and `timestamp`. The `id` (`evt_…`) stays the same when an event is repeated, so remember handled ids and skip a repeat. The `timestamp` is the Unix time of that delivery attempt: after checking the signature, refuse a body older than about five minutes. Both are inside the signed body; the headers `X-Sendrail-Event-Id` and `X-Sendrail-Timestamp` only repeat them.

## Test keys (sandbox)

A key created as "testovací" in the dashboard starts with `sr_test_`. It goes through every check of a real key (number, country, key rules and limits, opt-out list, templates) and answers in the same shape, but nothing is sent and nothing is charged. Use it in development and automated tests.

- The message starts as `queued` and becomes `sent` after about three seconds; the webhook fires as usual. Messages carry `"test": true` and ids starting with `msg_test_`.
- A recipient whose number ends in `0000` always ends as `failed`.
- `price` shows what the message would cost with a live key.
- Verification codes come back in the answer as `test_code`.
- Trial restrictions do not apply. There are no replies. Test messages are deleted after 30 days.

## Verification codes (OTP)

We make the code, send it and remember it; you ask whether the code the user typed is right. The SMS is charged as an ordinary message, checking is free.

`POST /v1/verify`

| Field | Type | |
|---|---|---|
| `to` | string, required | The number to verify. |
| `length` | integer, optional | Digits of the code, 4 to 8, default 6. |
| `ttl` | integer, optional | Seconds the code is valid, 60 to 900, default 300. |
| `lang` | string, optional | Language of the standard text: `cs` (default), `sk`, `en`. |
| `app` | string, optional | Name put in front of the text, at most 20 characters. |
| `template` | string, optional | Your own stored template instead of the standard text; it must contain `{{code}}` and may use `{{minutes}}` and `{{app}}`. |

Answer `202`:

```
{"id": "ver_3b1f0c9a1e7b2d4c8a9e01aa", "status": "pending", "to": "+420777123456", "expires_at": "2026-11-26 10:20:02",
 "attempts_left": 5, "message_id": "msg_5f0c9a1e7b2d4c8a9e01", "created_at": "2026-11-26 10:15:02",
 "segments": 1, "price": 0.5, "currency": "CZK"}
```

The standard Czech text is "Moje aplikace: váš ověřovací kód je 482913. Platí 5 min."

`POST /v1/verify/{id}/check` with `{"code": "482913"}` answers `200` with the same object plus `"valid": true|false`. `valid` is `true` exactly once: for the right code, before it expires, within five tries.

| `status` | Meaning |
|---|---|
| `pending` | Waiting for the right code. |
| `approved` | The right code was given. Final. |
| `failed` | Five wrong codes. Final. |
| `expired` | Time ran out. Final. |
| `replaced` | A newer code was sent to the same number. Final. |

`GET /v1/verify/{id}` returns the state. At most 3 codes per number in 10 minutes (`429 verify_rate_limited`). Only a hash of the code is stored. With a test key the answer to `POST /v1/verify` also has `test_code`. Never return the code or `test_code` to the person being verified; do the check on your server.

## Templates

A template is a stored text with places for values, e.g. `Dobrý den {{name}}, objednávka {{order}} je připravena.` Names: small letters, digits, `-`, `_`, at most 40 characters; at most 100 templates per account.

- `GET /v1/templates` → `{"items": [{"name": "order-ready", "message": "…", "variables": ["name", "order"], "updated_at": "…"}]}`
- `PUT /v1/templates/{name}` with `{"message": "…"}` creates or changes one.
- `DELETE /v1/templates/{name}` removes it.

Send: `{"to": "+420777123456", "template": "order-ready", "variables": {"name": "Jana", "order": "1042"}}`. A missing value answers `400 missing_variable`, an unknown name `404 template_not_found`.

## Number check

`POST /v1/numbers/check` with `{"number": "777 123 456"}`, or up to 100 numbers in `{"numbers": [...]}` (the answer is then `{"results": [...]}`). Free.

```
{"input": "777 123 456", "valid": true, "number": "+420777123456", "country": "CZ", "type": "mobile",
 "sms_capable": true, "supported": true, "price_per_segment": 0.5, "tier": "standard", "currency": "CZK",
 "allowed_by_key": true, "opted_out": null}
```

An invalid number answers `{"input": "…", "valid": false}`. `type` is `mobile`, `landline` or `special` for Czech and Slovak numbers (by the range the number was issued from) and `unknown` elsewhere. `supported` says whether we send to that country on the key's plan; `allowed_by_key` whether the key's country and recipient rules allow it; `opted_out` is `null`, `"all"` or `"bulk"`. This is not a live lookup in the mobile network.

## Opt-outs

Every account has a list of numbers it must not write to. A message to a listed number is refused with `403 recipient_opted_out` and not charged. `scope` is `all` (nothing is sent) or `bulk` (only mass mailings are held back).

- `GET /v1/optouts` → `{"items": [{"number": "+420777123456", "scope": "bulk", "source": "stop", "note": "reply \"STOP\"", "created_at": "…"}]}`
- `POST /v1/optouts` with `{"number": "…", "scope": "all", "note": "…"}` adds or changes an entry.
- `DELETE /v1/optouts/{number}` removes it (write `+` as `%2B`).

Reply STOP: when a person answers with nothing but `STOP`, `Odhlásit` or `Unsubscribe`, the number is added with scope `bulk` and source `stop`, and an event goes to the key's webhook:

```
{"event":"optout.created","data":{"number":"+420777123456","scope":"bulk","source":"stop","received_at":"2026-11-25 10:14:02"}}
```

Messages leave from shared numbers, so it is not always certain whose message the person answers. Therefore a STOP never blocks transactional messages, and it is applied to every account that sent that number a mass mailing in the last 30 days. Remove the entry if it is not right; change its scope to `all` if the person should get nothing.

## Bulk sending

Mass mailings (newsletters, campaigns, any larger batch not triggered by a single user action) must be marked as bulk. A bulk message is priced by the bulk line of the price list, is sent only over our own gateways and has the lowest priority: it leaves when nothing else is waiting, a few messages a minute, within daytime hours.

- One message: add `"type": "bulk"` to `POST /v1/messages`.
- Up to 500 at once: `POST /v1/messages/batch` with `{"message": "…", "to": ["+420…", "+420…"]}`, or `{"messages": [{"to": "…", "message": "…", "idempotency_key": "…"}, …]}` for a different text per recipient. `"dry_run": true` works here too.

The answer is `{"accepted": n, "rejected": n, "results": [...]}`; every recipient has its message `id`, `status`, `segments` and `price`, or an `error`. Accepted messages are charged at once and start as `scheduled`. Bulk sending is not available in trial mode. You are responsible for having the recipients' consent and for offering a way to opt out.

## Inbound: replies and missed calls

On accounts with inbound switched on (ask at info@sendrail.cz), `GET /v1/inbound?limit=50` lists the latest replies and missed calls of the key:

```
{"items": [{"type": "message", "id": 812, "from": "+420777123456", "message": "Yes, I will come", "received_at": "2026-11-25 10:14:02"},
           {"type": "missed_call", "id": 77, "from": "+420777123456", "message": null, "received_at": "2026-11-25 10:20:41"}]}
```

A reply or a call is assigned to the API key that last wrote to that number; messages leave from shared numbers, so this is best effort. With a webhook URL on the key each of them is also posted, signed like message events:

```
{"event":"message.received","data":{"id":812,"from":"+420777123456","message":"Yes, I will come","received_at":"2026-11-25 10:14:02"}}
{"event":"call.missed","data":{"id":77,"from":"+420777123456","received_at":"2026-11-25 10:20:41"}}
```

## Cancel

`POST /v1/messages/{id}/cancel` cancels a message that is still `scheduled` or `queued` and refunds it. A message that already left answers `409`.

## Account

`GET /v1/account`:

```
{"credit": 182.5, "bonus_credit": 0.0, "currency": "CZK", "trial_messages_left": 0, "trial_mode": false,
 "key": {"tier": "standard", "allowed_countries": "CZ,SK",
         "limits": {"messages_per_day": 50, "spend_per_day": 100.0, "allowed_recipients": null,
                    "messages_last_24h": 3, "spend_last_24h": 1.5}}}
```

Bonus credit is credit granted by us; it is spent first and pays only for the Standard plan.

## Key rules, plans and limits

| Plan | Behaviour |
|---|---|
| Standard | Sent over our own gateway network only. If it cannot be sent, the message ends as `failed` and is refunded. |
| Garance | If our own network cannot send within about a minute, the message goes out through a contracted backup operator at the same price. |

Each key can also carry rules set in the dashboard (not through the API):

- allowed destination countries: anything else answers `403 country_not_allowed`;
- allowed recipients: a list of numbers, anything else answers `403 recipient_not_allowed`;
- messages per 24 hours and spend per 24 hours: above them the key answers `429 daily_limit_reached` until older messages fall out of the window. Failed, cancelled and rejected messages do not count.

Use them for keys handed to scripts, test environments and AI agents.

## Trial mode

A new account gets 100 free messages. Until the first top-up they can be sent only to the phone number verified at sign-up (`403 trial_recipient` otherwise) and only with a key on the Standard plan.

## Errors

Errors are JSON: `{"error": {"code": "…", "message": "…"}}`.

| HTTP | Code | What to do |
|---|---|---|
| 400 | `missing_field`, `invalid_number`, `message_too_long`, `invalid_type`, `invalid_field`, `missing_variable`, `template_without_code`, `invalid_name`, `invalid_message`, `too_many`, `batch_too_large`, `invalid_cursor`, `invalid_url`, `rejected` | Fix the request; repeating it unchanged fails again. |
| 401 | `invalid_api_key` | Check the key and that it is not paused. |
| 402 | `insufficient_credit`, `trial_exhausted`, `plan_requires_credit` | Top up credit in the dashboard. Garance is paid only from topped-up credit. |
| 403 | `country_not_allowed`, `recipient_not_allowed`, `recipient_opted_out`, `trial_recipient`, `phone_not_verified`, `account_blocked`, `inbound_not_enabled`, `webhook_api_disabled` | Change the key's rules in the dashboard, verify the phone, or contact us. `webhook_api_disabled`: the owner allows it at the key in the dashboard. |
| 404 | `not_found`, `template_not_found` | The message, verification or template does not exist on the account. |
| 409 | `already_final`, `already_sending`, `webhook_limit` | The message can no longer be cancelled. `webhook_limit`: the key already has 10 webhook addresses. |
| 422 | `country_not_supported` | We do not send to that country on this plan yet. |
| 429 | `rate_limited`, `bulk_queue_full`, `daily_limit_reached`, `verify_rate_limited` | Slow down and retry later; a daily limit is raised by the key's owner in the dashboard. |
| 502 | `gateway_error` | Nothing was charged. Retry with the same `idempotency_key`. |

## Limits

60 messages per minute per API key; above that, and for every mass mailing, use bulk sending. Give the HTTP client a timeout of at least 20 seconds.

## MCP server for AI agents

`https://api.sendrail.cz/mcp` is a Model Context Protocol server (Streamable HTTP, stateless, JSON answers). It authenticates with the same API key in the `Authorization: Bearer` header and is bound by the same rules, limits and prices as the REST API.

Clients that cannot send a header (connectors in Claude on the web, ChatGPT) sign in with OAuth 2.1: give them only the address `https://api.sendrail.cz/mcp`. Discovery is at `https://api.sendrail.cz/.well-known/oauth-protected-resource` and `https://sendrail.cz/.well-known/oauth-authorization-server`; dynamic client registration, authorization code with PKCE (S256), public clients, rotating refresh tokens. The account owner picks the API key the application acts as; the tokens work on `/mcp` only and carry that key's limits.

| Tool | What it does |
|---|---|
| `send_message` | Sends one SMS (`to`, `message`, optional `idempotency_key`). |
| `estimate_message` | Checks and prices a message without sending. |
| `get_message` | Status of a message by `id`. |
| `cancel_message` | Cancels a message that has not left yet. |
| `list_messages` | Latest messages of the key, with text. |
| `list_inbound` | Replies and missed calls (inbound must be switched on). |
| `get_account` | Credit, plan, rules, limits and usage. |
| `check_number` | Validity, country, kind and price for a number, without sending. |
| `get_prices` | Price list. |

Claude Code:

```
claude mcp add --transport http sendrail https://api.sendrail.cz/mcp --header "Authorization: Bearer $SENDRAIL_API_KEY"
```

Cursor (`~/.cursor/mcp.json`):

```
{"mcpServers": {"sendrail": {"url": "https://api.sendrail.cz/mcp", "headers": {"Authorization": "Bearer sr_live_…"}}}}
```

Codex (`~/.codex/config.toml`):

```
[mcp_servers.sendrail]
url = "https://api.sendrail.cz/mcp"
bearer_token_env_var = "SENDRAIL_API_KEY"
```

The server cannot top up credit, create or change keys, change limits, or send bulk mailings. With a test key (`sr_test_…`) the tools work the same and nothing is sent.

## n8n

Use the HTTP Request node with a "Header Auth" credential (name `Authorization`, value `Bearer <API key>`). Workflows to import: https://sendrail.cz/n8n