AI Income LabJoin AI Income Lab ↗

Build guide · By · Published · Updated

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.

Diagram: check an API request before adding an agent: send a request, inspect the status, assert the result. Wrong key 401, unknown model 404, valid request 200.
Send a local request, inspect its status, and assert the expected result.

1. Know which credential you need

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.

  1. Confirm the gateway address belongs to your installation. Keep local practice on loopback; use HTTPS for a remote service.
  2. Configure a supported provider connection and check that provider’s billing before sending a real request.
  3. Keep the real key in a server-side environment variable. Never place it in browser JavaScript, a public repository, or a screenshot.
  4. Send one short, non-sensitive test prompt using the gateway’s documented request format and token limit.
  5. Check both the response and the provider usage report. A successful response does not prove zero cost.
  6. Only then configure the same endpoint and model in your editor or agent.

When it fails

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

See every tool I recommend →

You may also like

Written by . I’ve shipped software since 2013 and test AI tools on real builds for the AI Automation Station YouTube channel. Each guide shows the date it was last updated and lists the sources it relies on.

Build the next one with AI Income Lab

Step-by-step courses, reusable templates, and a community that answers when you get stuck.

Join AI Income Lab ↗From $29/month. Cancel anytime.
Join 2,900+ members building with AI

Not ready to join? Get the weekly build notes: new guides and tested builds in one short email.