DEV Community

deAPI
deAPI

Posted on

How to Generate Images with Flux API in Python

 Flux, built by Black Forest Labs, has become the go-to open-source model for image generation. This tutorial walks you through generating your first image with the Flux API using Python — from API key to saved file in under 5 minutes.

We'll use deAPI, which runs Flux models on decentralized GPUs and exposes an OpenAI-compatible endpoint. If you've ever used the OpenAI SDK, you already know 90% of what you need.

Prerequisites

  • Python 3.8+
  • The OpenAI Python package and requests (pip install openai requests)
  • A deAPI account (sign up at app.deapi.ai/dashboard — $5 free credits, no card required)

Step 1: Get your API key

Head to app.deapi.ai/dashboard and create an account. Your API key lives under Settings > API Keys. It starts with dpn-sk- — copy it and keep it safe.

Step 2: Generate your first image

Because deAPI is OpenAI-compatible, you use the standard openai Python package. The only thing that really changes is base_url — point it at deAPI and pass your deAPI key.

from openai import OpenAI

client = OpenAI(
    api_key="dpn-sk-your-key-here",
    base_url="https://oai.deapi.ai/v1"
)

response = client.images.generate(
    model="Flux1schnell",
    prompt="A neon-lit Tokyo alley at midnight, rain reflections on asphalt, cinematic photography",
    size="1024x1024",
    n=1
)

print(response.data[0].url)
Enter fullscreen mode Exit fullscreen mode

That's it. Run the script, and you'll get back a URL pointing to your generated image.

Step 3: Save the image to disk

Most use cases need the actual file, not just a URL. Here's a complete script that generates and saves:

import requests
from openai import OpenAI

client = OpenAI(
    api_key="dpn-sk-your-key-here",
    base_url="https://oai.deapi.ai/v1"
)

response = client.images.generate(
    model="Flux1schnell",
    prompt="A neon-lit Tokyo alley at midnight, rain reflections on asphalt, cinematic photography",
    size="1024x1024",
    n=1
)

image_url = response.data[0].url
image_response = requests.get(image_url)
image_response.raise_for_status()

with open("generated_image.png", "wb") as f:
    f.write(image_response.content)

print(f"Image saved! URL: {image_url}")
Enter fullscreen mode Exit fullscreen mode

Tweaking your generations

Resolution. For FLUX.1 Schnell the size parameter accepts values from 256×256 up to 2048×2048 in 128px increments. Smaller sizes are faster and cheaper — start with 768×768 for quick iterations.

Prompting tips. Flux responds well to specific visual language. Instead of "a beautiful sunset," try "golden hour over Santorini, whitewashed buildings, warm film grain, shot on 35mm." Lighting descriptions and camera terms give the model something concrete to work with.

Batch generation. Set n=2 or n=4 to generate multiple variations in a single call. Useful for exploring prompt directions before committing to a final version.

Flux models on deAPI

deAPI currently offers two Flux variants:

Model Slug Max resolution Best for
FLUX.1 Schnell 12B NF4 Flux1schnell 2048×2048 Fast prototyping, iterations
FLUX.2 Klein 4B BF16 Flux_2_Klein_4B_BF16 1536×1536 Higher quality, also supports img2img

Switch between them by changing the model parameter. Everything else stays identical.

Next steps

Want to test prompts without writing code? The playground lets you experiment directly in the browser.

The full API reference lives at docs.deapi.ai.

This same OpenAI-compatible pattern works for text-to-speech, transcription, and video generation too — swap the model slug and endpoint, keep everything else.


Built with deAPI — open-source AI models, decentralized GPUs, $5 free credits to start.

Top comments (2)

Collapse
 
pietrus914 profile image
Piotr

Great article! OpenAI API compatibility is a gamechanger these days! In a flash, the app switches to a cheaper solution for the AI ​​media generation.

Collapse
 
deapi profile image
deAPI

Thanks, Piotr! We also consider OpenAI API compatibility to be a key feature today. The same code allows you to use hundreds of open source models and easily switch between them.