Integrate the API into your application.
This guide helps you integrate 123API into your application with a production-friendly request flow.
Image and video endpoints commonly use an asynchronous processing model:
import time
import requests
def wait_for_completion(task_id, api_key, max_wait=300):
start_time = time.time()
while time.time() - start_time < max_wait:
response = requests.get(
f"https://123api.co/v1/tasks/{task_id}",
headers={"Authorization": f"Bearer {api_key}"}
)
result = response.json()
if result.get("status") == "completed":
return result
if result.get("status") == "failed":
raise RuntimeError(result.get("error", "task failed"))
time.sleep(2)
raise TimeoutError("task timeout")| Status | Description | Resolution |
|---|---|---|
| 400 | Invalid request parameters | Check request parameters and format |
| 401 | Authentication failed | Verify your API key |
| 402 | Insufficient balance | Recharge or switch to a supported project key |
| 429 | Rate limit exceeded | Reduce request frequency and add retries |
| 500 | Server error | Retry later with exponential backoff |
try:
response = requests.post(
"https://123api.co/v1/chat/completions",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
},
json=payload,
timeout=60
)
response.raise_for_status()
except requests.HTTPError as exc:
if exc.response.status_code == 401:
print("Invalid API key")
elif exc.response.status_code == 402:
print("Insufficient account balance")
else:
print(f"Request failed: {exc.response.text}")If you run into issues during development:
GET /v1/models