An API key is a long string of characters that lets a program use an AI service on your account. When you make a picture in Google AI Studio, you are signed in and sitting at the screen. An API, short for application programming interface, is the same service offered to programs, and a program cannot sign in or tick a box. So the service gives you a key, and the program sends it with every request.
Our example is a lighting brand that wants forty variations of a table lamp, made overnight by a short script while you sleep.
See how a request proves which account pays
A request is a small bundle of text holding the model's name, your prompt, a few settings such as the aspect ratio, and the key. The key travels in a header, a labelled line of text that goes with the request but is not part of the prompt. Google's header is called x-goog-api-key. OpenAI and Replicate use one called Authorization with the word Bearer in front of the key, and fal uses Authorization with the word Key.
The service reads the key, finds the project it belongs to, checks what the key may do, runs the job and adds the cost to that project's bill. The request never says who typed it. Bearer means exactly that, whoever holds the string is trusted.
So anyone holding your key can spend your money, which is why Google's documentation says to treat a Gemini key like a password. A script running on a stranger's machine with your key can ask for pictures as fast as the service will make them. The only limits that stop it are the spending caps you set in advance.
Make the narrowest key the job allows
Every key is tied to a bill. Google says each Gemini API key belongs to a Google Cloud project, which holds the billing, the collaborators and the permissions.
Scope is what a key is allowed to do, and many keys can be narrowed. Google lets you restrict a key to the Gemini API alone and to particular websites, apps or IP addresses. Hugging Face offers read, write and fine-grained tokens, the last limited to specific models or organisations. OpenAI recommends an expiry date on every project key. A narrow key does less damage when it escapes.
Make one key per job and name it after the job, such as lamp-variations-overnight. When one leaks you switch off one job instead of every tool you own, and the name tells you where it was used.
Give the key to your code by name
Each official library looks for its key under a fixed name, held in an environment variable, a named value your terminal hands to every program it starts. Spell the names exactly.
| Provider | What it calls the key | The name its library reads |
|---|---|---|
| API key | GEMINI_API_KEY | |
| OpenAI | API key | OPENAI_API_KEY |
| Hugging Face | Access token | HF_TOKEN |
| Replicate | API token | REPLICATE_API_TOKEN |
| fal | Key | FAL_KEY |
Google's library also reads GOOGLE_API_KEY, and if both are set that one wins, which can make a brand-new key seem to be ignored. Your code only ever contains the name. The value lives in a file or a vault on your own machine.
Keep each key in two places only
Keys leak because they are made to be pasted. They end up in a message to a colleague, a screenshot in a deck, a code file pushed to GitHub, a prompt typed into a chatbot or a web page's code, where Google warns that visitors can pull them out. Some providers scan for leaked keys and switch them off, but only after the leak. A key belongs in the console that made it and in a secrets store on your own computer, and anywhere else counts as a leak.
Some providers, fal among them, show a key only once, so put each new key straight into safe storage. Then write a record beside it, such as "lamp-variations-overnight, for the lamp batch, paid by the lighting brand's project, stored in the lamp folder's .env, expires at the end of the job, seen by me only". On the day a key leaks, that record tells you which key to switch off and what will stop working.
The next lesson covers the terminal in twenty minutes.