Not paying twice for a generation: the Idempotency-Key header

All in One AIUsing the AI API·· 2 views· 3 min read

In short: if the connection drops in the middle of a generation request, you do not know whether the image or the video was started. Repeating carelessly can give you two generations, and you pay for both. With the Idempotency-Key header, you repeat the request without that risk: the API recognises the value and returns the saved answer, without generating again.

How to use it

1 Generate a unique value for each generation. A UUID works well.
2 Send it in the Idempotency-Key header of POST /v1/media/generations, from the first attempt.
3 If you get no answer, repeat with the same value and the same body.

In Python, with the value generated once and reused in the attempts:

import os, time, uuid, requests

API = "https://api.interweb.co.mz/v1"
headers = {
"Authorization": "Bearer " + os.environ["INTERWEB_API_KEY"],
"Idempotency-Key": str(uuid.uuid4()),
}
body = {"model": "openai/gpt-image-2/text-to-image",
"input": {"prompt": "A bright, modern office with plants", "size": "1024x1024"}}

for attempt in range(3):
try:
r = requests.post(API + "/media/generations", headers=headers, json=body, timeout=30)
print(r.status_code, r.headers.get("Idempotent-Replayed"), r.json())
break
except requests.RequestException:
time.sleep(5)

With curl, on Linux and macOS, keep the value in a variable and repeat the same command until you get an answer:

IDEM=$(uuidgen)
curl "https://api.interweb.co.mz/v1/media/generations" \
-H "Authorization: Bearer $INTERWEB_API_KEY" \
-H "Idempotency-Key: $IDEM" \
-H "Content-Type: application/json" \
-d '{"model": "openai/gpt-image-2/text-to-image", "input": {"prompt": "A bright, modern office with plants", "size": "1024x1024"}}'

What the API answers

Situation Answer
First request with this value The generation runs normally.
Same value and same body The saved answer comes back, with the Idempotent-Replayed: true header. Nothing is generated or charged again.
Same value and a different body 409 with the code idempotency_conflict.
Same value, with the first request still in progress 409 with the code idempotency_conflict. Wait a little and repeat.

The rules

The value can have up to 255 visible characters and stays reserved for 24 hours, per key. Use the same value only to repeat the same operation: a new image gets a new value. If you got an error and fixed the request, use a new value too. The header is accepted in chat (POST /v1/chat/completions, POST /v1/messages and POST /v1/responses) and it is required when you buy credits through the API.

The OpenAI SDKs do not have the generation operations. Use plain HTTP requests, as in the examples. In a batch of images, each image has its own value. See creating many images at once.
The same rules apply to purchases, payments and support tickets through the API: see Idempotency-Key in our API.

Before a batch of generations, check your balance and the prices in All in One AI.

See All in One AI

SEE ALSO

Idempotency-Key in our API: repeating a purchase, payment or ticket without doing it twice

Timeouts, retries and idempotency: how not to charge a customer twice

Creating many AI images at once: the balance, the price and the request limit

Creating AI images through the API: the request, the status and saving the file

All in One AI

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from 321,75 MT/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.