> ## Documentation Index
> Fetch the complete documentation index at: https://docs.styne.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Wake triggers and schedules

> Wake a sleeping sandbox when a schedule comes due or a webhook arrives, and hand it the event, so an agent costs nothing between messages.

A sleeping sandbox costs no compute. With a trigger you do not keep the agent alive, poll for it, or run your own scheduler next to it.

## Recipe: an agent that sleeps until it is needed

**1. Create a sandbox that pauses when idle**

Create the sandbox with `autoPause` and a short `timeout`: it pauses on its own when idle, and a paused sandbox costs disk
only.

**2. Add a schedule for periodic work**

For example every 15 minutes:

```bash theme={"theme":"css-variables"}
curl -sS -X POST "https://api.styne.ai/sandboxes/$SANDBOX_ID/triggers" \
  -H "Authorization: Bearer $STYNE_API_KEY" -H 'Content-Type: application/json' \
  -d '{"type": "schedule", "schedule": {"cron": "*/15 * * * *", "timezone": "UTC"}, "deliver": {"file": {"dir": "/home/user/inbox"}}}'
```

**3. Add a webhook for messages from outside**

A chat bot, a CI system or your own backend:

```bash theme={"theme":"css-variables"}
curl -sS -X POST "https://api.styne.ai/sandboxes/$SANDBOX_ID/triggers" \
  -H "Authorization: Bearer $STYNE_API_KEY" -H 'Content-Type: application/json' \
  -d '{"type": "webhook", "webhook": {"verify": "token"}, "deliver": {"file": {"dir": "/home/user/inbox"}}, "keep_awake_seconds": 30}'
```

The answer carries the trigger's `url` and its `secret` (shown this once). A sender posts to the URL with the secret in
the `X-Wake-Token` header.

**4. Let it sleep**

Each firing wakes the sandbox, writes the event as a file into `/home/user/inbox`, keeps the sandbox awake 30 seconds so
your agent can act on it, and the sandbox pauses again. Your agent reads the folder when it starts or wakes, so it needs no
scheduler, no queue and no process that stays up between events.

## Trigger types

A trigger belongs to one sandbox and is one of two types:

| Type | Fires when |
| - | - |
| `schedule` | A cron expression comes due, in the time zone you name |
| `webhook` | A sender posts to the trigger's URL with a valid signature: a Telegram bot, Slack, Stripe, GitHub, or anything that can send a secret |

Each firing is an **event**. We store it before we answer the sender, wake the sandbox if it sleeps, and hand the event over.

## Choose how an event reaches the sandbox

You pick one `deliver` mode per trigger:

| `deliver` | What happens inside the sandbox | Taken when |
| - | - | - |
| `{"http": {"port": 8080, "path": "/telegram"}}` | `POST http://127.0.0.1:8080/telegram`. A webhook's body is posted exactly as the sender sent it, with its `Content-Type`; a schedule posts the event envelope (JSON). Headers `X-Wake-Event-Id` and `X-Wake-Trigger-Id` are added. Needs `curl` or `python3` in the image. | Your server answers 2xx |
| `{"file": {"dir": "/home/user/inbox"}}` | The event envelope is written to `/home/user/inbox/<event id>.json` (written to a temporary name, then renamed, so a reader never sees half a file). | The file is in place |
| `{"command": {"command": "python3 /app/on_event.py", "timeout_seconds": 60}}` | `sh -c` runs the command with the envelope on standard input and `WAKE_EVENT_ID`, `WAKE_TRIGGER_ID`, `WAKE_EVENT_SOURCE` in its environment. | Exit code 0 |

After a delivery the sandbox stays awake at least `keep_awake_seconds` (default 60, at most 3600), then sleeps again by its
own timeout and pause settings. Create the sandbox with `autoPause` so its timeout pauses it instead of ending it.

The envelope:

```json theme={"theme":"css-variables"}
{
  "id": "evt_5d0c1f3a9b2e4c7d8e6f1a2b3c4d5e6f",
  "trigger_id": "trg_8f2a...",
  "sandbox_id": "...",
  "source": "webhook",
  "received_at": "2026-11-13T09:00:00.120Z",
  "attempt": 1,
  "late": false,
  "content_type": "application/json",
  "headers": {"user-agent": "..."},
  "body": {"update_id": 42, "message": {"text": "hi"}}
}
```

