# API and webhooks

Sendlane gives each account its own WhatsApp number on an isolated session. Your app sends messages with a REST API and receives replies on signed webhooks.

Base URL: `https://sendlane.amm-labs.com/api/v1`

Machine-readable versions of this page: [Markdown](/docs.md), [OpenAPI 3.1](/openapi.json), [llms.txt](/llms.txt) and [llms-full.txt](/llms-full.txt).

## Quickstart for AI agents

Follow this order. Every step names the exact request and the field to check.

1. Get an API token from a human. They create it under **API keys** in the dashboard. Send it as `Authorization: Bearer <token>` on every request. Never put it in a URL.
2. `GET /session`. Read `status`.
   - `ready`: go to step 4.
   - anything else: go to step 3.
3. `POST /session/start`. If the response has a `qr` field, a human must scan that data URI with the phone. Poll `GET /session` every 3 to 5 seconds until `status` is `ready`. Refresh the code with `GET /session/qr` if it expires. Do not call `POST /session/start` more than 6 times a minute.
4. `POST /messages` with `{ "to": "<chat id>", "text": "<message>" }`. A `200` means the message was handed to the WhatsApp session.
5. Replies arrive on the webhook URL the human set in the dashboard. Verify `X-Webhook-Signature` before you trust a payload.

Rules to follow:

- A token is bound to one account and one session. You cannot choose another session.
- Treat `409` as "pairing is not finished". Do not retry in a tight loop. Ask a human or poll `GET /session`.
- Treat `429` as "slow down". Wait, then retry with a longer delay.
- Treat `503` as "the WhatsApp sidecar is down". Retry later and tell a human if it persists.
- Only message people who have opted in. WhatsApp can ban numbers that look like spam.

## Authentication

Create a personal access token under **API keys** in the dashboard and send it with every request.

```http
Authorization: Bearer <token>
Accept: application/json
```

A token belongs to one account and always uses that account's WhatsApp session (`u{userId}`). You cannot name another session.

## Session

### GET /session

The current pairing state.

```json
{
  "session_id": "u42",
  "status": "ready",
  "phone_number": "966512345678",
  "push_name": "My Shop",
  "ready_at": "2026-11-28T10:00:00+00:00"
}
```

### POST /session/start

Boots the session and returns a QR code data URI when pairing is needed. Limited to 6 requests per minute.

```json
{ "session_id": "u42", "status": "qr", "qr": "data:image/png;base64,..." }
```

### GET /session/qr

Refreshes the QR code while pairing. Limited to 12 requests per minute.

## Messages

### POST /messages

Sends a text message from the paired number. Limited to 60 requests per minute.

```json
{
  "to": "966512345678@c.us",
  "text": "Hello from my app"
}
```

`to` can be a chat ID (`...@c.us`) or a number the sidecar accepts. You get a `409` if the session is not `ready`, and a `200` with the send result under `result` on success.

```bash
curl -X POST https://sendlane.amm-labs.com/api/v1/messages \
  -H "Authorization: Bearer $SENDLANE_TOKEN" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -d '{"to":"966512345678@c.us","text":"Hello from my app"}'
```

## Health

### GET /health

An authenticated, shallow check of the database and the WhatsApp stack. Returns `503` when something is unhealthy.

## Errors

| Code | Meaning | What to do |
|------|---------|------------|
| 401 | Missing or invalid token | Stop. Ask a human for a valid token |
| 409 | Session not connected | Finish pairing, then retry |
| 429 | Rate limited | Wait, then retry with backoff |
| 503 | The WhatsApp sidecar is unreachable | Retry later |

## Webhooks

When someone messages a paired number, Sendlane queues a POST to the webhook URL you set in the dashboard. Set a signing secret of at least 16 characters and store the same value in your app.

```http
POST https://your-app.example.com/webhooks/sendlane
Content-Type: application/json
X-Webhook-Signature: sha256=<hex>
```

```json
{
  "event": "message.received",
  "data": {
    "session_id": "u42",
    "from": "966512345678@c.us",
    "body": "Hello",
    "type": "chat"
  }
}
```

| Event | When |
|-------|------|
| `message.received` | Someone messaged your number |
| `session.ready` | The phone paired successfully |
| `session.disconnected` | The session dropped. Includes a `reason` |

Messages with `fromMe: true` are not forwarded.

## Verify a signature

The signature is an HMAC-SHA256 of the raw request body. Compare it in constant time before you trust the payload.

```php
$payload  = $request->getContent();
$expected = 'sha256='.hash_hmac('sha256', $payload, $secret);

if (! hash_equals($expected, $request->header('X-Webhook-Signature', ''))) {
    abort(401);
}
```

```javascript
const crypto = require('crypto');

const expected = 'sha256=' +
  crypto.createHmac('sha256', secret).update(rawBody).digest('hex');
```

## Retries and local testing

Delivery runs on a queue with 5 attempts and exponential backoff. Return a 2xx quickly and do the heavy work asynchronously.

Sendlane cannot reach `localhost` on your laptop. Expose your endpoint with a tunnel or use a staging URL while you test.
