Generate image/video via Kie.ai REST API — proxy for Grok Imagine models
Path Shortcuts
OUT = D:/ClaudeMediaGen/output-fal # ใช้ร่วมกับ fal output
BASE = https://api.kie.ai/api/v1/jobs
Environment Variables
| Var | Address | How to read |
|---|---|---|
KIE_API_KEY |
Windows User Env | os.environ["KIE_API_KEY"] (set via setx or [Environment]::SetEnvironmentVariable) |
Model Selection
| Model ID | Type | Best For | Output |
|---|---|---|---|
grok-imagine/text-to-image |
text2img | Aesthetic/cinematic images, good with Thai prompts | 3 รูป/request |
grok-imagine/image-to-image |
img2img | Edit/adjust style from a reference image | TBD |
grok-imagine/image-to-video |
img2vid | Create video from a still image | TBD |
grok-imagine/upscale |
upscale | Increase video resolution | TBD |
veo3_fast |
video | Video fast, supports i2v (REFERENCE_2_VIDEO) | video + audio |
veo3_quality |
video | Video highest quality | video + audio |
veo3_lite |
video | Video most economical | video + audio |
Note: text-to-image + veo3_fast tested and actually works / other models are in docs but not yet tested Veo3 uses a different endpoint — see section "Veo3 Video Generation" and "API Endpoints Summary" below
Aspect Ratios
| Value | Use when |
|---|---|
2:3 |
Portrait (default for people images) |
3:2 |
Landscape |
1:1 |
Square |
16:9 |
Wide cinematic |
9:16 |
Story/Reels format |
API Flow — 3 steps
1. Create Task
POST {BASE}/createTask
Authorization: Bearer <KIE_API_KEY>
Content-Type: application/json
Body:
{
"model": "grok-imagine/text-to-image",
"input": {
"prompt": "...",
"aspect_ratio": "2:3"
}
}
Response: { "code": 200, "data": { "taskId": "..." } }
- Check
code == 200first; if not, = error - Keep
taskIdfor polling
2. Poll Task Status
GET {BASE}/recordInfo?taskId=<taskId>
Authorization: Bearer <KIE_API_KEY>
Task States:
| State | Meaning | Action |
|---|---|---|
waiting |
In queue | Keep waiting |
queuing |
In queue | Keep waiting |
generating |
Generating | Keep waiting |
success |
Done | ดึง URL |
fail |
Failed | Read failMsg + failCode |
Poll strategy: every 5 seconds, timeout 5 minutes (60 rounds)
When success:
data.resultJsonis a JSON string → must parse it again- Inside there is
resultUrls: [url1, url2, url3]— gets 3 images per 1 request
3. Download Images
Important: URL must include special headers, otherwise it will be blocked
Headers สำหรับ download:
User-Agent: Mozilla/5.0
Referer: https://kie.ai/
Naming convention:
kie_grok_{taskId_last8}_{index}.jpg
Output directory: D:/ClaudeMediaGen/output-fal/
Prompt Rules
Grok prompt syntax rules have moved to the shared model-card. See grok-image.md for:
- Hybrid prompt strategy (English + Thai)
- Forbidden words
- Subject short / Scene long rule
- Do not specify body size
Implementation Guide
Create a gen script in Python
- Read API key from
os.environ["KIE_API_KEY"](Windows User Env — set once system-wide) - Create task → POST
/createTask→ keeptaskId - Poll → GET
/recordInfo?taskId=→ loop every 5s until gettingsuccessorfail - Parse result →
data.resultJsonas a string →json.loads()→resultUrls - Download → put in
User-Agent+Refererheaders → save as.jpg - Create a paired .txt → save the prompt text alongside the image every time (for traceability)
Batch Generation
- Multiple requests can be sent simultaneously (no rate limit encountered in testing)
- Each request yields 3 images → 10 requests = 30 images
- Use
run_in_background: truefor parallel gen in Claude Code
Error Handling
code != 200from createTask → show the full error responsestate == "fail"→ readdata.failMsg+data.failCode- Network error while polling → just retry, no need to re-create the task
- Download fail → retry with headers
Cost
- No cost per request data from Kie.ai yet — always use preview before batch
Rules
- Preview before batch — gen 1 request first (yields 3 images), check quality before generating more
- Do not Read generated images — wastes a lot of context, just report the file path
- .txt always paired — save the prompt text alongside the image every time
- Do not Read the output file of a background task — issue the gen command and be done
File Upload API (tested)
Use to upload files (e.g. images for image-to-video) before submitting gen
POST https://kieai.redpandaai.co/api/file-stream-upload
Authorization: Bearer <KIE_API_KEY>
Content-Type: multipart/form-data
Parameters:
file: (binary)
uploadPath: (string)
fileName: (string)
Response:
{
"success": true,
"data": {
"downloadUrl": "https://tempfile.redpandaai.co/..."
}
}
Note:
- Use curl multipart only (base64 upload returns 403)
- Files are automatically deleted after 3 days
- Must upload before using image-to-video (Veo3 REFERENCE_2_VIDEO)
Veo3 Video Generation (tested)
Veo3 uses a different endpoint from the Market API (Grok Imagine) — both generate and poll
Create Task
POST https://api.kie.ai/api/v1/veo/generate
Authorization: Bearer <KIE_API_KEY>
Content-Type: application/json
Body:
{
"prompt": "...",
"imageUrls": ["https://uploaded-url"],
"model": "veo3_fast",
"generationType": "REFERENCE_2_VIDEO",
"aspect_ratio": "9:16"
}
| Parameter | ค่า | Note |
|---|---|---|
model |
veo3_fast, veo3_quality, veo3_lite |
fast supports i2v |
generationType |
TEXT_2_VIDEO, REFERENCE_2_VIDEO |
REFERENCE_2_VIDEO works only with veo3_fast |
aspect_ratio |
16:9, 9:16, Auto |
— |
imageUrls |
array of URLs | Must upload first via file-stream-upload |
Optional: enableTranslation, enableFallback, watermark, callBackUrl, seeds
Poll Status
GET https://api.kie.ai/api/v1/veo/record-info?taskId=<taskId>
Authorization: Bearer <KIE_API_KEY>
| Field | ค่า | Meaning |
|---|---|---|
successFlag |
0 |
Generating |
successFlag |
1 |
Success |
successFlag |
2 |
Failed |
successFlag |
3 |
Failed |
- Video URL is in
data.response.resultUrls[] - มี audio (
hasAudioList: [true]) - Takes approximately 60-120 seconds
Veo3 Flow: Upload → Generate → Poll
1. Upload image → POST file-stream-upload → ได้ downloadUrl
2. Generate → POST /veo/generate (ใส่ downloadUrl ใน imageUrls) → ได้ taskId
3. Poll → GET /veo/record-info?taskId= → วน loop จน successFlag != 0
4. Download → ดึง video จาก resultUrls[]
API Endpoints Summary
| Feature | Market API (Grok Imagine) | Veo3 API |
|---|---|---|
| Create task | POST /api/v1/jobs/createTask |
POST /api/v1/veo/generate |
| Poll status | GET /api/v1/jobs/recordInfo |
GET /api/v1/veo/record-info |
| File upload | POST kieai.redpandaai.co/api/file-stream-upload |
(same) |
| Base URL | https://api.kie.ai |
https://api.kie.ai |
Related Skills
/art-engine- upstream: model selection and prompt syntax translation/fal-ai- alternative execution path for Grok (same model, different API - fal.ai direct vs Kie.ai proxy)/art-director- upstream: creative brief crafting/sira-image-prefer- taste DNA for prompt preferences