Skip to main content

Overview

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

Minimal Example

import requests

response = requests.post(
    "https://hub.oxen.ai/api/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/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")

What’s Next