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
- Choose the model: for text-to-image use
gpt-image-2-text-to-image; if you have a reference image URL, usegpt-image-2-image-to-image. For the 2.5 series, use the full namegpt-image-2-5-flare-…orgpt-image-2-5-sunburst-…with the same suffix (text-to-imageorimage-to-image). - Build the
inputobject: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). - 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 nocurlin the sandbox; usepython3:
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))
- To save images to the gallery or share with other apps, use
image_save; to check the image, useview_imageto open the downloaded file. - 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.resultJsonin the response is a string; you must calljson.loadson it to getresultUrls. 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
backgroundwith 2K or 4K will be rejected. - Including
input_urlsin 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
codeis not 200, the request was rejected (auth, balance, or parameters); readmsgfirst to adjust parameters. For task failures, inspectfailCodeandfailMsg. - If
image_saveon this device reports "service not found", fall back to copying the file toPictures/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, callGET /api/v1/chat/credit. For new series (e.g.,gpt-image-2-6), parameters should be validated againstGET /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.txtis 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
taskIdand let the user decide whether to wait longer or abandon.