DEV Community

diwushennian4955
diwushennian4955

Posted on • Originally published at nexaapi.com

How to Use Flux.1 Schnell API in 5 Lines of Python (Free Tier Available)

Flux.1 Schnell is the fastest open-source text-to-image model from Black Forest Labs — Apache 2.0 licensed, 12B parameters, generates images in 1–4 steps.

Here's how to call it via NexaAPI (cheapest inference API, 56+ models, free tier):

5-Line Python Example

import requests

response = requests.post(
    "https://flux-schnell2.p.rapidapi.com/generate",
    headers={"x-rapidapi-key": "YOUR_KEY", "x-rapidapi-host": "flux-schnell2.p.rapidapi.com"},
    json={"prompt": "A neon-lit Tokyo street at night, cinematic", "num_inference_steps": 4}
)
print(response.json()["image_url"])
Enter fullscreen mode Exit fullscreen mode

That's it. One API call, one image URL back.

Why NexaAPI for Flux.1 Schnell?

Provider Price/Image
NexaAPI $0.001 ← cheapest
FAL.ai $0.003
Replicate ~$0.003

3× cheaper than FAL.ai. Plus you get 55 other models (SDXL, Flux Dev, Flux Pro, vision models, TTS) under the same API key.

Full Working Example

import requests
import base64
from PIL import Image
from io import BytesIO

def generate_image(prompt: str, api_key: str) -> str:
    """Generate an image using Flux.1 Schnell via NexaAPI."""

    headers = {
        "x-rapidapi-key": api_key,
        "x-rapidapi-host": "flux-schnell2.p.rapidapi.com",
        "Content-Type": "application/json"
    }

    payload = {
        "prompt": prompt,
        "num_inference_steps": 4,   # 1-4 steps, faster = lower quality
        "width": 1024,
        "height": 1024,
        "guidance_scale": 0         # Schnell ignores guidance, set to 0
    }

    response = requests.post(
        "https://flux-schnell2.p.rapidapi.com/generate",
        headers=headers,
        json=payload,
        timeout=30
    )
    response.raise_for_status()

    return response.json()["image_url"]

# Usage
api_key = "your_nexaapi_key_here"
image_url = generate_image("A cyberpunk developer at a glowing terminal", api_key)
print(f"Generated: {image_url}")
Enter fullscreen mode Exit fullscreen mode

JavaScript/Node.js Version

async function generateImage(prompt, apiKey) {
  const response = await fetch('https://flux-schnell2.p.rapidapi.com/generate', {
    method: 'POST',
    headers: {
      'x-rapidapi-key': apiKey,
      'x-rapidapi-host': 'flux-schnell2.p.rapidapi.com',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      prompt,
      num_inference_steps: 4,
      width: 1024,
      height: 1024
    })
  });

  const data = await response.json();
  return data.image_url;
}

// Usage
const imageUrl = await generateImage('A futuristic API dashboard UI', 'your_key');
console.log('Generated:', imageUrl);
Enter fullscreen mode Exit fullscreen mode

What About the Other 55+ Models?

Same API key works for:

  • Flux Dev — higher quality, slower ($0.008/image)
  • Flux Pro 1.1 — professional grade ($0.017/image)
  • Flux Kontext Pro — image editing ($0.013/image)
  • Imagen 4, GPT Image 1.5 — Google/OpenAI alternatives
  • Vision models — for image captioning and analysis
  • TTS — ElevenLabs V3, Gemini TTS

One key, one billing dashboard, 56+ models.

Get Started

  1. Sign up at nexaapi.com — free tier available, no credit card required
  2. Grab your API key
  3. Check the full model catalog and pricing
  4. Full docs: nexaapi.com

Pricing: NexaAPI Flux Schnell $0.001/image vs FAL.ai $0.003/image. Data from official provider pages, March 2026.

Top comments (0)