How to Test an AI API Key With curl and Python

Here is how to test ai api key access without building anything: three calls, about two minutes, and you will know if the key is valid, funded and allowed to generate text.
Key points
- Call GET /v1/models first: it proves the key is accepted without spending credit.
- Send one short chat request to model uncensored to confirm generation works.
- Read the X-Balance-USD header or GET /v1/balance to see what is left.
- 401 points at the key, 402 at the balance, 429 at your request rate.
The three-call test
To test an AI API key properly you need to prove three separate things: the server accepts the key, the key has credit behind it, and a real completion comes back. One call per question keeps the diagnosis simple. If something fails you know which layer is at fault.
- Models list. Authenticates the key and costs nothing.
- Chat completion. Confirms the model answers and that your request format is right.
- Balance. Confirms what is left after the test.
Everything below uses the base URL https://api.aiapisource.com/v1 and the model ID uncensored. Put your key in an environment variable so it never ends up in your shell history or a repository.
export AIAPI_KEY="paste-your-key-here"
If you do not have a key yet, the API key page shows how to create one. New accounts can claim a $0.50 trial credit there with one click, which is more than enough for every test in this article.
Step 1: list models with curl
The cheapest check is a read-only request. GET /v1/models returns the model list and requires a valid key, so a successful answer proves authentication.
curl -s https://api.aiapisource.com/v1/models \
-H "Authorization: Bearer $AIAPI_KEY"
A healthy response is JSON with a data array that includes uncensored. If you see an HTML page, a connection error or an empty reply, the problem is the URL, not the key. Check that you kept the /v1 suffix and that there is no trailing space after the key in your variable.
Add -i to the curl command to see response headers. The API sends X-Request-Id on every call. Keep that value if you ever contact support, because it identifies the exact request.
Step 2: send one chat request
Now check generation. Keep the prompt tiny and cap the output so the test costs a fraction of a cent.
curl -s https://api.aiapisource.com/v1/chat/completions \
-H "Authorization: Bearer $AIAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "Reply with the single word: ready"}],
"max_tokens": 10
}'
Look for choices[0].message.content and a usage object with prompt_tokens and completion_tokens. A request of roughly 20 input tokens and 5 output tokens costs 20 / 1,000,000 x $0.15 plus 5 / 1,000,000 x $0.50, which is about $0.0000055. You could run a hundred of these for well under a cent.
The same endpoint also works for streaming: add "stream": true and tokens arrive as server-sent events. Test that separately once the basic call works.
Step 3: the same test in Python
If your real code uses an OpenAI SDK, test through the SDK too. It catches problems curl will not, such as a base URL passed to the wrong argument.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["AIAPI_KEY"],
base_url="https://api.aiapisource.com/v1",
)
print([m.id for m in client.models.list().data])
r = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Reply with the single word: ready"}],
max_tokens=10,
)
print(r.choices[0].message.content)
print(r.usage)
The first print should show a list containing uncensored. The second shows the model reply. For a longer walkthrough of the SDK setup, see the OpenAI API Python guide on this site.
Run the Python version from the same shell where you exported the key. If it raises a KeyError on AIAPI_KEY, the variable is not set in that session, which is the single most common reason a test fails before it ever reaches the network.
Node works the same way: pass baseURL: "https://api.aiapisource.com/v1" and the key to the OpenAI client constructor.
Check the balance
A key can be valid and still have nothing to spend. Two ways to read the balance:
curl -s https://api.aiapisource.com/v1/balance \
-H "Authorization: Bearer $AIAPI_KEY"
# OpenAI-style alternative
curl -s https://api.aiapisource.com/v1/dashboard/billing/credit_grants \
-H "Authorization: Bearer $AIAPI_KEY"
Every response also carries an X-Balance-USD header, so the chat call from step 2 already told you the remaining credit if you ran it with -i. In Python, the OpenAI SDK exposes this through client.chat.completions.with_raw_response.create(...), where you read response.headers["X-Balance-USD"].
Failed or refused requests are not charged, so a test that errors out does not eat credit. Prepaid credit does not expire, which means an unused balance will still be there next week.
Reading the error codes
When a test fails, the HTTP status narrows the cause quickly.
| Status | Usual meaning | What to do |
|---|---|---|
| 401 | The key is missing, mistyped or revoked | Check the Authorization: Bearer header, copy the key again, look for hidden spaces or quotes |
| 402 | No credit left to pay for the request | Read the balance, then top up on the pricing page |
| 404 | Wrong path or wrong model ID | Use /v1/chat/completions and model uncensored |
| 429 | Rate or concurrency limit, or short overload | Wait and retry; see below |
A 429 carries an error code. rate_limited means too many requests per minute and concurrency means too many at once. A full key allows 300 requests per minute and 8 concurrent requests; a trial key allows 60 per minute and 2 at once. On temporary overload the API answers 429 with Retry-After: 5, so wait five seconds and send the request again. A streamed request holds its concurrency slot until the stream finishes, which is a common reason a trial key hits the limit during parallel tests.
The status codes 401, 402 and 404 follow normal API conventions, so treat the table as a first guess and read the JSON error body for the exact message.
A reusable smoke-test script
Once the manual test passes, keep a script you can run after rotating keys or changing environments.
import os, sys
import requests
BASE = "https://api.aiapisource.com/v1"
H = {"Authorization": "Bearer " + os.environ["AIAPI_KEY"]}
m = requests.get(BASE + "/models", headers=H, timeout=15)
print("models", m.status_code)
c = requests.post(
BASE + "/chat/completions",
headers=H,
json={"model": "uncensored",
"messages": [{"role": "user", "content": "ping"}],
"max_tokens": 5},
timeout=60,
)
print("chat", c.status_code, "balance", c.headers.get("X-Balance-USD"))
sys.exit(0 if m.ok and c.ok else 1)
The exit code makes it easy to drop into a deploy check. For parameters beyond this minimum, the API docs list everything the endpoint accepts.
More guides
Questions and answers
Does testing an API key cost money?
The models list is free to call. A short chat request with a low max_tokens costs a few millionths of a dollar at $0.15 per 1M input tokens and $0.50 per 1M output tokens. Failed requests are not charged.
How do I test a key without a card?
Claim the $0.50 trial credit on the API key page after signing up. It needs no card and is valid for 7 days, one per person.
Why does a valid key return 429 right away?
You probably hit a concurrency or per-minute limit. Trial keys allow 2 concurrent requests and 60 per minute; full keys allow 8 and 300. Check the error code, then slow down or retry after a few seconds.
Can I test the key in Postman or an app instead of curl?
Yes. Any OpenAI-compatible client works: set the base URL to https://api.aiapisource.com/v1, paste the key as a Bearer token and use model uncensored.
Your key is one form away
Create an account, copy the key, change the base URL. That is the whole setup.