# Giky API Python Quickstart

A complete standard-library example for Python 3. Set AI_API_KEY in the environment and run from your backend.

## Run the example

This creates one paid image task. The loop waits up to about ten minutes; a timeout does not cancel a task. Persist IDs outside the process in a production application.

```
import json, os, time, uuid
from urllib.request import Request, urlopen

base = os.environ.get("AI_API_BASE_URL", "https://gikyapi.com").rstrip("/")
key = os.environ["AI_API_KEY"]
request_id = str(uuid.uuid4())
payload = {"model":"seedream-5-flash","operation":"text-to-image","input":{"prompt":"A blue ceramic cup beside an orange, soft morning light","resolution":"2K","aspect_ratio":"1:1"}}
headers = {"User-Agent": "AI-API-Python/1.0", "Authorization": "Bearer " + key, "Content-Type": "application/json",
           "Idempotency-Key": request_id}
# Persist request_id + payload before sending. On an uncertain submit response,
# resend that exact pair; do not generate a different request_id.
print("Request ID:", request_id)
req = Request(base + "/v1/tasks", data=json.dumps(payload).encode(), headers=headers)
with urlopen(req, timeout=35) as response:
    task = json.load(response)
print("Task ID:", task["id"])
for _ in range(120):
    time.sleep(5)
    req = Request(base + "/v1/tasks/" + task["id"], headers={"User-Agent": "AI-API-Python/1.0", "Authorization": "Bearer " + key})
    with urlopen(req, timeout=30) as response:
        task = json.load(response)
    if task["status"] == "succeeded":
        print(json.dumps(task["outputs"]))
        break
    if task["status"] in ("failed", "needs_review"):
        raise RuntimeError("Inspect task " + task["id"] + ": " + task["status"])
else:
    print("Still pending. Keep this task ID and query later:", task["id"])
```

## Handle network errors

The example deliberately stops on HTTP/network errors. Retry a temporary GET with backoff. For an uncertain POST, reuse the saved request_id and identical payload. Never wrap the whole script in an automatic retry that creates a new UUID.
