Test your first AI API request locally
Quick answer
Run a local test before calling a real provider: send one request to the example mock server, read the status code, and assert the result. A wrong key should return 401, an unknown model 404 and a valid request 200. The example needs Node.js 22 or later, costs nothing and never reads your API keys.
You have configured a model or gateway. What do you actually run next? Viewers asked this under my 9Router setup video, alongside questions about authentication and paid plans. This follow-up starts with a local example you can run and inspect.
The example uses a local mock server, not a language model. It needs Node.js 22 or later, costs nothing in provider usage, and does not read your API keys. Passing it verifies the example’s request handling, not a connection to 9Router or a paid provider.

1. Know which credential you need
- Chat subscription: access to a provider’s chat product under its current plan.
- Provider API key: a credential for the provider’s developer API, with its own billing and limits.
- Gateway key: access to your router. The router still needs a supported upstream provider connection.
For example, Anthropic documents Claude subscriptions and Console API access as separate products. Paid Claude plans cover the chat apps; API keys come from the Claude Console, where API usage is billed separately, although Max and Team plans include monthly API credits. A free local router does not make the model calls behind it free. If you mainly want coding models at a predictable monthly price rather than per-request billing, compare OpenCode Go plans and limits.
2. Run the local example
Keep the first request small
Download the example and run it. It uses only Node’s built-in modules; no npm install is required for this command.
curl -O https://www.ai-automation-station.com/examples/first-api-request.mjs
node first-api-request.mjs
You can also read the example in your browser first. Its server binds only to 127.0.0.1 on an available port, checks three requests, and closes afterward.
3. Read the result
Wrong key: HTTP 401
Wrong model: HTTP 404
Valid request: HTTP 200
PASS: local request checks. No provider API was called.
These are the expected results from the included executable checks. The first two requests deliberately fail. The third returns a fixed message, connection-ok (local mock). Nothing here measures model quality, fallback reliability, or token savings.
4. Understand the request
The client sends JSON to POST /v1/chat/completions. It supplies a bearer key, a model ID, and a message. The server reads those values and chooses a response:
{
"model": "demo-model",
"messages": [{ "role": "user", "content": "Reply with connection-ok" }],
"stream": false
}
demo-only and demo-model belong only to this mock. They will not authenticate a real service. The example also stops after five seconds and rejects redirects, so it does not silently forward its authorization header elsewhere.
5. Move to your actual gateway
Once the local example makes sense, use your gateway’s own request example. The 9Router project documents a chat-completions endpoint and model discovery. Copy the base URL and exact model or combo ID from the version you installed; do not reuse the mock values.
- Confirm the gateway address belongs to your installation. Keep local practice on loopback; use HTTPS for a remote service.
- Configure a supported provider connection and check that provider’s billing before sending a real request.
- Keep the real key in a server-side environment variable. Never place it in browser JavaScript, a public repository, or a screenshot.
- Send one short, non-sensitive test prompt using the gateway’s documented request format and token limit.
- Check both the response and the provider usage report. A successful response does not prove zero cost.
- Only then configure the same endpoint and model in your editor or agent.
When it fails
- Connection refused: check that the gateway is running and that the host and port match its UI.
- 401 or 403: check which service issued the credential and whether that connection is authorized. A gateway key and a provider key have different jobs.
- 404: check the endpoint path and exact model ID. This mock uses 404 for an unknown model; real services may report a different status.
- 429: inspect the provider’s quota and rate-limit details. Do not assume retries are free.
- HTTP 200 with the wrong format: verify that the service supports the API format your client expects. This example expects a non-streaming response.
Source and next step
The complete example is first-api-request.mjs. For a production example with a saved fallback, continue with the React YouTube-feed guide.
Once a request passes, the Codex tutorial applies the same verify-first habit to code changes, and the Claude Code tutorial for beginners covers setting up a coding assistant. To connect a provider to an agent app, see Hermes Desktop.
FAQ
Does the example call a real AI provider?
No. It uses a local mock server, needs Node.js 22 or later, costs nothing and never reads your API keys. Passing it verifies the example’s request handling, not a connection to 9Router or a paid provider.
What results should I expect?
A wrong key returns HTTP 401, an unknown model returns HTTP 404 and a valid request returns HTTP 200, followed by a PASS line.
Is a chat subscription the same as an API key?
No. A chat subscription covers the provider’s chat product, a provider API key has its own billing and limits, and a gateway key only gives access to your router. Anthropic, for example, bills API usage separately from paid Claude plans.
Where should I keep a real API key?
In a server-side environment variable. Never place it in browser JavaScript, a public repository or a screenshot.
Sources
- Claude Help: Why API usage is billed separately from a paid Claude plan
- 9Router: chat skill documentation (GitHub)
- Example script: first-api-request.mjs
You may also like
Build the next one with AI Income Lab
Step-by-step courses, reusable templates, and a community that answers when you get stuck.
- Courses for builds like this one
- Copy-ready templates
- Ask questions while you build
- Weekly coaching on VIP


