API key
In shortAn API key is a secret code that lets a program call an AI service on your account, and the usage is billed to you.
In plain words
An API key is a long string of letters and digits. When a program sends a request to an AI service, it adds the key. The provider reads it, knows which account or project to charge, and answers. The key works like a password and a credit card in one: whoever holds it can use your quota and run up your bill.
An API key is not the same as a chat subscription. A paid chat plan and an API account are billed separately. The page subscription vs API explains the difference, and AI subscriptions lists the plans.
Why it matters
- Money: A leaked key can be used by strangers. Charges land on your account until you stop it.
- Data: Anyone with the key can send requests in your name, which may touch your projects and settings.
- Basic rules from the providers: OpenAI's guidance says to avoid exposing keys in code or public repositories and to store them in a secure place. Google's Gemini docs have a section on security and secret management with the same aim.
What to do:
- Keep the key out of your code, screenshots, chats and public repositories. Use an environment variable or a secret manager.
- Use one key per project, so you can revoke one without breaking the rest.
- Never put a key in a web page or mobile app that users can inspect. Call the AI service from your own server instead.
- Set a spending limit. Anthropic, for example, lets you set your own monthly spend limit below your tier's cap, and once it is reached, requests fail until the limit is raised or the month turns. See how to create an API key with a spending limit.
- If a key leaks, revoke it at once, create a new one and check your usage.
Example
A developer commits a small script to a public code site. The script contains the key as plain text. Within hours, automated scanners find it and use it. The developer sees an unexpected bill. A key read from an environment variable, with a low spending limit, would have limited the damage.
Common misconceptions
- "A private repository is safe enough." Repositories can become public, be copied or be shared. Keep keys out anyway.
- "The provider will refund any misuse." Policies differ. Do not count on it.
- "A key is a login." It has no person behind it, so no second factor protects it. Guard it like a secret.
Questions and answers
What is an API key used for?
It identifies your account to an AI service so that software can send requests and the provider can bill the use to you.
What should I do if my API key leaks?
Revoke it in the provider's console right away, create a new key, update your apps and check recent usage for charges you did not make.
How do I limit what an API key can cost?
Set a spending limit in the provider's billing settings, use one key per project and watch the usage page. See the guide linked above.
Read next
- Rate limit
A rate limit caps how many requests or tokens you may send to an AI API in a set time; going over returns an error, usually HTTP 429.
- Price per million tokens
Price per million tokens is the usual way AI APIs state their rates: what you pay for each million tokens sent in or generated.
- Create an API key and set a spending limit
Create a key, keep it out of your code, cap your monthly spend and know how to switch it off, with steps for OpenAI, Anthropic and Google.
- Subscription or API: what is cheaper for you?
A flat $20 plan or pay per token: one formula and three examples show where each wins, plus the limits and privacy settings that count beyond cost.
- What not to paste into AI chatbots
Passwords, client data, IDs and secrets do not belong in a chatbot: a traffic-light checklist, provider data rules, Swiss and EU basics, and safer options.