Skip to main content

Overview

Generate images from text descriptions using models like FLUX and DALL-E.

Minimal Example (Synchronous)

import requests

response = requests.post(
    "https://hub.oxen.ai/api/ai/images/generate",
    headers={
        "Authorization": "Bearer YOUR_API_KEY",
        "Content-Type": "application/json",
    },
    json={
        "model": "black-forest-labs-flux-2-klein-4b",
        "prompt": "A red cube on a white background, minimal",
    },
)

data = response.json()
print("Image URL:", data["images"][0]["url"])

With Base64 Response

Set response_format to "b64_json" to get the image bytes directly instead of a URL:
import requests
import base64

response = requests.post(
    "https://hub.oxen.ai/api/ai/images/generate",
    headers={
        "Authorization": "Bearer YOUR_API_KEY",
        "Content-Type": "application/json",
    },
    json={
        "model": "black-forest-labs-flux-2-klein-4b",
        "prompt": "A blue sphere on grey background",
        "response_format": "b64_json",
    },
)

data = response.json()
image_bytes = base64.b64decode(data["images"][0]["b64_json"])

with open("output.png", "wb") as f:
    f.write(image_bytes)

print("Saved to output.png")
For longer-running image generation jobs, using the async queue avoids long-lived HTTP connections:
import requests
import time

API_KEY = "YOUR_API_KEY"
MODEL = "black-forest-labs-flux-2-klein-4b"
HEADERS = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json",
}

# 1. Enqueue
response = requests.post(
    "https://hub.oxen.ai/api/ai/queue",
    headers=HEADERS,
    json={
        "model": MODEL,
        "prompt": "A watercolor painting of a mountain landscape",
    },
)
generations = response.json()["generations"]
print(f"Enqueued {len(generations)} generation(s)")

# 2. Poll until done
while True:
    status = requests.get(
        "https://hub.oxen.ai/api/ai/queue",
        headers=HEADERS,
        params={"model": MODEL},
    ).json()
    if status["count"] == 0:
        break
    print(f"Still processing: {status['count']} remaining")
    time.sleep(10)

print("Done!")

What’s Next