HiAPI
  • Models
  • Pricing
Search

Search HiAPI models, tools, and resources.

  • Models
  • Pricing
HiAPI

One API, All AI Models

Generate images, video, and audio with leading models through one production-ready API.

Get a free API key

AI Image API

  • All image models
  • GPT Image 2
  • Nano Banana 2
  • Seedream 5.0 Pro
  • Qwen Image 2.0 Pro
  • FLUX 1.1 Pro

AI Video API

  • All video models
  • Seedance 2.5
  • FLUX.3 Video
  • Seedance 2.0
  • Veo 3.1
  • Kling 3.0

AI Audio API

  • All audio models
  • MiniMax Music 2.6
  • MiniMax Music 1.5
  • ElevenLabs v3
  • Text to music
  • Text to speech

Product

  • Model marketplace
  • Playground
  • Pricing
  • Image API Cost Calculator
  • Free GPT Image 2 Generator
  • Free Nano Banana Image Generator
  • Outfit Preview
  • Product Photo Lab

Developers

  • Documentation
  • API Reference
  • Agent Skills
  • LLM integration index
  • Blog

Company

  • About
  • Contact support
  • Terms of Service
  • Privacy Policy

© 2026 hiapi. All rights reserved.

Open source on GitHubPython SDK on PyPI
  • What you'll build
  • The minimal working example
  • Same flow in Python, with polling
  • Parameters that matter
  • Production write-up
  • Related resources
  • FAQ
TutorialJul 30, 2026

How to Use flux-2-klein-9b/text-to-image via the hiapi API

hiapiapi-guideimage-generationflux

Latest models

Explore models

Contents
  • What you'll build
  • The minimal working example
  • Same flow in Python, with polling
  • Parameters that matter
  • Production write-up
  • Related resources
  • FAQ

Generate it with HiAPI

Choose a model, enter your prompt, and see the result.

HiAPI Blog

Related articles

HiAPI

Generate it with HiAPI


slug: flux-2-klein-9b-text-to-image-api-guide locale: en title: How to Use flux-2-klein-9b/text-to-image via the hiapi API category: tutorial summary:

  • Minimal curl and Python examples for flux-2-klein-9b/text-to-image over hiapi's unified /v1/tasks endpoint.
  • 'Full parameter table: prompt, aspect_ratio, num_inference_steps, output_format, and seed — the strict schema rejects unknown fields.'
  • 'Production notes: callback.url vs polling, the Idempotency-Key header, and the two error envelope shapes.' takeaways:
  • Create with POST /v1/tasks, then poll GET /v1/tasks/{taskId} or use callback.url for the result.
  • num_inference_steps only accepts 4-8 (integer); there's no size or n field for this model.
  • Same Idempotency-Key header returns the same taskId on retry — safe to reuse on timeouts.
  • 400 errors use error_code, 401 auth errors use a nested error.code — check HTTP status first. meta_title: flux-2-klein-9b Text-to-Image API Guide meta_description: Call flux-2-klein-9b/text-to-image with curl and Python; create a task, poll for the image, and handle callbacks, idempotency, and errors in production. keywords:
  • flux-2-klein-9b text-to-image api
  • flux-2-klein-9b api example
  • hiapi image generation api
  • flux klein api curl python tags:
  • api-guide
  • image-generation
  • flux author_name: hiapi is_published: false cover_image: https://static.hiapi.ai/blog/_default/recipe-cover.jpg

How to Use flux-2-klein-9b/text-to-image via the hiapi API

What you'll build

A script that sends a prompt to flux-2-klein-9b/text-to-image, waits for the image, and downloads it — plus the production details (callbacks, idempotency, error shapes) you need before this runs unattended.

Prerequisite: grab an API key from the hiapi dashboard. Every request below authenticates with Authorization: Bearer sk-<your-api-key>.

The minimal working example

hiapi generation models run behind one unified async endpoint: POST /v1/tasks creates a job and hands back a taskId immediately; you then poll GET /v1/tasks/{taskId} until it reaches a terminal state.

curl -X POST https://api.hiapi.ai/v1/tasks \
  -H "Authorization: Bearer sk-<your-api-key>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "flux-2-klein-9b/text-to-image",
    "input": {
      "prompt": "a red fox walking through fresh snow, golden hour light",
      "aspect_ratio": "16:9"
    }
  }'

That returns:

{"code": 200, "data": {"taskId": "tk-hiapi-01K..."}, "message": "success"}

Poll the same task until data.status flips from handling to success (it briefly passes through archiving in between):

curl https://api.hiapi.ai/v1/tasks/tk-hiapi-01K... \
  -H "Authorization: Bearer sk-<your-api-key>"
{
  "code": 200,
  "data": {
    "status": "success",
    "output": [
      {"type": "image", "url": "https://temp.hiapi.ai/.../01K...-0.png", "expireAt": 1785941465}
    ]
  },
  "message": "success"
}

data.output[0].url is the finished image. It's a temporary link — in testing it expired about 7 days after generation — so download the bytes and store them yourself rather than linking to it directly.

Same flow in Python, with polling

import time
import requests

API_KEY = "sk-<your-api-key>"
BASE = "https://api.hiapi.ai/v1"
headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}

resp = requests.post(f"{BASE}/tasks", headers=headers, json={
    "model": "flux-2-klein-9b/text-to-image",
    "input": {
        "prompt": "a red fox walking through fresh snow, golden hour light",
        "aspect_ratio": "16:9",
    },
})
resp.raise_for_status()
task_id = resp.json()["data"]["taskId"]

