Python Examples
This page uses only Python's standard library.
Submit, Poll, Download
python
import json
import time
import urllib.request
BASE_URL = "https://api.llmapi.site"
API_KEY = "YOUR_TASK_API_KEY"
def request_json(method, path, payload=None, extra_headers=None):
data = None
headers = {
"Authorization": f"Bearer {API_KEY}",
"Accept": "application/json",
}
if payload is not None:
data = json.dumps(payload).encode("utf-8")
headers["Content-Type"] = "application/json"
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(BASE_URL + path, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=60) as resp:
return json.loads(resp.read().decode("utf-8"))
task = request_json("POST", "/v1/tasks", {
"model": "gpt-image-2",
"modality": "image",
"params": {
"model": "gpt-image-2",
"prompt": "a clean studio product photo of a matte black water bottle",
"n": 1,
"image_size": "2K",
"aspect_ratio": "1:1",
},
"metadata": {"source": "python-example"},
}, {"Idempotency-Key": "python-image-order-001"})
task_id = task["id"]
while True:
detail = request_json("GET", f"/v1/tasks/{task_id}")
if detail["status"] in {"succeeded", "failed", "timeout", "canceled"}:
break
time.sleep(3)
if detail["status"] != "succeeded":
raise RuntimeError(detail.get("error") or detail["status"])
asset_url = detail["assets"][0]["url"]
with urllib.request.urlopen(asset_url, timeout=60) as resp:
image_bytes = resp.read()
with open("result.png", "wb") as f:
f.write(image_bytes)Minimal Video Submission
First use GET /v1/tasks/models to confirm that the target model is available to the current key. Video tasks use the same polling flow shown above:
python
video_task = request_json("POST", "/v1/tasks", {
"model": "seedance-2-0-fast",
"modality": "video",
"params": {
"prompt": "A paper airplane glides across a quiet studio, static camera",
"duration": 5,
"resolution": "720p",
"aspect_ratio": "16:9",
"generate_audio": False,
},
"metadata": {"source": "python-video-example"},
}, {"Idempotency-Key": "python-video-order-001"})
video_task_id = video_task["id"]Reference Image Data URL
python
import base64
def data_url(path, content_type="image/png"):
with open(path, "rb") as f:
encoded = base64.b64encode(f.read()).decode("ascii")
return f"data:{content_type};base64,{encoded}"OpenAI-style Edit Payload
python
payload = {
"model": "gpt-image-2",
"modality": "image",
"params": {
"model": "gpt-image-2",
"prompt": "Replace the background with a premium studio scene.",
"images": [{"image_url": data_url("input.png")}],
"n": 1,
"image_size": "2K",
"aspect_ratio": "1:1",
"extra_body": {
"input_fidelity": "high",
},
},
}Gemini Extension Parameters
python
payload = {
"model": "gemini-3.1-flash-image",
"modality": "image",
"params": {
"model": "gemini-3.1-flash-image",
"prompt": "Create a product image using this object.",
"images": [{"image_url": data_url("input.png")}],
"n": 1,
"image_size": "2K",
"aspect_ratio": "16:9",
"extra_body": {
"generationConfig": {
"temperature": 0.7,
},
},
},
}