How to call a FLUX API from Python

To use FLUX from Python, POST your prompt as JSON with requests, read the base64 image from the answer and write it to a file. Our endpoints run FLUX.1 [schnell] ($0.03, 1024×1024) and FLUX.2 [klein] 4B ($0.06, custom size and seed); payment happens inside the request with x402, so there's no API key. If you don't want a crypto wallet, the same image API is on RapidAPI with card plans.

Try it free Image Generation HD API — sharper images, any size, readable text · $0.06 / call

One function for both models

pay_post stands for your x402 v2 client's POST: it receives the 402, pays in USDC on Base and repeats the request with the PAYMENT-SIGNATURE header. Plain requests.post gets you as far as the 402.

import base64

BASE = "https://iamai.markets/api/x402/services/"
SCHNELL = BASE + "ij0SpjTJvY-K"   # $0.03, always 1024x1024
KLEIN = BASE + "pBOfMSmBCVNq"     # $0.06, size + seed

def flux(prompt, out, width=None, height=None, seed=None, steps=6):
    if len(prompt) > 2000:
        raise ValueError("prompt must be 2,000 characters or less")
    if width or height or seed is not None:
        url, body = KLEIN, {"prompt": prompt, "width": width or 1024,
                            "height": height or 1024}
        if seed is not None:
            body["seed"] = seed
    else:
        url, body = SCHNELL, {"prompt": prompt, "steps": steps}

    r = pay_post(url, json=body, timeout=120)
    r.raise_for_status()
    res = r.json()["result"]
    with open(out, "wb") as f:
        f.write(base64.b64decode(res["image_base64"]))
    return res["width"], res["height"], res["model"]

flux("isometric tiny coffee shop, soft pastel colours", "draft.jpg")
flux("isometric tiny coffee shop, soft pastel colours", "banner.jpg",
     width=1536, height=640, seed=7)

Seeds: getting the same picture twice

On the HD endpoint, the same prompt, size and seed give you the same image again. Keep the seed of a result you like, then change one word in the prompt to make a close variation.

The standard endpoint has no seed, so every call is a fresh roll. Save the files you like; you can't regenerate them exactly.

Sizes on HD

Errors worth handling

No wallet? Use the card route

The AI Image Generator is also on RapidAPI (https://rapidapi.com/VPNKorp/api/ai-image-generator32) with a free plan of 10 images a month and paid plans from $9.99 for 500. RapidAPI shows ready-made Python code with your key filled in; the image still comes back as base64.

The product

Questions

Can I pass a negative prompt?

No, there's no negative prompt field. Describe what you want instead, for example "plain white background" rather than "no clutter".

Is this the official Black Forest Labs API?

No. We run the FLUX models on Cloudflare Workers AI and sell access per call.

Can I make several images in one call?

No, one image per call. Loop over your prompts; each call is charged separately.

Related

Updated 2026-12-01