How to Test an AI API Key: OpenAI, Claude, Gemini and Gateways
Copy-paste curl commands to test an AI API key, what each status code means, and how to check latency, billing and refunds before you trust a provider.
On this page
Short answer: the cheapest way to test an AI API key is to list the models it can see. A 200 with a model list means the key is valid; a 401 means it is wrong, revoked, or sent in the wrong header. Then send one tiny chat request to confirm the account can actually spend, and read the usage field to see what it cost.
| Provider | Zero-cost key check | Auth header |
|---|---|---|
| OpenAI | GET https://api.openai.com/v1/models | Authorization: Bearer |
| Anthropic (Claude) | GET https://api.anthropic.com/v1/models | x-api-key + anthropic-version |
| Google Gemini | GET https://generativelanguage.googleapis.com/v1beta/models | ?key= or x-goog-api-key |
| OpenAI-compatible gateway (APIKO) | GET https://api.apiko.app/v1/models | Authorization: Bearer |
All commands below read the key from an environment variable. Export it once (export OPENAI_API_KEY=...) instead of pasting it into each command, so it does not end up in your shell history or a screenshot.
Test an OpenAI API key
curl https://api.openai.com/v1/models \
-H "Authorization: Bearer $OPENAI_API_KEY"
A valid key returns JSON with a data array of model objects. This request does not generate tokens. If you also want to confirm the account has credit, send a one-line chat completion with a model id from that list; a key can be valid and still have no quota.
Test a Claude (Anthropic) API key
Anthropic also has a model list endpoint, which is the zero-cost check:
curl https://api.anthropic.com/v1/models \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01"
To confirm the key can actually run a model, send a minimal Messages request:
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-haiku-4-5",
"max_tokens": 16,
"messages": [{"role": "user", "content": "Say OK."}]
}'
The most common mistake is sending an Anthropic key as Authorization: Bearer to api.anthropic.com. Anthropic's own API wants x-api-key, and the anthropic-version header is required.
Test a Gemini API key
curl "https://generativelanguage.googleapis.com/v1beta/models?key=$GEMINI_API_KEY"
A valid key returns a models array. Putting the key in the URL is convenient but it can land in proxy and server logs; the Gemini API also accepts it in an x-goog-api-key header. Note that Gemini tends to report a bad key as a 400 whose message says the API key is not valid, rather than a 401.
Test a key on an OpenAI-compatible gateway
Gateways that speak the OpenAI format use the same two steps. With APIKO as the example, first list the models your key can call:
curl https://api.apiko.app/v1/models \
-H "Authorization: Bearer $APIKO_API_KEY"
Copy a model id from that response exactly, then send one small streamed chat completion with usage reporting turned on:
curl -N https://api.apiko.app/v1/chat/completions \
-H "Authorization: Bearer $APIKO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6.1-sol",
"messages": [{"role": "user", "content": "Reply with the single word OK."}],
"stream": true,
"stream_options": {"include_usage": true}
}'
With stream_options.include_usage, the final chunk before data: [DONE] carries the real usage object, which you will need for the billing check later. Two details worth knowing:
- Do not squeeze the test with a tiny
max_tokenson reasoning models. Thinking tokens count toward the limit, so a very small cap can return no visible text while still billing output tokens. - The
modelfield in the response is the name the provider reports and can differ from the id you sent (often a dated version). Pricing and usage logs use the id in your request.
The full request and response reference is in the Chat API docs.
What the response means
| Status | What it usually means | What to do |
|---|---|---|
200 | Key is valid; for a chat request, the call ran and was billed | Read usage and keep going |
400 | Malformed request or unsupported parameter (Gemini also uses it for an invalid key) | Read error.message; fix the body. Do not retry unchanged |
401 | Key is wrong, revoked, or sent in the wrong header | Check the full key, that it still exists in your console, the header name, and the base URL. Do not retry |
403 | Key is valid but not allowed. On APIKO, insufficient_user_quota means the balance is too low | Add credit or fix permissions. Do not retry |
404 model_not_found | Model id is wrong or the model was retired (APIKO returns 404; the same message on a 503 means the model exists but is briefly unavailable) | Call GET /v1/models with the same key and copy the id exactly. Retrying will not help |
429 | Rate limit, or on some providers out of quota | Read the body. Back off and retry rate limits; add credit for quota errors |
5xx | Server or upstream trouble | Retry with exponential backoff and jitter; check the provider's status page |
For reference, APIKO's documented rate limit is 500 requests per minute per account, counted across all keys and models. Over the limit it returns 429 with error.type rate_limit_error and no Retry-After header, and a 429 is not billed. The docs recommend retrying 429 and 503 with exponential backoff and random jitter starting at about one second, and never retrying 400, 401 or 403.
Going further: testing a provider before you trust it
A working key answers "can I call this?" If you are choosing a provider or an AI API gateway for production traffic, you also want to know how fast it is, whether the bill matches the work, and what happens when a call fails. Each of these takes a few minutes, and none of them requires trusting a landing page.
Measure latency and time to first token yourself
Do not rely on anyone's published latency numbers, including ours. Run the same small request ten times and look at the median and the worst case:
for i in $(seq 1 10); do
curl -s -o /dev/null -w "request $i: %{time_total}s\n" \
https://api.apiko.app/v1/chat/completions \
-H "Authorization: Bearer $APIKO_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-6.1-sol","messages":[{"role":"user","content":"Reply with OK."}]}'
done
The median tells you what everyday use feels like; the maximum tells you how badly one slow call can stall your pipeline. For chat interfaces, time to first token matters more than total time. curl's %{time_starttransfer} gives the first byte, which can arrive before the first token, so measure the first content chunk directly:
import os, time
from openai import OpenAI
client = OpenAI(base_url="https://api.apiko.app/v1", api_key=os.environ["APIKO_API_KEY"])
start = time.perf_counter()
first = None
stream = client.chat.completions.create(
model="gpt-6.1-sol",
messages=[{"role": "user", "content": "Count from 1 to 20."}],
stream=True,
)
for chunk in stream:
if first is None and chunk.choices and chunk.choices[0].delta.content:
first = time.perf_counter() - start
total = time.perf_counter() - start
print("first token:", first, "total:", round(total, 2))
Swap the base_url and key to run the same script against the provider directly. Comparing direct and gateway numbers from the same machine is the only fair way to see what a gateway adds.
Reconcile one small charge
Send one request with a fixed prompt and compare three numbers:
- The
usageobject in the response:prompt_tokens,completion_tokens, and any cached tokens. - The published per-million-token price on the pricing page.
- The amount actually charged for that request in your console.
The math is prompt_tokens × input price / 1,000,000 + completion_tokens × output price / 1,000,000. For example, at APIKO's 2026-10-04 price for gpt-6.1-sol ($0.20 input, $1.00 output per million tokens), 1,000 prompt tokens and 200 completion tokens come to $0.0002 + $0.0002 = $0.0004. Prices follow upstream costs and are re-synced several times a day, so always take the rate from the live pricing page, not from a blog post.
On APIKO, the charge shows up in Task Logs in the console, with the time, model, request ID, key, duration, cost and status for each call. The billing model is published: an estimate is pre-held when the request starts, the charge settles on the real usage the provider reports, and the hold is refunded automatically if the call fails. If your hand calculation and the logged cost disagree across several tries, that is the signal to dig in, with any provider.
Force a failure and confirm it is not billed
Send a request with a model id you know does not exist, then check your balance and the logs. On APIKO this returns 404 with model_not_found, and nothing should be charged for it. A gateway that bills failed calls quietly costs more than its price list says, especially under load, when transient errors are common.
Know what does not count as a failure: if your own client times out or disconnects, the model call keeps running and is billed. On APIKO, a non-streaming request with no response within 100 seconds gets a 524 from the edge proxy, but it still completes in the background and is charged. Size client timeouts for the slowest model, use "stream": true for long output, and do not resubmit blindly.
Check the public signals
Some things you can check before you even sign up:
- Status page. Is there a public one with timestamped data, and does it ever show partial degradation instead of permanent green? APIKO's is at /status.
- Published billing rules. Are pre-hold, settlement and refund-on-failure written down, or only implied? Are there subscriptions, minimums, or expiring balances?
- Docs completeness. Can you get to a working request without contacting anyone? Look for the base URL, auth header, a status code table, the rate limit, and how to get usage on streamed responses.
Checklist
| Check | How | What a pass looks like |
|---|---|---|
| Key valid | GET /v1/models | 200 and a model list |
| Account can spend | One tiny chat request | 200 with a usage object |
| Errors are legible | Bad key, bad model id | Distinct 401 / 403 / 404 or 503 with a clear message |
| Latency | 10 identical requests, plus first-token timing | Stable median, no extreme outliers |
| Billing | Hand-calculate one request | Logged cost matches usage × published price |
| Failures | Invalid model id | Error returned, nothing charged |
| Public signals | Status page, billing rules, docs | Timestamped status, written billing rules, complete error docs |
None of this is specific to one vendor. Run it against whatever you use today and against any provider you are considering.
Try it on APIKO
If you want to run the gateway half of this checklist on APIKO, sign in with Google using a Gmail address and $0.20 is added to your balance automatically, no card needed, once per person. The trial works with every model, including Claude, image and video, which is enough to run the checks above with a few test requests. Create an account, then follow the quickstart guide if you want to wire it into code.
Frequently asked questions
Does testing an API key cost money?
Listing models (GET /v1/models on OpenAI, Anthropic and OpenAI-compatible gateways, or the Gemini models endpoint) does not generate tokens, so it is not billed. A chat or messages request is billed for the tokens it uses; with a one-line prompt that is a fraction of a cent.
Why does my API key return 401?
The key is wrong, revoked or deleted, or it is sent in the wrong header. OpenAI and OpenAI-compatible gateways expect Authorization: Bearer <key>; Anthropic expects x-api-key. Also check that the base URL belongs to the service that issued the key, and that no space or newline was copied with it.
How do I test an API key without spending tokens?
Call the model list endpoint with the key. A 200 with a list of models proves the key is valid and shows which model ids it can use. It does not prove the account has balance; only a small paid request does that.
Why do I get 429 on a brand-new key?
A 429 means either a rate limit or, on some providers, no remaining quota or credit (OpenAI reports the latter as insufficient_quota). Read the error body to tell which. Retry rate limits with exponential backoff and jitter; add credit for quota errors. On APIKO a 429 is a rate limit and is not billed.
Is it safe to test an API key in an online tool?
We advise against it. Pasting a key into a third-party website gives that site full use of your account and balance. Test from your own terminal with the key in an environment variable, and rotate any key you have already pasted somewhere you do not control.
My key works but every chat request fails. What next?
Check the model id first: call GET /v1/models with the same key and copy an id exactly. Then read error.message in full. On APIKO, 403 insufficient_user_quota means the balance is too low, and 404 model_not_found means the id is wrong or the model was retired.