Sub topic 1
What an API Key Is and Why It Is a Secret
Prerequisites
This chapter assumes you can:
- Use a terminal and run basic commands.
- Read short Python or JavaScript snippets.
- Describe what an HTTP API is: a program sends a request to a URL and gets a response.
- Use Git at a basic level (commit, push,
.gitignore).
What an API key is
An API key is a long string of characters that a provider issues to you. You send it with each request to the provider's API. The provider uses it to answer two questions: who is making this request, and are they allowed to make it?
For an LLM provider, a valid key lets the caller run models and be billed for it. The provider does not check who is holding the key. It only checks that the key is valid.
A typical request carries the key in an HTTP header:
import os
import requests
api_key = os.environ["LLM_API_KEY"]
response = requests.post(
"https://api.example.com/v1/chat", # placeholder URL: use your provider's documented endpoint
headers={"Authorization": f"Bearer {api_key}"},
json={"prompt": "Say hello"},
timeout=30,
)
print(response.status_code)The Authorization: Bearer ... header is a common pattern. A "bearer" credential grants access to whoever bears (carries) it. Providers differ in header names, so check your provider's documentation.
What a key authorizes
A key is tied to your account. Anyone who holds a copy can usually do the following:
- Send requests to any model your account can use.
- Consume your quota and rate limits, which can block your own application.
- Run up charges on your payment method.
- Depending on the provider, read usage data or manage other resources.
Why it is a secret
Treat an API key like a password. There are three reasons.
- It is the only proof of identity. The provider has no second check. A copied key works as well as the original.
- Misuse costs real money. LLM requests are billed per use. A stolen key can be used for large workloads, and you pay the bill.
- Leaks are found fast. Automated scanners watch public code hosts for key-shaped strings. A key pushed to a public repository can be found and abused within minutes.
A key differs from a password in one way. A password identifies a person, and the person can be asked for a second factor. A key identifies an application or account and is usually used by code, so it often has no second factor. This is why the rest of this tutorial focuses on where the key is stored.
The provider sees the same valid key in both cases, so it serves both requests and bills you.
Keys versus other credentials
| Credential | Identifies | Typical use | Main risk if leaked |
|---|---|---|---|
| Password | A person | Logging in to a website | Account takeover |
| API key | An application or account | Server-to-API calls | Unauthorized use and billing |
| Short-lived token | A session or user | Temporary access | Limited by expiry |
Short-lived tokens expire, so a leak has a limited window. Most LLM API keys do not expire on their own. A leaked key stays dangerous until you revoke it.
The rule for the rest of this tutorial
Never write the key into source code, a committed file, a screenshot, or a chat message. The next chapters show how to get a key, where it tends to leak, and how to load it from the environment instead.
Exercise
A teammate says, "The key is a random 40-character string, so nobody could guess it. It is fine to commit it to our private repository." Give two reasons this is risky.
Show answer
First, the key's safety depends on staying private, not on being hard to guess. Anyone who sees it can use it. A private repository is visible to everyone with access, and its history, forks, and clones spread copies of the key. If the repository is made public later, or an account with access is compromised, the key is exposed. Second, the key is not tied to a person, and most LLM keys do not expire. A leaked copy keeps working and bills your account until you revoke the key, and committed keys remain in Git history even after the file is deleted.