Documentation IndexFetch the complete documentation index at: /llms.txtUse this file to discover all available pages before exploring further.
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
在您的应用中集成API的开发指南
import time import requests def wait_for_completion(api_key, task_id, max_wait=300): """轮询任务直到完成""" url = f"https://api.tokendazi.com/v1/tasks/{task_id}" headers = {"Authorization": f"Bearer {api_key}"} start_time = time.time() while time.time() - start_time < max_wait: data = requests.get(url, headers=headers).json()["data"] status = data["status"] if status == "completed": return data["result"] elif status in ("failed", "cancelled"): raise Exception(f"任务{status}: {data.get('error')}") time.sleep(2) # 等待 2 秒后再次轮询 raise Exception("任务超时")
import requests response = requests.post( "https://api.tokendazi.com/v1/images/generations", headers={"Authorization": f"Bearer {api_key}"}, json={"model": "gpt-4o-image", "prompt": "一只可爱的熊猫"}, ) if response.status_code != 200: error = response.json().get("error", {}) if response.status_code == 401: print("API密钥无效") elif response.status_code == 402: print("账户余额不足") else: print(f"错误: {error.get('message')}")