CommunityImagegithub.com

kie-image

Generate or edit images using KIE's GPT Image API: submit tasks, poll for results, and download images to the workspace.

What is kie-image?

This skill enables AI agents to generate or edit images using the KIE (GPT Image) API directly from the command line, without relying on a graphical interface. It supports both text-to-image and image-to-image models with configurable parameters like resolution (1K, 2K, 4K), aspect ratio, and background. The skill handles the full workflow: reading the API key from the workspace, constructing a request payload, submitting the task via a Python script, polling for completion, and downloading resulting image files to `/workspace/out/images/`. It is designed for platforms like Claude Code, Codex, or similar agent-based coding environments that allow script execution in a sandbox. Key features include automatic retry handling, error detection for billing or content rejection, and integration with workspace file management for downstream use.

Works with~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/difyz9/mika-skills/tree/HEAD/skills/kie-image

Installed? Explore more Image skills: steipete/songsee, affaan-m/frontend-design, mattpocock/improve-codebase-architecture · View all 6 →

Ask in your favorite AI

Open a new chat with this agent skill pre-loaded.

Documentation

Entry Point

  • Standalone service at https://api.kie.ai, authenticated via Authorization: Bearer <key> header. Not related to Mika's built-in image models, no need to open the app.
  • Run commands in the sandbox, provided "Settings › Files & Compute › Allow scripts to access the internet" is enabled.
  • The key is read from /workspace/kie_key.txt; if the file is absent, ask the user for the key once, write it to the file, and never display it again or record it in the skill or conversation body.

Steps

  1. Choose the model: for text-to-image use gpt-image-2-text-to-image; if you have a reference image URL, use gpt-image-2-image-to-image. For the 2.5 series, use the full name gpt-image-2-5-flare-… or gpt-image-2-5-sunburst-… with the same suffix (text-to-image or image-to-image).
  2. Build the input object: prompt (≤20,000 characters), resolution (1K / 2K / 4K), aspect_ratio, background (only valid for 1K), input_urls (≤16 public http(s) URLs, only for image-to-image models).
  3. Run the entire workflow in one go using run_command (submission is billed, so confirm the number of images and resolution with the user first). There is no curl in the sandbox; use python3:
import json,os,time,urllib.request as u
M="gpt-image-2-text-to-image";INP={"prompt":"image description","resolution":"1K","aspect_ratio":"1:1"}
k=open("/workspace/kie_key.txt").read().strip()
def c(m,p,b=None):
    r=u.Request("https://api.kie.ai"+p,method=m,data=b and json.dumps(b).encode(),
      headers={"Authorization":"Bearer "+k,"Content-Type":"application/json"})
    d=json.load(u.urlopen(r,timeout=90))
    assert d["code"]==200,(d["code"],d["msg"]);return d["data"]
t=c("POST","/api/v1/jobs/createTask",{"model":M,"input":INP})["taskId"];print(t)
while True:
    s=c("GET","/api/v1/jobs/recordInfo?taskId="+t)
    if s["state"] in("success","fail"):break
    time.sleep(4)
assert s["state"]=="success",(s.get("failCode"),s.get("failMsg"))
for i,x in enumerate(json.loads(s["resultJson"])["resultUrls"]):
    o=f"/workspace/out/images/{t}-{i}.png";os.makedirs(os.path.dirname(o),exist_ok=True)
    open(o,"wb").write(u.urlopen(x).read());print(o,os.path.getsize(o))
  1. To save images to the gallery or share with other apps, use image_save; to check the image, use view_image to open the downloaded file.
  2. The workspace is isolated per conversation: generating images and then saving to gallery or posting should be done either in a single session, or when split, submit the task with the same conversation identifier (task run --conversation) to ensure files from the previous round are visible.

Common Pitfalls

  • The data.resultJson in the response is a string; you must call json.loads on it to get resultUrls. Treating it directly as an object will yield nothing.
  • 2K does not support 5:4, 4:5, 3:1, 1:3, 9:21; 4K does not support 3:1, 1:3, 9:21. For v2.5, 27:16, 16:27, 9:8, 8:9 only work at 1K. Passing background with 2K or 4K will be rejected.
  • Including input_urls in a text-to-image request, or omitting the reference image for an image-to-image request, will cause failure; retrying the same payload won't help.
  • If the response code is not 200, the request was rejected (auth, balance, or parameters); read msg first to adjust parameters. For task failures, inspect failCode and failMsg.
  • If image_save on this device reports "service not found", fall back to copying the file to Pictures/Mika/ and registering it with the media library, then confirm the media library record shows the file is imported. Do not just say "saved".
  • Each image incurs a cost: GPT Image 2 at 1K costs 3 credits (~$0.015), at 2K costs 5 credits; do not submit the same prompt repeatedly just to pick the best one.
  • To check models and pricing, call GET /api/v1/models?q=gpt-image-2; to check balance, call GET /api/v1/chat/credit. For new series (e.g., gpt-image-2-6), parameters should be validated against GET /api/v1/models/<model>/schema.

How to Confirm Success

Polling returns state as success; the resultUrls have been downloaded to files under /workspace/out/images/ and the file size is not just a few hundred bytes (error page); then use view_image to visually confirm the image meets requirements before declaring success.

When to Stop and Hand Over to the User

  • The file kie_key.txt is missing, or the API returns 401/403.
  • The remaining credits are insufficient for the selected resolution and image count.
  • The provider rejects the content with a failCode: report it truthfully, do not rephrase and retry.
  • Generation still in progress after 5 minutes: report the taskId and let the user decide whether to wait longer or abandon.

Related Skills