# Idempotency

> Retry requests that create rentals or move money safely, with an Idempotency-Key.

Source: https://legion.ambient.xyz/docs/api/idempotency

A network error can leave you unsure whether a request went through. With an `Idempotency-Key`, you can send it again without renting twice or paying twice.

## Which operations take one

These operations require an `Idempotency-Key` header:

- [Rent GPUs on a host](https://legion.ambient.xyz/docs/api/rentals/create-rental) `POST /v1/rentals`
- [Make a single-use download link for a ready workspace export](https://legion.ambient.xyz/docs/api/rentals/create-workspace-export-download-link) `POST /v1/rentals/{rental_id}/workspace-exports/{export_id}/download-links`
- [Back up a stopped or failed rental's workspace](https://legion.ambient.xyz/docs/api/rentals/create-rental-backup) `POST /v1/rentals/{rental_id}/backups`
- [Mint a download link for a ready backup](https://legion.ambient.xyz/docs/api/rentals/create-backup-download-link) `POST /v1/backups/{backup_id}/download-links`
- [Create a service](https://legion.ambient.xyz/docs/api/services/create-service) `POST /v1/services`
- [Create a service key](https://legion.ambient.xyz/docs/api/services/create-service-key) `POST /v1/services/{service_id}/keys`
- [Top up the organization's balance](https://legion.ambient.xyz/docs/api/billing/create-top-up) `POST /v1/billing/top-ups`
- [Turn auto top-up on or off and set its amounts](https://legion.ambient.xyz/docs/api/billing/put-auto-top-up) `PUT /v1/billing/auto-top-up`
- [Refund unused credit to the cards and bank accounts it came from](https://legion.ambient.xyz/docs/api/billing/create-credit-refund) `POST /v1/billing/credit-refunds`
- [Redeem a promo code](https://legion.ambient.xyz/docs/api/billing/redeem-promo-code) `POST /v1/billing/promo-codes/redeem`
- [Ask for a destination to be added to the allow-list](https://legion.ambient.xyz/docs/api/network/create-egress-request) `POST /v1/egress-requests`
- [Schedule maintenance on one or many hosts](https://legion.ambient.xyz/docs/api/hosts/schedule-maintenance) `POST /v1/maintenance-windows`

Stopping, starting and deleting a rental are idempotent by themselves, and take no key.

## How it works

- Choose a key for each thing you mean to do, such as a UUID. It can be up to 255 characters.
- Send the same key and the same body again, and you get the original result. Nothing is done twice.
- Send the same key with a different body, and you get `422 idempotency_key_reused`.
- Use a new key for a new request.

## Example: rent a GPU

This request rents a GPU and spends credit.

```sh
curl -s -X POST "https://api.legion.ambient.xyz/v1/rentals" \
  -H "Authorization: Bearer $LEGION_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 6f1c2b8e-3d4a-4f5e-9b7c-2a1d0e8f9c3b" \
  -d '{
    "host_id": "8c0e2f4a-1b3d-4e5f-9a7b-6c5d4e3f2a1b",
    "gpu_count": 1,
    "image": "pytorch/pytorch:2.4.0-cuda12.4-cudnn9-runtime",
    "ssh_public_key": "ssh-ed25519 AAAA… you@laptop"
  }'
```

If the request times out, send exactly the same command again. If the rental was created, you get it back; if not, it's created now.

`web_terminal` defaults to `true` and `ssh_agent_forwarding` to `false`. See [createRental](https://legion.ambient.xyz/docs/api/rentals/create-rental).
