What is an API key, and why does a leaked one cost money?
An API key is a secret string that tells a service which account or project a request comes from. The service tracks billing and quota for that project. So anyone who holds your key can send requests as you, and the charges land on you. Treat the key like a password and keep it out of your code.
If you code with an AI agent, you will meet API keys within the first hour. The agent asks for one, a tutorial says "never commit it", and nobody explains why. This page explains what an API key is and why a leaked one turns into a bill.
01What is an API key?
An API (application programming interface) is the door one program uses to ask another for something: a language model for an answer, a cloud provider for a server, a payment service for a charge. An API key is the string you show at that door. API keys are account credentials, like a username and password.
The string does two jobs. It says who is asking, so the provider knows whose account to use. It also says whose bill to charge: standard keys associate a request with a project for billing and quota purposes. The second job is why a stolen key is a money problem, not only a privacy problem.
02How API keys work
Your app sends a request and includes the key. The provider checks it, does the work and records the usage against the project the key belongs to.
your app -- request + key --> provider -- does the work
|
+-- usage counted on the project's bill| What the key does | What it means for you |
|---|---|
| Names the project making the request | Billing and quota are tracked for that project |
| Works like a login | Whoever holds the string can use the account |
| Can be restricted | Restrictions limit how a Google Cloud key can be used, and unrestricted keys are insecure |
03Is it just a password?
Nearly, with three differences. A password lives in your head. A key sits in a file, a shell setting or a config, where programs read it. It is tied to a project's billing and quota, so misuse shows up as usage and charges. And some providers show the secret part once: for AWS, that is the only chance to save the secret access key. Lose it and you create a new one, so save the secret the moment it appears and put it straight into a secrets store, not into a chat or a note.
04What can someone do with your API keys?
With your secret key, an unauthorized party can make unauthorized charges, access customer data or disrupt your integration. For an AI or cloud provider the pattern is the same: the thief sends requests, and your project pays for them.
How fast does that happen? Automated scanners can locate publicly exposed secrets in minutes, and malicious actors can exploit them within hours.
05Why a leaked key turns into a bill
A key cannot tell you from a stranger. It only carries the project's name, so the provider keeps counting usage on that project.
One developer reported an unexpected charge of more than 54,000 euros for Gemini API use, which is Google's AI model service. It came within a few hours after turning on Firebase AI Logic, a Google service for adding AI to an app, in an existing project. A budget alert and a cost anomaly alert both fired, but a few hours late. The charges stopped once the API was disabled and the credentials were replaced. The request for a billing adjustment was ultimately denied.
That is one reported case, not an average. It shows that an alert arrives after the money is spent, while a hard cap stops it. A Google staff member replied on the Google AI developer forum that such a limit exists: at the first usage tier, Gemini API spending is capped at $250 a month and then cut off by default, with a 10 minute delay in reporting.
06Where keys leak from
Most leaks are boring. A key lands in a commit and the repository is public, and bad actors continuously scan public repositories for keys. Or a key sits in a .env file on your Mac, which is a plain text file of settings that programs load at start.
Coding agents add one more route. Commands that run inside Claude Code's sandbox inherit its environment, including any secrets in it. In Claude Code's auto mode, reading .env and sending credentials to the matching API is allowed by default. Malware looks for the same files. A malicious npm package attack called s1ngularity searched for .env files and SSH keys on Linux and macOS. A backdoor planted through fake developer job interviews searches the whole filesystem for credential and secret files on macOS. The infostealer page explains that family of malware, and Claude Code and .env files covers the agent side.
07How to keep the damage small
You cannot make a leak impossible, but you can make it cheap.
- Set spend alerts and a hard spend limit where the provider offers them.
- Restrict the key to what it needs, and keep the key string secure.
- Give keys an expiration date and rotate them on a schedule. Rotating means creating a replacement, switching your apps to it and revoking the old key.
- On OpenAI, give an application a project service account instead of one developer's key.
- Keep keys out of code. Pass them through environment variables or a secrets manager. The where to store guide shows the options on a Mac.
08What to remember about an API key
What an API key is, in one line: a login plus a billing address in one string. If one leaks, replace it first, then look at what it did. The first-hour checklist gives the steps. If the key sits in a public repository, go to the GitHub guide. Provider-specific pages cover an OpenAI key and an AWS access key.
FAQ
How do I get the API key?
You create it in the portal of the provider whose service you want to use, the same place where keys are later managed and revoked. On OpenAI, for example, that is the API keys page of your account. Save the key when it is shown, because AWS, for one, shows the secret part only once. Then keep it in an environment variable or a secrets manager, not in your code.
What exactly does an API key do?
It ties a request to a project for billing and quota. It also works as a login: API keys are account credentials, like a username and password.
What can someone do with your API keys?
With a secret key, an unauthorized party can make unauthorized charges, access customer data or disrupt your integration. What exactly is possible depends on the service and on what the key was allowed to do.
Is an API key just a password?
A key is a password with a bill attached. It is a credential and it names the project that gets charged. Unlike a password, it usually sits in files and settings that programs read.
Does it cost money to use an API key?
The key itself is only a string. What costs money is the usage it unlocks, because requests are tied to a project for billing and quota. OpenAI, for one, offers spend alerts and a hard spend limit to cap it.
Why are API keys so secret?
Because a key acts as a login to your account, and attackers look for them. Bad actors continuously scan public repositories for keys, and even private repositories can be exposed through development environments.
Related guides
Sources
- Stripe docs, API key best practices
- Google Cloud docs, API keys
- Google AI developers forum, unexpected Gemini API charge
- OpenAI docs, production best practices
- AWS IAM docs, updating access keys
- GitHub docs, remediating a leaked secret
- Claude Code docs, sandboxing
- Claude Code docs, permission modes
- Wiz, s1ngularity supply chain attack
- Microsoft Security blog, Contagious Interview
- Artificial Immunity