What Is an API Key? 5 Things to Know Before Your First AI API Call
An API key is a string generated by a service provider. Your program sends it along with each AI API request, and the provider uses it to know who is making the request and to record the usage under that person's account. It is both an identity credential and a billing credential, so it is not something you can paste around like an ordinary string.
Below are five things beginners ask about most, in the order they usually come up, followed by a minimal example you can run as-is and a storage checklist.
1. What does an API key actually do?
Think of it as a keycard with a spending limit. It does two things at once:
- Identity: the provider uses it to tell which account and which project a request comes from.
- Billing and limits: for pay-as-you-go APIs, usage is charged to the account that owns the key. Providers also usually apply rate limits per key or per project; check each provider's official docs for the exact rules.
So anyone who gets your key can send requests against your quota without needing your password.
2. How is it different from an account password?
| Account password | API key | |
|---|---|---|
| Where it is used | Logging in to a website or app, operated by a person | Written into a program or tool config, used by code |
| What it can do | Manage the account, view bills, chat | Usually only call the API; the permission scope depends on the provider |
| Can you have several? | Generally one set per account | Usually you can create several, split by purpose, and delete any of them at any time |
| If it leaks | You have to change your password | Delete or rotate that one key; nothing else is affected |
Also note that a web subscription and the API are often billed separately, and subscription quota cannot be used directly for the API. For more on this, see ChatGPT Plus vs. the OpenAI API: what's the difference.
3. What does "a key starting with sk" mean?
Many providers issue keys that start with sk-. This is just a common naming habit that makes keys easier for people and scanning tools to recognize; it is not a universal rule. Some providers use different prefixes, and some have no fixed prefix. The test is not how the key starts, but whether it is a string you generated from the provider's console or key page, and which is often shown in full only once, at creation. Check the relevant official docs for the exact format.
4. Where should an API key live, and who can see it?
There is only one rule for where to put it: never anywhere other people can see it.
- Do not put it in front-end web code or a mobile app. Anyone who opens the developer tools may be able to see it.
- Do not commit it to a Git repository, including a private one. Once you add collaborators or the repo goes public, it can leak.
- Do not post it in chat groups, screenshots, or support tickets. When you need help, replace the key with a placeholder first.
- For local development, an environment variable or an untracked
.envfile works.
For how teams should distribute and rotate keys, see How to store an API key securely. If you use OpenAI, creating keys per project and setting permissions and limits is a good habit; see How to use OpenAI project API keys.
Who can see your key: the provider can (it has to validate it), you yourself can usually copy it only once after creation or see only part of it, and anyone who gets the plaintext can use it.
5. How do you use it once? A minimal curl example
First put the key in an environment variable instead of typing it directly into the command:
export API_KEY="your-key"
Then send a minimal chat request. The following is an illustration in the OpenAI-compatible format. The endpoint URL and model name are placeholders; replace them with the values from your provider's docs:
curl "$BASE_URL/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "model-name",
"messages": [{"role": "user", "content": "Hello, introduce yourself in one sentence"}]
}'
A few points:
- The
Authorization: Bearer ...line is where the key does its job. Some providers use a different header (for example, Anthropic's official API usesx-api-key); follow the official docs. - A 401 usually means the key is wrong or has been deleted. A 429 usually means you hit a rate limit or a quota problem. The exact meaning of error codes is defined in the official docs.
- To first check whether the service itself is reachable, see the probe results for each provider's official endpoints on cdnping's API status page. You can also read AI API latency from mainland China to learn the common causes of slow connections.
If you are not yet clear on how an "AI API" differs from the web version, start with What is an AI API: a one-page explainer for beginners. If you want to take a Claude key and run your first request right away, see How to use the Claude API.
Storage checklist
Once you have a key, go through this list:
- It is stored only in an environment variable or an untracked config file.
.gitignoreexcludes.env-style files.- Each purpose has its own key, with a name that shows what it is for.
- You have set a usage cap or budget alert in the provider's dashboard (if supported).
- If you ever pasted it somewhere by accident, delete it and create a new one right away. Do not count on luck.
No overseas credit card, but want to use Claude Code or Codex?
Many beginners get stuck at payment: official APIs usually require a payment method on file, and some need an overseas credit card. AGI Plan is a monthly-subscription gateway that aggregates large models from multiple providers. You pay in RMB and do not need an overseas credit card. In tools such as Claude Code, Codex, and OpenCode, you only change one endpoint URL and then enter the API key it generates. Setup takes three steps: sign up with an emailed verification code, pick a plan and create a key, and change one setting.
To be clear: AGI Plan is a commercial partner of cdnping; AGI Plan is not an officially authorized channel; some models are marked "overseas only," so check the models page for specifics. Keys from a gateway like this must be stored the same way this article describes. For details, start at https://agiplan.dev/startup.
Next steps
- Still deciding between a subscription and the API: read Subscription or API for beginners.
- Already decided on the API: create a dedicated key in your chosen provider's console, store it using the checklist above, and get one curl request working before connecting it to a tool.