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

All in One AI··· 46 views· 3 min read

In short: send the request to POST /v1/media/generations, with the model and, in input, the description. The answer comes straight back, with the id. Then call GET /v1/media/generations/{id} every 5 seconds until the status is no longer processing. Image links are not permanent: download the files. See also the AI images page.

What you need

1 A key created on the API page in My MozOut, with ai:read and ai:write. Start with a sandbox_ key.
2 AI credits in your account, for the prod_ key. You buy them in All in One AI.
3 The model id. On the «Models» tab, the «Copy the id» button copies it.

The two steps

An image takes from a few seconds to a few minutes. So there are two steps: the request, which answers at once, and the status check, which you repeat until the image is done. The OpenAI SDK does not have these operations: use plain HTTP requests.

Step Operation What you get
1. The request POST /v1/media/generations Code 202, with the id, the status processing and the reserved credits in credits_reserved.
2. The check GET /v1/media/generations/{id} completed: the files in outputs and the real cost in credits_charged. failed: the reason in error, and nothing is charged.

A complete example, in Python

Keep the key in the MOZOUT_API_KEY environment variable and run:

import os, time, requests

API = "https://api.mozout.com/v1"
H = {"Authorization": "Bearer " + os.environ["MOZOUT_API_KEY"]}

job = requests.post(API + "/media/generations", headers=H, timeout=30, json={
"model": "openai/gpt-image-2/text-to-image",
"input": {"prompt": "A bright, modern office with plants and soft morning light", "size": "1024x1024"},
}).json()["data"]

while job["status"] == "processing":
time.sleep(5)
job = requests.get(API + "/media/generations/" + job["id"], headers=H, timeout=30).json()["data"]

print(job["status"], job["outputs"], job["credits_charged"])

The input fields change from model to model. GET /v1/models/{id} shows them, with the accepted values and the default for each one. Starting files, such as a photo to edit, go as public addresses (URLs).

Saving the files

The links in outputs are not permanent. Have your program download the images as soon as the generation finishes, and keep them on your server or your computer.

The key stays on the server. Never put it in code that runs in the browser or in a mobile app: anyone who sees it can spend your credits. See how to create an API key.
When you repeat a request after a network failure, send the Idempotency-Key header: that way you do not pay for two images. See how Idempotency-Key works. Without writing code, the aio command and the MCP server also create images with the same key. Everything is in the AI documentation.

Want AI images in your own program? See the AI images page.

See AI images

SEE ALSO

Testing All in One AI in the Sandbox at no cost: what each answer looks like

Creating an API key: name, mode, permissions, validity, IP list and spending limit

Our API's error codes: what each one means, and which ones are worth retrying

What an AI image costs in credits: the price per image, the reserve and the refund

RECOMMENDED PRODUCT

Web hosting with cPanel

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

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