# Event scenarios

https://docs.sendsms.to/en/guides/scenario-events/

An event of your system — an order is ready, a cart was abandoned, an outage — becomes an SMS with the text of a scenario you set up in the cabinet.

## How it works

1. In the cabinet switch on an “on event” scenario and set up the text with `{{name}}` variables.
2. On the scenario page, “Connect via API”, take its identifier (UUID).
3. Your system sends an event: [POST /v1/scenarios/{id}/events](https://docs.sendsms.to/en/api/scenario-event/) with a number (`to`) or a list of numbers (`recipients`) and the variable values.
4. The state of the event: [GET /v1/scenarios/{id}/events/{eventId}](https://docs.sendsms.to/en/api/scenario-event-state/).

```bash
curl --max-time 30 --request POST \
  --url 'https://api.sendsms.to/v1/scenarios/SCENARIO_ID/events' \
  --header "X-API-Key: $SENDSMS_API_KEY" \
  --header "Idempotency-Key: $(uuidgen 2>/dev/null || cat /proc/sys/kernel/random/uuid)" \
  --header 'Content-Type: application/json' \
  --data '{
  "to": "994XXXXXXXXX",
  "variables": {
    "order_id": "A-10492",
    "place": "12 Nizami St"
  }
}'
```

Response 202:

```json
{
  "eventId": "00000000-0000-0000-0000-000000000011",
  "status": "queued",
  "messageId": "00000000-0000-0000-0000-000000000012"
}
```

## One number or a list

- `to` — one SMS goes the same way as [POST /v1/messages](https://docs.sendsms.to/en/api/send-sms/): channel, sender, funds and stop list are checked the same; the answer carries `messageId`.
- `recipients` — the event becomes a normal campaign of the scenario with a check and a report; the answer carries the state of the run.
- Send either `to` or `recipients`, not both.

## Repeats and quiet hours

- `Idempotency-Key` is required: the same key with the same body returns the original answer with `duplicate: true` and sends nothing again; the same key with another body gives 422 `idempotency_key_reused`.
- During quiet hours an event waits for their end (`status: scheduled`, `sendAt`) unless the scenario sends events at once.
- A promotional scenario sends only to numbers that agreed to advertising, otherwise 422 `consent_required`.
- A paused scenario answers 409 `scenario_not_active`.

> The list of scenarios is not given out through the API: take the identifier in the cabinet. The key needs the `messages.send` permission.

```bash
curl --max-time 30 --request GET \
  --url 'https://api.sendsms.to/v1/scenarios/SCENARIO_ID/events/EVENT_ID' \
  --header "X-API-Key: $SENDSMS_API_KEY"
```

Response 200:

```json
{
  "eventId": "00000000-0000-0000-0000-000000000011",
  "status": "sent",
  "messageId": "00000000-0000-0000-0000-000000000012"
}
```
