Docs Home

Retrying API Requests Safely

A network error can leave you not knowing whether a request went through. Send an Idempotency-Key header with a write, and you can retry it without doing the work twice.

These operations take one:

OperationA retry with the same key gets
Create a clusterThe same cluster.provision Action, not a second cluster.
Delete a clusterThe same cluster.deprovision Action.
Change a cluster's planThe same cluster.change_plan Action.
Create a credentialThe same credential, with its secret.

Sending a key

curl -s https://api.bonsai.io/v1/clusters \
  -H "Authorization: Bearer $BONSAI_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 8e2c6a1e-5b7d-4c43-9f0a-2d4b1c9e7f31" \
  -d '{"name": "my-search", "plan": "sandbox", "region": "aws-us-east-1"}'

The key can be any 1 to 255 printable ASCII characters; a random UUID is a good choice. Use a new key for each change you mean to make, and the same key when retrying it.

Keys belong to the token that sent them, and are kept for 24 hours. Replays count toward your rate limit.

What a retry gets

  • The same key and the same request get the original response back, with an Idempotent-Replayed: true header. The response is rebuilt from what the first request made, as that is now: a replayed create returns the Action in its current state.
  • The same key with a different request is refused with 422 idempotency_key_reused.
  • A retry while the first request is still being handled gets 409 idempotency_key_in_use, with a Retry-After header.
  • A replay of something that's gone, such as a credential that has since been revoked, gets 409 idempotency_replay_unavailable. Use a new key if you mean to make a new one.
  • A request that fails doesn't use up its key. If a create is refused for want of a payment method, add the card, and send the same request with the same key.

Retrying without a key

Without a key, a retried write is a new request. A retried create can make a second cluster, and a retried delete, after the cluster is gone, gets 422 cluster_terminated rather than the Action. The Action is still in the cluster's Action list.

Ready to power your search with AI?

Launch a fully managed Elasticsearch or OpenSearch cluster, with built-in vector search and AI capabilities.

Create an AI Ready Search Cluster
Calming Bonsai waves