Skip to content
pols.so docs
Esc
↑↓navigate↵open⌘Jpreview
On this page

Idempotency keys

Retry a sandbox create, a fork or a template save after a timeout or a lost answer without making, and paying for, a second one.

A create can succeed on the server while its answer is lost: the connection drops, a proxy times out, or an agent’s tool call is cut off. Retrying it blindly makes a second sandbox, which runs and is billed alongside the first. An idempotency key makes the retry safe: send the same key again, and pols returns what the first request made instead of making another.

KEY=$(uuidgen)
pols new --name ci-run --size large --idempotency-key "$KEY"
# the answer was lost? Run the same command again:
pols new --name ci-run --size large --idempotency-key "$KEY"   # the same sandbox, nothing new

Where it works

Operation CLI API MCP TypeScript Python
Create a sandbox pols new --idempotency-key POST /v1/sandboxes sandbox_create idempotency_key sandboxes.create(req, { idempotencyKey }) sandboxes.create(idempotency_key=)
Fork a sandbox pols fork --idempotency-key POST /v1/sandboxes/{sandbox}/fork sandbox_fork idempotency_key sandboxes.fork(..., { idempotencyKey }) sandboxes.fork(idempotency_key=)
Save or build a template pols template save and pols template build --idempotency-key POST /v1/templates none templates.create(req, { idempotencyKey }) templates.create and templates.build(idempotency_key=)

Over the API, send the key in the Idempotency-Key header, 1 to 255 characters:

curl -X POST https://api.pols.so/v1/sandboxes \
  -H "Authorization: Bearer $POLS_API_KEY" \
  -H "Idempotency-Key: 4f3c2a9e-7d1b-4c55-9a0e-2b8f6d1c3e77" \
  -H "Content-Type: application/json" \
  -d '{"name": "ci-run", "size": "large"}'

How it behaves

  • The same key and the same request within 24 hours, in the same org, gets the first request’s response, its status and body as they were, and creates nothing. Requests sent at the same moment with the same key create one sandbox.
  • The same key with a different request fails with 422 idempotency_key_reused. Use a new key for a new create.
  • Only a successful create is kept. After a request failed, for example with 403 quota_exceeded, the key can be used again.
  • Keys are per org and kept for 24 hours.

The response replayed is the one from the time of the first request: the sandbox it names may have moved on since, for example from pending to running. Wait for it, or fetch it with pols get, as after any create.

Choosing keys

Use a fresh random value, such as a UUID, for each create you mean to make, and reuse it only to retry that same create. None of the clients, the CLI or the MCP server retries on its own or makes a key for you: generate the key before the first attempt and keep it until you have an answer.

For an agent, a useful pattern is to derive the key from the task it is working on, so that a restarted agent that sends the same create again within the day gets the sandbox it already has.