Image Generation with Flux API
Generate images using Flux API in Python with a simple tutorial and examples

Introduction to Flux API
Flux, developed by Black Forest Labs, has become a popular open-source model for image generation. In this post, we'll explore how to use the Flux API in Python to generate images. We'll utilize the deAPI, which runs Flux models on decentralized GPUs and provides an OpenAI-compatible endpoint.
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
Start by creating an account on the deAPI dashboard. Your API key can be found under Settings > API Keys. It begins with dpn-sk- — copy and store it securely.
import os
api_key = 'dpn-sk-YOUR_API_KEY'
os.environ['API_KEY'] = api_keyStep 2: Generate Your First Image
To generate an image, you'll need to send a POST request to the deAPI endpoint with your API key and the image prompt.
import requests
url = 'https://api.deapi.ai/v1/images'
prompt = 'A futuristic cityscape at sunset'
response = requests.post(url, headers={'Authorization': f'Bearer {api_key}'}, json={'prompt': prompt})Example Use Cases
- Generate product images for e-commerce websites
- Create art pieces using text prompts
- Develop interactive stories with dynamically generated images
Remember to check the deAPI documentation for the most up-to-date information on usage and pricing.