---
title: Sandbox Lifecycle Webhooks
product: vercel
url: /docs/sandbox/lifecycle-webhooks
canonical_url: "https://vercel.com/docs/sandbox/lifecycle-webhooks"
last_updated: 2018-10-20
type: reference
prerequisites:
  - /docs/sandbox
related:
  - /docs/sandbox/concepts/persistent-sandboxes
  - /docs/webhooks
  - /docs/webhooks/webhooks-api
  - /docs/rest-api
  - /docs/sandbox/sdk/javascript
summary: Notify your backend when a Vercel Sandbox session starts, approaches its timeout, or stops, with the stop reason and session usage.
install_vercel_plugin: npx plugins add vercel/vercel-plugin
search_with_vercel_cli: npx vercel@latest docs search "<query>"
---

# Sandbox Lifecycle Webhooks

Sandbox lifecycle webhooks notify your backend when a sandbox session starts, is about to time out, or stops. Use them to track session state, extend a session before it times out, and clean up after it ends, including sessions that another caller started or stopped.

A session is one run of a sandbox. Resuming a sandbox starts a new session with a new ID. See [sandboxes and sessions](/docs/sandbox/concepts/persistent-sandboxes#sandboxes-and-sessions).

## Session events

| Event                      | Sent when                                                                                                                           |
| -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| `sandbox.session.started`  | A session starts, after you create or resume a sandbox.                                                                             |
| `sandbox.session.expiring` | A running session is about to time out, 1 minute before the timeout by default. See [expiry warnings](#configure-expiry-warnings).  |
| `sandbox.session.stopped`  | A session stops for any reason. The event includes the [stop reason](#stop-reasons) and the session's usage.                        |

A session that fails to start sends no events.

## Subscribe to session events

You need a Pro or Enterprise team, permission to configure its [account webhooks](/docs/webhooks#account-webhooks), and a public endpoint that accepts POST requests.

1. In your team's [**Settings > Webhooks**](https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fsettings%2Fwebhooks), select the Sandbox events, choose the target projects, and enter your endpoint URL. See [webhook setup](/docs/webhooks#configure-a-webhook) for the complete procedure.
2. Save the secret shown when you create the webhook.
3. In your endpoint, verify each request with the `x-vercel-signature` header before you process it. See [securing webhooks](/docs/webhooks/webhooks-api#securing-webhooks) for receiver examples.

## Read the payload

Each event uses the [webhook payload format](/docs/webhooks/webhooks-api#payload). `type` is the event name, and `payload.sandbox` and `payload.session` are the same sandbox and session objects the Sandbox API returns.

| Key                                          | [Type](/docs/rest-api#types) | Description                                                                                    |
| -------------------------------------------- | ------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| `payload.team.id`                            | [ID](/docs/rest-api#types)           | The team that owns the sandbox.                                                                |
| `payload.project.id`                         | [ID](/docs/rest-api#types)           | The project that owns the sandbox.                                                             |
| `payload.user.id`                            | [ID](/docs/rest-api#types)           | Always `null`. No user acts in a sandbox session event.                                        |
| `payload.sandbox.name`                       | [String](/docs/rest-api#types)       | The sandbox's name.                                                                            |
| `payload.sandbox.currentSessionId`           | [ID](/docs/rest-api#types)           | The sandbox's session when Vercel sent the event. In stopped events, the session that stopped. |
| `payload.sandbox.currentSnapshotId`          | [ID](/docs/rest-api#types)           | The sandbox's current snapshot, when it has one.                                               |
| `payload.sandbox.status`                     | [String](/docs/rest-api#types)       | The sandbox's status when Vercel sent the event. Always `stopped` in stopped events.           |
| `payload.sandbox.statusUpdatedAt`            | [Number](/docs/rest-api#types)       | When the sandbox's status last changed, in milliseconds since the epoch.                       |
| `payload.sandbox.expiresAt`                  | [Number](/docs/rest-api#types)       | When the running session times out, in milliseconds since the epoch. Absent in stopped events. |
| `payload.sandbox.expiryNotificationLeadTime` | [Number](/docs/rest-api#types)       | Expiry warning lead time in milliseconds. See [expiry warnings](#configure-expiry-warnings).   |
| `payload.session.id`                         | [ID](/docs/rest-api#types)           | The session the event is about.                                                                |
| `payload.session.status`                     | [String](/docs/rest-api#types)       | The session's status when Vercel sent the event.                                               |
| `payload.session.startedAt`                  | [Number](/docs/rest-api#types)       | When the session started, in milliseconds since the epoch.                                     |

Stopped events also include these fields:

| Key                                   | [Type](/docs/rest-api#types) | Description                                                 |
| ------------------------------------- | ------------------------------------------------------------- | ----------------------------------------------------------- |
| `payload.session.stoppedAt`           | [Number](/docs/rest-api#types)       | When the session stopped, in milliseconds since the epoch.  |
| `payload.session.stopReason`          | [String](/docs/rest-api#types)       | Why the session stopped. See [stop reasons](#stop-reasons). |
| `payload.session.duration`            | [Number](/docs/rest-api#types)       | Session duration in milliseconds.                           |
| `payload.session.activeCpuDurationMs` | [Number](/docs/rest-api#types)       | Active CPU time in milliseconds.                            |
| `payload.session.networkTransfer`     | [Object](/docs/rest-api#types)       | Data transfer as `ingress` and `egress`, in bytes.          |

This example shows a `sandbox.session.stopped` request body, shortened to the fields in the tables:

```json
{
  "id": "example_event_id",
  "type": "sandbox.session.stopped",
  "createdAt": 1750344802000,
  "payload": {
    "user": { "id": null },
    "team": { "id": "team_abc" },
    "project": { "id": "prj_abc" },
    "sandbox": {
      "name": "my-workspace",
      "currentSessionId": "sbx_session",
      "currentSnapshotId": "snap_abc",
      "status": "stopped",
      "statusUpdatedAt": 1750344801629
    },
    "session": {
      "id": "sbx_session",
      "status": "stopped",
      "startedAt": 1750344501629,
      "stoppedAt": 1750344801629,
      "stopReason": "timeout",
      "duration": 300000,
      "activeCpuDurationMs": 4200,
      "networkTransfer": { "ingress": 12543852, "egress": 15368 }
    }
  }
}
```

In a stopped event, `payload.sandbox` describes the stopped session, even if the sandbox has since resumed with a newer session. Fetch the sandbox from the Sandbox API when you need its current state.

Vercel sends the stopped event after it saves the session's stop time, usage, and any snapshot taken on shutdown. If the platform stops a session without a report from it, the event carries the `error` stop reason and zero usage values.

### Stop reasons

| `stopReason` | Meaning                                         |
| ------------ | ----------------------------------------------- |
| `requested`  | A caller stopped the session through the API.   |
| `snapshot`   | The session stopped to take a snapshot.         |
| `timeout`    | The session reached its timeout.                |
| `exited`     | The session's main process exited on its own.   |
| `error`      | The session failed, or the platform stopped it. |

A stop reason says how the session ended. It doesn't tell you whether the work inside the session succeeded.

## Configure expiry warnings

Vercel sends `sandbox.session.expiring` 1 minute before a session's timeout, so your backend can extend the session or save its work. To extend the session, call [`sandbox.extendTimeout()`](/docs/sandbox/sdk/javascript#sandbox.extendtimeout) in the TypeScript SDK or [`extend_execution_time_limit()`](/docs/sandbox/sdk/python#extend_execution_time_limit) in the Python SDK. Each extension sets a new deadline, and Vercel sends another warning before it.

This timeline shows a session with a 10 minute timeout that your backend extends once:

| Elapsed (m:ss)   | What happens                                                                                       |
| ---------------- | -------------------------------------------------------------------------------------------------- |
| 0:00             | The session starts. The deadline is 10:00.                                                         |
| 9:00             | Vercel sends `sandbox.session.expiring`.                                                           |
| 9:30             | Your backend extends the timeout by 5 minutes. The deadline moves to 15:00.                        |
| 14:00            | Vercel sends `sandbox.session.expiring` again. `payload.sandbox.expiresAt` holds the new deadline. |
| 15:00            | The session times out, and Vercel sends `sandbox.session.stopped` with the `timeout` stop reason.  |

To get more warning, set `expiryNotificationLeadTime` when you [create](/docs/rest-api/sandboxes/create-a-named-sandbox-2), [fork](/docs/rest-api/sandboxes/fork-a-named-sandbox-1), or [update](/docs/rest-api/sandboxes/update-a-sandbox) a sandbox through the Sandbox REST API. The value is in milliseconds, from `60000` (1 minute, the default) to `3600000` (1 hour). Create and fork apply it to the session they start, and a fork that omits it keeps the source sandbox's lead time. An update applies from the sandbox's next session.

Plan for these two cases:

- **Short deadlines**: When a deadline is already closer than the lead time, Vercel sends the warning right away. This happens when a session's timeout is shorter than the lead time, or when an extension leaves less than the lead time.
- **Missed warnings**: A session that stops before its warning is due doesn't send one. A failed timeout extension can also leave the original deadline without a warning, because Vercel drops that deadline's warning once you request the extension. For work that must finish before the timeout, also track `payload.sandbox.expiresAt` in your own code.

## Handle duplicate and delayed events

Vercel can deliver an event more than once, and a session's started and stopped events can arrive in either order. Deduplicate on the event type and `payload.session.id`, plus `payload.sandbox.expiresAt` for expiring events. A repeated event can carry a new event `id`, so don't deduplicate on `id` alone.

An event can also arrive after the session has changed, so check the session's current state before you act on it. See [HTTP responses](/docs/webhooks/webhooks-api#http-response) and [delivery attempts and retries](/docs/webhooks/webhooks-api#delivery-attempts-and-retries) for the delivery contract.

## Subscribe agents through Vercel MCP

Agents can subscribe to the same events through [Vercel MCP](/docs/agent-resources/vercel-mcp), which delivers them as MCP events to the callback URL that your MCP client registers. A subscription covers the whole team, so pass the team ID as `team_id`. The event's `data` field holds the webhook's `payload` object, and the connected user needs permission to manage the team's webhooks.

For example, an agent can subscribe to `sandbox.session.expiring` and call the [`extend_session_timeout`](/docs/agent-resources/vercel-mcp/tools/sandboxes/extend_session_timeout) tool with `data.session.id` to keep its sandbox running.

To set up a client, see [Vercel MCP now supports MCP events](/changelog/vercel-mcp-now-supports-mcp-events) and the [AI SDK cookbook](https://ai-sdk.dev/cookbook/node/mcp-events).

## Troubleshoot missing events

If your endpoint doesn't receive an event, check the following:

- **Subscription**: The webhook subscribes to the event, and its project scope includes the sandbox's project.
- **Recent webhook changes**: You created or last changed the webhook more than a minute before the event. Webhook changes can take up to a minute to apply.
- **Plan**: The team is on a Pro or Enterprise plan.
- **Session start**: The session started. A session that fails to start sends no events.
- **Expiry warnings**: The session was still running when its warning was due.
- **Sandbox name**: The session belongs to a sandbox with a [name](/docs/sandbox/concepts/persistent-sandboxes#sandbox-names).
- **Project transfers**: Nobody has started or completed a transfer of the project to another team.

## Search the docs from your terminal

Search the Vercel docs and read pages as Markdown with the Vercel CLI. No login required.

```bash
# Find docs pages by keyword
npx vercel@latest docs search "environment variables"
npx vercel@latest docs search "function timeout"

# Print a docs page as Markdown, by path or URL
npx vercel@latest docs inspect <page>
```

---

[View full sitemap](/docs/sitemap)


If you encounter incorrect information, can’t find what you need, need a capability Vercel doesn’t currently offer, or have any other feedback to report, please do it by following the instructions at https://vercel.com/feedback.md.
