Minimal example
Keep the key in env vars. Prefer flash for cost; pro for harder tasks; VIP ids only after VIP is active.
Prerequisites
- Node.js 18 or newer, which ships a global
fetch. On older runtimes addnode-fetchoraxiosinstead. - An AI24X API key stored in
process.env.AI24X_API_KEY. Never hard-code it into a repository file. - Top-level
await, as used below, requires an ES module (.mjs, or"type": "module"inpackage.json). Inside CommonJS, wrap the same code in anasyncfunction.
Full example with error handling
fetch resolves even when the request fails, so check res.ok before you parse anything:
Choosing a tier
Each request names a model. Use flash for high-volume, forgiving workloads such as tagging, extraction and summarising user input. Step up to pro when a call really needs deeper reasoning or a longer context window. Named VIP models are addressed with a vip- prefix, for example vip-kimi, and only resolve once VIP access is active on the account.
Because the request shape is identical across tiers, you can make the tier an environment variable and change your cost profile without touching the calling code.
Common errors and fixes
- 401 Unauthorized — the key is absent or wrong. Confirm
process.env.AI24X_API_KEYis actually populated in this process; a missing.envload is the usual cause. - 402 / insufficient credits — top up the AI24X credit balance and resend the same request.
- 400 Bad Request — the body was not valid JSON. Always pass the payload through
JSON.stringifyrather than building a string by hand. - 429 Too Many Requests — you are sending faster than your plan allows. Queue the calls or add a small delay between them.
- fetch is not defined — you are on a runtime older than Node 18. Upgrade, or install a fetch polyfill.
Calling the API from a Node process means there is no browser in the path, so CORS settings are irrelevant here. If you see CORS errors, the request is coming from client-side code and the key would be exposed anyway — move the call behind your own server.
Troubleshooting order
- Reproduce the call with curl to separate a key problem from a code problem.
- Log
res.statusandres.text()before you attemptres.json(). - Confirm the key is loaded into the process that makes the call.
- Confirm the payload survives
JSON.stringifyunchanged. - Only then look at timeouts, DNS and any egress proxy in your network.
curl · HTTP / Python · Sign up
Gateway guides: What is an AI gateway · OpenAI-compatible API · Pricing · Start free