while True:
    task = requests.get(f"{BASE}/tasks/{task_id}", headers=headers).json()["data"]
    if task["status"] == "success":
        image_url = task["output"][0]["url"]
        break
    if task["status"] == "failed":
        raise RuntimeError(f"generation failed: {task}")
    time.sleep(3)

image_bytes = requests.get(image_url).content
open("fox.png", "wb").write(image_bytes)

Parameters that matter

flux-2-klein-9b/text-to-image has a strict input schema — sending a field it doesn't recognize (size, n, negative_prompt, whatever another hiapi image model accepts) gets rejected outright, so don't copy params across models without checking the model page first.

FieldRequiredNotes
promptYesString describing the image.
aspect_ratioNoOne of 1:1, 4:3, 3:4, 16:9, 9:16. Defaults to 4:3 if omitted.
num_inference_stepsNoInteger, 4–8. Defaults to 4; higher values trade speed for a bit more detail.
output_formatNoOne of jpeg, png, webp. Defaults to png.
seedNoInteger. Reuse a seed to make results reproducible across calls.

Full parameter reference lives on the model page; current per-image pricing is on the pricing page — check there rather than hardcoding a number, since usage-based rates change.

Production write-up

Callbacks over polling. For anything beyond a quick script, skip the poll loop and add callback.url (plus "when": "final") to the task-creation body — hiapi POSTs the result to your endpoint once the task finishes, exactly the same payload shape you'd get from GET /v1/tasks/{taskId}. See Create Task for the callback field and signature-verification details.

{
  "model": "flux-2-klein-9b/text-to-image",
  "callback": {"url": "https://yourapp.com/hooks/hiapi", "when": "final"},
  "input": {"prompt": "a red fox walking through fresh snow, golden hour light"}
}

Idempotency. Set an Idempotency-Key header (up to 255 bytes) on your POST /v1/tasks call. Retrying the same request with the same key under the same account won't create a second task — you get the original taskId back. That's what makes it safe to retry a request that timed out on your side without double-billing.

Error handling. Two different failure shapes show up in practice, so check both:

  • A bad input (missing prompt, an out-of-range num_inference_steps, an unsupported field) returns HTTP 400 with {"error_code": "INVALID_REQUEST", "message": "..."}.
  • An invalid or unauthorized API key returns HTTP 401 with a different envelope: {"error": {"code": "permission_denied", "message": "...", "request_id": "..."}}.

Branch on the HTTP status first, then read whichever error field is present — don't assume one envelope shape for every failure.

Related resources

  • Get Task Detail — full polling reference
  • Authentication — API key scopes and header format
  • flux-2-klein-9b/image-to-image — same family, edits an input image instead of generating from scratch
  • flux-2-klein-4b/text-to-image — smaller, faster model in the same klein family

FAQ

Does flux-2-klein-9b/text-to-image support image-to-image? Not directly — that's a separate model, flux-2-klein-9b/image-to-image, with its own input schema.

What's the difference between flux-2-klein-9b and flux-2-klein-4b? Both are text-to-image endpoints in the same FLUX.2 [klein] family; 9b is the larger of the two, aimed at stronger realism, text rendering, and detail. Check the 4b model page for its current pricing and parameters before switching.

Can I request multiple images in one call? No — the schema doesn't accept an n or count field. Send one request per image you need.

How long is the returned image URL valid? The output[0].url is a temporary link with its own expireAt timestamp; treat it as short-lived (roughly a week in testing) and download the bytes right after the task succeeds instead of storing the URL.

Why am I getting a 401 permission_denied error? Your key is missing, invalid, or doesn't have access to this model. Confirm you're sending Authorization: Bearer sk-... with a key from your dashboard, not a placeholder.

Do I need to run my own server to use this API? Only if you want callbacks. Polling GET /v1/tasks/{taskId} from a script, cron job, or serverless function works fine for low-volume use; switch to callback.url once you're generating at scale.

Latest models

View all models
  • GPT Image 2From $0.007/image
  • Nano Banana 2From $0.051/image
  • Seedream 5.0 ProFrom $0.050/image
  • Seedance 2.5From $0.121/s

Explore models

TextImageVideoAudio
Back to blog
GPT Image 2From $0.007/image
Nano Banana 2From $0.051/image
Seedream 5.0 ProFrom $0.050/image
Seedance 2.5From $0.121/s
View all models
TextChat and reasoning
ImageGenerate and edit
VideoText and image to video
AudioSpeech and music
Start generating
View model pricing
View all articles
How to Use the flux-3 API for Text-to-Video, Audio, and Continuation

How to Use the flux-3 API for Text-to-Video, Audio, and Continuation

minimax-music-3 API: curl & Python Guide

minimax-music-3 API: curl & Python Guide

How to use grok-imagine-image-2.0/image-to-image via the hiapi API: curl, Python, and a working request

How to use grok-imagine-image-2.0/image-to-image via the hiapi API: curl, Python, and a working request

How to Use grok-imagine-image-2.0/text-to-image via the hiapi API: curl, Python, and a Working Request

How to Use grok-imagine-image-2.0/text-to-image via the hiapi API: curl, Python, and a Working Request

How to Use the qwen-image-3.0 API: curl, Python, and a Working Request

How to Use the qwen-image-3.0 API: curl, Python, and a Working Request

How to Use qwen-image-3.0-pro via the hiapi API: curl, Python, and a Working Request

How to Use qwen-image-3.0-pro via the hiapi API: curl, Python, and a Working Request

Start generating