# Send a message

Sends a message on the chosen channel: SMS or WhatsApp today.

`POST https://api.rassilo.ai/v1/messages`

## Body parameters

|  |  |  |  |
| --- | --- | --- | --- |
| `channel` | `string` | required | The sending channel. — `sms` \| `whatsapp` |
| `to` | `string` | required | E.164 number, for example `+22200000000`. — `^\+[1-9]\d{6,14}$` |
| `body` | `string` | optional | The message text. May mix Arabic and French. — length 1–1600 |
| `template` | `string` | optional | A catalogue template, rendered by Rassilo instead of `body`. Send either `body` or `template`. Today: `identity_verify_otp_v1`. — `identity_verify_otp_v1` |
| `data` | `object` | optional | The template’s variables: required with `template`, not allowed without it. For `identity_verify_otp_v1`: `code` (4 to 8 digits) and `expiry_minutes` (1 to 60). |
| `language` | `string` | optional | The template variant to render: required with `template`, not allowed without it. — `ar` \| `fr` \| `en` |
| `category` | `string` | optional | The message category, which drives routing and priority. With `template`, it is the template’s own: any other value gives a `400`. — `transactional` \| `utility` \| `marketing` \| `authentication` |

## Headers

|  |  |  |  |
| --- | --- | --- | --- |
| `X-Idempotency-Key` | `string` | required | Replaying the same request with the same key does not send twice. The cURL samples generate it with `uuidgen` (on Debian, package `uuid-runtime`). — length 0–255 |

## Example request

**cURL**

```sh
curl https://api.rassilo.ai/v1/messages \
  -H "Authorization: Bearer $RASSILO_API_KEY" \
  -H "Content-Type: application/json" \
  -H "X-Idempotency-Key: $(uuidgen)" \
  -d '{
    "channel": "sms",
    "to": "+22200000000",
    "body": "Votre code Karam Pay est 482913."
  }'
```

**Node.js**

```node
// send.mjs
import { randomUUID } from 'node:crypto';

const response = await fetch('https://api.rassilo.ai/v1/messages', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.RASSILO_API_KEY}`,
    'Content-Type': 'application/json',
    'X-Idempotency-Key': randomUUID(),
  },
  body: JSON.stringify({
    channel: 'sms',
    to: '+22200000000',
    body: 'Votre code Karam Pay est 482913.',
  }),
});

const message = await response.json();
console.log(message.provisionalId, message.status); // … submitted
```

**Python**

```python
# send.py
import os
import uuid

import requests

response = requests.post(
    "https://api.rassilo.ai/v1/messages",
    headers={
        "Authorization": f"Bearer {os.environ['RASSILO_API_KEY']}",
        "X-Idempotency-Key": str(uuid.uuid4()),
    },
    json={
        "channel": "sms",
        "to": "+22200000000",
        "body": "Votre code Karam Pay est 482913.",
    },
)

message = response.json()
print(message["provisionalId"], message["status"])  # … submitted
```

**PHP**

```php
<?php
// send.php
$ch = curl_init('https://api.rassilo.ai/v1/messages');
curl_setopt_array($ch, [
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => [
        'Authorization: Bearer ' . getenv('RASSILO_API_KEY'),
        'Content-Type: application/json',
        'X-Idempotency-Key: ' . bin2hex(random_bytes(16)),
    ],
    CURLOPT_POSTFIELDS     => json_encode([
        'channel' => 'sms',
        'to'      => '+22200000000',
        'body'    => 'Votre code Karam Pay est 482913.',
    ]),
]);

$message = json_decode(curl_exec($ch), true);
echo $message['provisionalId'] . ' ' . $message['status']; // … submitted
```

**Java**

```java
// SendMessage.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.UUID;

public class SendMessage {
    public static void main(String[] args) throws Exception {
        String json = """
            {
              "channel": "sms",
              "to": "+22200000000",
              "body": "Votre code Karam Pay est 482913."
            }""";

        HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.rassilo.ai/v1/messages"))
            .header("Authorization", "Bearer " + System.getenv("RASSILO_API_KEY"))
            .header("Content-Type", "application/json")
            .header("X-Idempotency-Key", UUID.randomUUID().toString())
            .POST(HttpRequest.BodyPublishers.ofString(json))
            .build();

        HttpResponse<String> response = HttpClient.newHttpClient()
            .send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println(response.body()); // … "status": "submitted" …
    }
}
```

## Responses

**201** — Message created

```json
{
  "provisionalId": "msg_provisoire_0001",
  "channel": "sms",
  "to": "+22200000000",
  "provider": "bird",
  "providerMessageId": "bird_0001",
  "status": "submitted",
  "parts": 1,
  "bodyEncoding": "GSM7"
}
```

**400** — Invalid parameter

```json
{
  "error": {
    "code": "validation_failed",
    "fieldErrors": [
      {
        "field": "to",
        "code": "regex"
      }
    ]
  }
}
```

**422** — Message too long

```json
{
  "error": {
    "code": "message_too_long",
    "parts": 12,
    "bodyEncoding": "UCS2",
    "maxParts": 10
  }
}
```

## Error codes

| HTTP | Code |  |
| --- | --- | --- |
| `400` | — | Invalid request: a parameter, a missing or too long `X-Idempotency-Key` header, or a destination this channel does not support. |
| `401` | — | Key missing or incorrect. |
| `403` | `insufficient_scope` | The key is valid but lacks the scope this operation needs, or it is a publishable key. Nothing is created. |
| `413` | `payload_too_large` | The request body is too large. |
| `422` | `test_env_not_available`, `template_unavailable`, `message_too_long` | `test_env_not_available`: This refusal also answers any send with a test-environment key: test keys can read and preview, but not send yet. `template_unavailable`: This template has no variant for this channel, or no approved WhatsApp template is configured for this language. Nothing is created. `message_too_long`: The text needs more segments than the operator accepts in one send; `parts`, `bodyEncoding` and `maxParts` give the detail. The same text will fail the same way. |
| `502` | — | The operator rejected or did not accept the send. |
| `503` | — | Service temporarily unavailable: try again later. |
