Sub topic 1
What an API Key Is and Why It Is a Secret
Prerequisites
This chapter assumes you can:
- Use a terminal and set a shell variable.
- Commit and push with Git and know what
.gitignoredoes. - Read short Python or JavaScript snippets.
- Describe an HTTP API as a service you call by sending requests to a URL and reading the responses.
You do not need an account with any LLM provider yet. The next chapter covers creating a key.
What an API key is
An API key is a long, random string that a provider issues to your account. You send it with each request to the provider's API. A typical key looks like this:
sk-proj-3f9aQ1xK...mZ7bThe exact format varies by provider. Many keys have a short prefix that identifies the provider or key type, followed by a random body. The prefix is useful for secret scanners, which you will meet later in this tutorial.
The provider stores a record that ties the key to your account, your billing method, your usage limits, and any permissions you set. When a request arrives with the key, the provider looks up that record and treats the request as coming from you.
Authentication and authorization
Two ideas are bundled into one string here.
- Authentication answers "who is calling?" The key identifies your account or project.
- Authorization answers "what may this caller do?" The key's record determines which models, endpoints, and spending limits apply.
An LLM API key is usually a bearer credential. Whoever bears (holds) the key is treated as the owner. The provider does not check your name, your machine, or your IP address by default. It checks only that the string is valid.
That is the central fact of this chapter. A key proves nothing about the person using it. It proves only that the sender knows the string.
How a key travels in a request
Most LLM APIs expect the key in an HTTP header named Authorization, in the form Bearer <key>. Some providers use a custom header name instead. Your provider's documentation is the authority on this.
Here is the same exchange as a command. The URL is a placeholder, so use the endpoint from your provider's documentation. The key comes from an environment variable, which later chapters explain in detail.
curl -s -o /dev/null -w "%{http_code}\n" \
https://api.example.com/v1/chat \
-H "Authorization: Bearer $LLM_API_KEY" \
-H "Content-Type: application/json" \
-d '{"prompt": "Say hello"}'The command prints only the HTTP status code. A 200 means the provider accepted the key. A 401 usually means the key is missing, mistyped, or revoked. A 403 usually means the key is valid but not permitted to do what you asked.
Why it is a secret
A key behaves like a password, with a few differences that make leaks worse.
- No second factor. A password for a web account is often paired with a code or a device check. An API key typically stands alone.
- It works from anywhere. Anyone on the internet who has the string can send requests with it.
- It is built for automation. Nothing slows an attacker down. Scripts can send thousands of requests per minute.
- It is often long-lived. Many keys have no expiry until you revoke them.
- It is metered. Every request the key makes is charged to your account.
The last point is what sets LLM keys apart from many other credentials. Model calls cost real money, and large requests cost more. A stolen key is a direct line to your billing account.
What a leaked key lets someone do
The exact damage depends on the key's permissions and your account limits. Typical outcomes are:
| Outcome | How it happens | Who pays |
|---|---|---|
| Unexpected charges | An attacker runs their own workloads through your key | You |
| Quota exhaustion | Their traffic uses your rate limit, so your app gets errors | You and your users |
| Data exposure | If the key can read stored files, threads, or fine-tuned models, they can retrieve them | You and your users |
| Account abuse | Requests made with your key generate content that violates provider terms | You, through suspension |
| Resale | The key is shared or sold, and many people use it at once | You |
Attackers do not need to target you personally. Automated scrapers watch public code hosts and look for strings that match known key formats. A key pushed to a public repository can be found and used within minutes. Treat any key that has been exposed publicly as already compromised.
Common misconceptions
"My repository is private, so it is safe." A private repository still exposes the key to everyone with access, including future collaborators, third-party integrations, and anyone who later makes the repository public. Git history also keeps old commits, so deleting a line later does not remove the key from earlier versions.
"I'll only use it for testing." A test key still bills your account and still works from anywhere. The risk depends on the key's permissions and limits, not on your intentions.
"Nobody would find my key." Scanners do not need to know you exist. They search for the pattern, not for you.
"Encoding hides it." Base64 or simple obfuscation does not protect a key. Anyone can reverse it. If your code can use the key, anyone with your code can recover it.
"The provider will notice and stop it." Some providers scan public sources and revoke keys they find. Others do not, and you should not count on it. Billing caused before a revocation may still be yours to pay.
The goal of the rest of this tutorial
The aim is not perfect secrecy, which is impossible in practice. The aim is to make a leak unlikely and make its effects small and short-lived. The tutorial builds this in layers.
- Keep the key out of source code, using environment variables and
.envfiles that Git ignores. - Keep the key on servers you control, never in browser or mobile code.
- Limit what any single key can do and spend.
- Detect exposure early with scanning.
- Revoke and replace keys quickly when something goes wrong.
Each layer covers failures of the others. Developers do commit .env files by accident, so spend limits and revocation matter even when your habits are good.
Check your understanding
Exercise
A teammate says, "The API key is just an identifier, like a username, so it's fine to put it in our front-end code." Using the idea of a bearer credential, explain what is wrong with that claim.
Show answer
A username only says who you claim to be. An API key is a bearer credential, so possession of the string is the only proof the provider checks. Anyone who copies the key from front-end code can send requests that the provider treats as coming from your account, and the usage is billed to you. The key must be kept secret like a password, which means it belongs on a server you control.
Exercise
You get a 401 response from an API, then a 403 response after fixing something. What does each status code usually indicate about your key?
Show answer
A 401 usually means the provider did not accept the key at all: it was missing, mistyped, expired, or revoked. A 403 usually means the key is valid and recognized, but its permissions do not allow the request you made, such as a model or endpoint the key is not scoped for.
Exercise
Name two reasons an exposed LLM key can cost more than an exposed password for a typical web account.
Show answer
Any two of these: the key usually has no second factor; it works from anywhere; it is built for automated high-volume use; it is often long-lived; and every request is metered and billed to your account.