Cookbook
The Cookbook walks through complete tasks, from an empty project to a working result. Every tutorial states its deliverable up front, lists the prerequisites, includes code you can run as is (curl and Python unless noted otherwise), and ends with an approximate cost.
Conventions used everywhere
Section titled “Conventions used everywhere”All tutorials share the setup below, which keeps the code in each one short. Skim it once.
- API key:
MIRAPI_API_KEY, created in https://platform.mirapi.ai → API Keys. Send it asAuthorization: Bearer <key>(OpenAI protocol),x-api-key: <key>(Anthropic protocol), orx-goog-api-key: <key>(Gemini protocol). - Model:
deepseek-chat, the exactidreturned byGET /v1/models. To switch models, change that one string. - Balance: prepaid and non-expiring. When the balance is empty, requests fail with 403; after you top up, the same key works again immediately.
- Streaming: standard SSE, sent as
data:lines and terminated bydata: [DONE]. Reasoning content arrives before the answer.
Base URLs per protocol
Section titled “Base URLs per protocol”| Protocol | Base URL | Auth header |
|---|---|---|
| OpenAI | https://api.mirapi.ai/v1 |
Authorization: Bearer |
| Anthropic | https://api.mirapi.ai |
x-api-key or Authorization: Bearer |
| Gemini | https://api.mirapi.ai or https://api.mirapi.ai/v1beta |
x-goog-api-key or ?key= |
Rule of thumb: if your tool appends its own path after the base URL (such as /chat/completions), use https://api.mirapi.ai/v1.
Error codes at a glance
Section titled “Error codes at a glance”- 401: the key is missing, invalid, or unknown. Fix the credentials.
- 403: the key is valid but was rejected because of insufficient balance, a model that is not whitelisted, a quota limit, or the IP allowlist.
- 413: the request is too large. Reduce its size.
- 429: rate limited. Retry with jittered exponential backoff (1s → 2s → 4s, capped at ~30s). There is no
Retry-Afterheader. - 500: a transient upstream or gateway failure. Idempotent requests are safe to retry.
Every error message ends with a request ID. Include it in any support ticket.
Tutorials
Section titled “Tutorials”| # | Tutorial | You end up with |
|---|---|---|
| 1 | First integration | A one-file Python script that streams a chat completion |
| 2 | Migrate in one prompt | An existing project pointed at MirAPI by Claude Code or Cursor, plus a verification checklist |
| 3 | Tool-calling agent | A complete function-calling loop: define → call → execute → feed back → final answer |
| 4 | Automatic code review | A Claude Code Stop hook that runs a Codex review on every finished message |
| 5 | Claude Code advanced | settings.json, hooks, model switching, and a spend cap via the key’s quota |
| 6 | CI code review | A GitHub Actions job that reviews every pull request and posts the result |
| 7 | Text-to-video | A complete submit → poll → download script with timeout and failure branches |
| 8 | Image-to-video | First-frame control for video generation |
| 9 | Image generation | Generated and edited images, saved to disk |
| 10 | RAG | A single-file retrieval pipeline: embeddings → recall → rerank → cited answer |
| 11 | Batch processing | A rate-limited, resumable async batch translator for a CSV |
Cost notes
Section titled “Cost notes”The cost at the end of each tutorial is approximate; the exact amount depends on the model and on input and output length. For exact figures, use the Platform usage log, plus GET /api/log/token for per-request detail and GET /api/usage/token for account totals. Non-streaming responses report token counts only, never an amount, so reconcile against those endpoints.
Related links
Section titled “Related links”- Quickstart: the five-minute path to your first request.
- Models & pricing: browse the catalog and see how pricing works.
- Configuration: base URLs, auth, parameters, and errors in full.
- FAQ: common questions about keys, billing, and limits.