Sub topic 2
Creating your API key
This chapter covers how to create an API key in a provider console and what to do in the moments after it appears. It assumes you can use a terminal, have a rough idea of what an HTTP API is, and have read the previous chapter on what a key authorizes. It does not cover loading the key into code. The next chapters do that.
What you are creating
An API key is a long random string that identifies your account to the provider. Your code sends it with each request, usually in an Authorization header. The provider uses it to decide whose quota to use and whose bill to charge.
The key does not prove who is calling. It proves only that the caller knows the string. Anyone who has a copy can spend your money and use your rate limits. That is why the console treats creation as a one-time event.
Most providers follow the same pattern, though button names and menu layout differ:
- You sign in to a web console with an account.
- You attach billing or a free-tier plan.
- You create a key, optionally inside a project or workspace.
- The console shows the full key once.
- After that, the console shows only a label and a few trailing characters.
The provider stores a hash of the key, not the key itself. That is why it cannot show it to you again. If you lose it, you create a new one.
Before you create the key
A few choices made now affect how easily you can limit damage later.
Pick the right account
Use an account you control long term. A key created under a personal account for a work project is awkward to hand over, and it can break when that person leaves. For a team, create the key under an organization or workspace account, not a personal one. Turn on two-factor authentication for the console account. Anyone who can sign in to the console can create new keys, so the console login is as sensitive as the keys themselves.
Understand the billing model
Most LLM providers charge by usage, measured in tokens. Some require prepaid credit. Others bill after the fact and may let usage run past what you expected. Before you create a key, find three things in the console:
- Whether usage is prepaid or billed afterward.
- Whether you can set a monthly spend limit or a budget alert.
- Whether the free tier has its own rate limits.
Set a low spend limit now, even a few dollars. A later chapter covers limits in detail, but the console is open and it takes a minute.
Use a project if one is offered
Many consoles group keys under projects or workspaces. A project lets you separate usage and cost, and it lets you revoke one project's keys without touching the others. If your provider offers this, create a project named for what the key is for, such as tutorial-dev, and do not put every key in a default project.
Creating the key
The exact clicks vary, so look for a section named something like "API keys", "Credentials", or "Access". Then:
- Choose to create a new key.
- Give it a name that says where and why it is used, for example
laptop-dev-tutorialorci-integration-tests. - If the console asks for permissions or scopes, choose the narrowest option that lets your code work. Some providers offer read-only or restricted keys. Others offer only full-access keys.
- If the console asks for an expiry, set one for experiments. A key that expires on its own cannot stay valid forever in a forgotten file.
- Confirm. The console shows the key.
A good name is the cheapest security feature available. When you later see unexpected usage, a key named laptop-dev-tutorial tells you where to look. A key named key1 tells you nothing.
The moment the key is shown
This is the step that causes most problems. The dialog shows the key once, and what you do next matters.
Copy it directly into a password manager
Paste the key into a password manager entry, or another secret store you already trust. Put the key name and project in the entry's notes so you can match it to the console later. Do not paste it into any of these:
- A chat message or email to yourself.
- A note-taking app that syncs to shared folders.
- A shared document or wiki.
- A screenshot. Screenshots of the console dialog are a common leak path, and recording a screen while creating a key has the same problem.
- A source file, even temporarily. Temporary edits often get committed.
Set it in your shell without leaving it in history
The chapter on environment variables covers this properly. For now, you only need to confirm that the key was copied correctly. The simplest approach is the one from the plan:
export LLM_API_KEY="paste-your-key-here"
echo "${#LLM_API_KEY} characters set"The echo prints only the length, so the key never appears on screen. There is one gotcha. In bash, a command typed at the prompt is saved to your history file, which means the export line above stores the key in plain text in ~/.bash_history. To avoid that, read the value without echoing or recording it:
read -rs -p "Paste your key: " LLM_API_KEY
echo
export LLM_API_KEY
echo "${#LLM_API_KEY} characters set"Here -s hides typed input, -r stops backslash handling, and -p sets the prompt. The variable exists only for the current shell session. When you close the terminal it is gone, which is acceptable at this stage.
Check the key's shape, not its secrecy
Keys from a given provider often have a recognizable shape: a prefix, a length, a character set. You can use this to catch paste mistakes without printing the key. A length check is enough for most mistakes. Typical errors are:
| Symptom | Likely cause |
|---|---|
| Length is a few characters too short | The paste cut off the end, or the selection missed the last characters |
| Length is a few characters too long | A trailing space or newline was copied |
| Length is zero | The variable was never set, or it was set in a different terminal |
| Requests fail with an authentication error | The key is wrong, revoked, expired, or belongs to a different project than the one you are calling |
A trailing newline or space is the most common copy-paste fault. It makes a valid key fail with an authentication error and is hard to see by eye. Checking the length catches it.
Common mistakes at creation time
- Creating one key and reusing it everywhere. When one of many uses leaks, you must revoke it and break all of them. One key per environment and per tool keeps the revocation small.
- Choosing a full-access key when a restricted one is available. A leaked restricted key can do less damage.
- Skipping the spend limit because the free tier seems safe. Free tiers end, and accounts with a stored payment method can move to paid usage.
- Testing with the key in a notebook or script and leaving it in a cell output. Output cells are saved with the file.
- Treating the console as less sensitive than the key. A compromised console login lets an attacker create keys you have never seen.
What you have at the end
You should now have four things:
- A key stored in a password manager.
- A key name that tells you its purpose.
- A spend limit or budget alert on the account.
- A shell session where
LLM_API_KEYis set and has the expected length.
The key is not yet safe to use in a project. Keys leak mostly through the ordinary habits of development, and the next chapter covers those habits so you can avoid them.
Exercise
You created a key, closed the dialog, and then realized you did not copy it. The console now shows only the key name and its last four characters. What should you do, and why?
Show answer
Delete that key and create a new one, copying the new key into a password manager before closing the dialog. The provider stores only a hash, so it cannot show the original again. Leaving the old key active is a risk, because it is a working credential that nobody has recorded.
Exercise
You run export LLM_API_KEY="..." at the bash prompt and then echo "${#LLM_API_KEY} characters set" prints 52, but the console says keys are 51 characters. What is the most likely problem, and how can you fix it without retyping blindly?
Show answer
A trailing space or newline was probably copied with the key. Set the variable again with read -rs -p "Paste your key: " LLM_API_KEY, paste more carefully, and recheck the length. Also remove the earlier export line from your shell history, since it stored the key in plain text.