`body` is the sender's body as JSON when it is JSON, as a string when it is text, else `null` with `body_base64`. A schedule
event has `"source": "schedule"`, `scheduled_for` and no body.

## Set a schedule

```json theme={"theme":"css-variables"}
{"type": "schedule",
 "schedule": {"cron": "*/15 * * * *", "timezone": "Europe/Berlin", "catch_up": "one"},
 "deliver": {"command": {"command": "python3 /app/tick.py"}}}
```

Five fields (minute hour day-of-month month day-of-week), minute resolution, any IANA time zone. A local time skipped by a
daylight-saving change does not fire that day; a repeated one fires once.

If we could not fire a slot on time (more than a minute late), `catch_up` decides: `one` (default) fires once for everything
missed, with `"late": true`; `skip` fires nothing for the missed slots. Either way the next slot is the next one in the future.

## Verify webhook signatures

The trigger's `webhook.verify` names how a request proves it comes from your sender:

| `verify` | Checked | Secret |
| - | - | - |
| `token` (default) | The secret in the URL (`/hooks/<id>/<secret>`) or the `X-Wake-Token` header | We make it |
| `telegram` | `X-Telegram-Bot-Api-Secret-Token` equals the secret | We make it; you pass it to `setWebhook` as `secret_token` |
| `github` | `X-Hub-Signature-256` | Paste the webhook secret you set on GitHub |
| `stripe` | `Stripe-Signature`, at most 5 minutes old | Paste the endpoint's signing secret (`whsec_...`) |
| `slack` | `X-Slack-Signature` with `X-Slack-Request-Timestamp`, at most 5 minutes old | Paste the app's signing secret |
| `standard` | Standard Webhooks (`webhook-id`, `webhook-timestamp`, `webhook-signature`), at most 5 minutes old | Paste the `whsec_...` secret |

The secret is shown once, when you create the trigger. A request that fails the check gets `401` and is not stored.

## Rely on delivery guarantees

* **Accepted means kept.** A webhook gets `200 {"ok": true, "event_id": ...}` only after the event is written and on disk.
  The sender never waits for the wake.
* **At least once.** If we crash after your sandbox took an event and before we recorded it, you get it again with the
  same `id`. Use the id to drop repeats.
* **Retries.** An event that is not taken (the port is not listening, the command exits non-zero, the wake fails) is tried
  again after 1, 2, 5, 10, 30, 60, 120, 300, 600, 1800 seconds, then hourly, until it is 24 hours old. Then it is **dead**:
  it stays in the event list with its last error for 7 days, and you can send it again.
* **Sender retries count once.** When the sender gives its own delivery id (Telegram `update_id`, GitHub
  `X-GitHub-Delivery`, Stripe event id, Standard Webhooks `webhook-id`, or your `Idempotency-Key` header), a retry of the
  same message is the same event.
* **Order.** Events of one sandbox are handed over one at a time, oldest first. A retried event can come after a newer one.

## Example: a Telegram bot that sleeps

The bot runs a small web server inside the sandbox. Telegram posts each update to the trigger's URL; the sandbox wakes, the
update is posted to the bot's port, and the bot answers through the Bot API.

**1. Create a sandbox and start the bot**

Create a sandbox that pauses when idle, and start the bot's server in it (any framework that accepts webhook updates; the
update arrives as Telegram sent it):

```python theme={"theme":"css-variables"}
from e2b import Sandbox

sbx = Sandbox.create("code", timeout=120, auto_pause=True)
sbx.files.write("/home/user/bot.py", BOT_SOURCE)          # the server below, on port 8080
sbx.commands.run("python3 /home/user/bot.py", background=True)
```

The bot itself is any server that takes the update and answers through the Bot API, for example:

```python theme={"theme":"css-variables"}
import json, os, urllib.request
from http.server import BaseHTTPRequestHandler, HTTPServer

TOKEN = os.environ["BOT_TOKEN"]

class Bot(BaseHTTPRequestHandler):
    def do_POST(self):
        update = json.loads(self.rfile.read(int(self.headers.get("Content-Length", 0))) or b"{}")
        msg = update.get("message") or {}
        answer = {"chat_id": msg["chat"]["id"], "text": f"you said: {msg.get('text', '')}"}
        req = urllib.request.Request(f"https://api.telegram.org/bot{TOKEN}/sendMessage",
                                     data=json.dumps(answer).encode(), headers={"Content-Type": "application/json"})
        urllib.request.urlopen(req, timeout=20).read()
        self.send_response(200)
        self.end_headers()

HTTPServer(("127.0.0.1", 8080), Bot).serve_forever()
```

