Sub topic 1
What an API Key Is and Why It Is a Secret
Prerequisites
This chapter assumes basic command-line use, Git, and a general understanding of HTTP APIs. You should also be able to read short Python or JavaScript examples.
What an API Key Is and Why It Is a Secret
An API key is a credential, usually a long random string, that identifies your application to an API. An API is a service that accepts requests from software. An LLM provider uses the key to decide whether a request is allowed and which account pays for it.
Treat the key like a password. Anyone who obtains it may send requests under your account, consume your quota, or create unexpected charges. A key may also grant access to more provider features than your application needs.
Creating Your API Key
Open your provider's console and find its API-key or developer-settings page. Create a key with the narrowest permissions and usage limits available. Copy it when the console shows it. Many providers show the full value only once.
Store the value immediately in a secure password manager or your local development environment. Do not paste it into source code, an issue, a chat message, or a screenshot. Label the key by purpose, such as local-development, so you can revoke it later without affecting other systems.
How Keys Leak
Common leak paths include:
- Committing a key in a source file or
.envfile. - Sending the key to browser or mobile code, where users can inspect it.
- Printing request headers or environment variables in logs.
- Including it in screenshots, support tickets, or copied terminal output.
- Sharing a CI configuration or deployment file containing the value.
A .gitignore entry helps prevent future commits, but it cannot remove a key already present in Git history. A leaked key must be revoked.
Storing the Key in an Environment Variable
An environment variable is a named value supplied to a running process. Set it in your shell instead of writing the key into code:
export LLM_API_KEY="paste-your-key-here"
echo "${#LLM_API_KEY} characters set"The command prints only the length. Your application can read LLM_API_KEY at runtime.
Exercise
Why should a server read the API key from an environment variable?
Show answer
This keeps the credential outside source code, where it is easier to commit, expose, or distribute accidentally.