**2. Give it a Telegram trigger**

```bash theme={"theme":"css-variables"}
curl -sS -X POST "https://api.styne.ai/sandboxes/$SANDBOX_ID/triggers" -H "Authorization: Bearer $STYNE_API_KEY" -H 'Content-Type: application/json' -d '{
  "type": "webhook",
  "webhook": {"verify": "telegram"},
  "deliver": {"http": {"port": 8080, "path": "/telegram"}},
  "keep_awake_seconds": 60
}'
```

The answer carries the trigger's URL and its secret (shown this once):
`{"id": "trg_...", "webhook": {"verify": "telegram", "url": "https://.../hooks/trg_...", "secret": "..."}, ...}`

**3. Point the bot at it**

Pass the secret as `secret_token`:

```text theme={"theme":"css-variables"}
curl -s "https://api.telegram.org/bot$BOT_TOKEN/setWebhook" \
  -d "url=$TRIGGER_URL" -d "secret_token=$TRIGGER_SECRET"
```

**4. Let the sandbox sleep**

Each message to the bot wakes it, is handed to `/telegram`, and the sandbox sleeps again 60 seconds
later. Telegram gets its `200` before the wake, so it never times out or resends because of a slow wake.

## Watch and replay events

Read a trigger's events with `GET /sandboxes/{id}/triggers/{trigger}/events?status=dead&limit=100&body=true`. `retry` sends a dead event again; `fire` runs the trigger now with the request body, for testing.
Each event shows `status` (`pending`, `delivering`, `delivered`, `dead`), `attempts`, `last_error`, `received_at`,
`delivered_at`, `receive_to_deliver_ms` and, when the delivery woke the sandbox, `wake_ms`.

## Limits

| Limit | Default |
| - | - |
| Triggers per account | 100 |
| Triggers per sandbox | 20 |
| Events per webhook trigger per minute | 600 (more answer `429` with `Retry-After`; senders retry) |
| Events per account per minute, all triggers | 3,000 |
| Body size | 256 KiB (`413` above) |
| Event kept after it finished | 7 days |

A suspended account's triggers are switched off and never wake a sandbox. While an account cannot pay, its events wait and
are tried again after a top-up. Deleting a sandbox deletes its triggers. Deleting the account removes all of its triggers
before its sandboxes are ended, and an event of a deleted account is never handed over. A wake by a trigger is billed like any other
resume.

## Errors

| Status | `error_code` | Meaning |
| - | - | - |
| 401 | `trigger_signature` | The signature or token does not match |
| 404 | `trigger_not_found` | No such trigger (or its sandbox was deleted) |
| 410 | `trigger_disabled` | The trigger is switched off |
| 410 | `account_deleted` | The account is deleted; no trigger can be made |
| 413 | | Body over 256 KiB |
| 429 | `trigger_rate` / `trigger_limit` | Too many events this minute / too many triggers |
| 503 | `trigger_store_unavailable` | Retry; nothing was stored |

## Operations

| Method | Path | What it does |
| - | - | - |
| `POST` | `/sandboxes/{id}/triggers` | Add a schedule or webhook trigger to a sandbox |
| `GET` | `/sandboxes/{id}/triggers` | List a sandbox's triggers |
| `GET` | `/sandboxes/{id}/triggers/{trigger}` | One trigger, with next\_fire\_at for a schedule |
| `PATCH` | `/sandboxes/{id}/triggers/{trigger}` | Enable or disable a trigger, or change its keep-awake time |
| `DELETE` | `/sandboxes/{id}/triggers/{trigger}` | Delete a trigger |
| `GET` | `/sandboxes/{id}/triggers/{trigger}/events` | List a trigger's events with their delivery status |
| `POST` | `/sandboxes/{id}/triggers/{trigger}/events/{event}/retry` | Send a dead event again |
| `POST` | `/sandboxes/{id}/triggers/{trigger}/fire` | Fire a trigger now with the request body, for testing |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.