Topic 1 of 5 · 4 min read
Setting up keys and official SDKs
Goal: Make an authenticated call with an official SDK while keeping the API key out of source code and version control.
An API key is a password that spends money, so create it per project, keep it in an environment variable, and let an official SDK read it from there.
Check yourself · 1 of 3
Where should an API key live in a small server-side project?
Why it matters
Every request to Claude, OpenAI or Gemini is billed to the account that owns the key. Anyone who holds the key can run requests on your bill and read what your app sends. Keys pushed to a public repository are found and abused quickly, because providers and attackers both scan for them. A key in source code is also hard to rotate, since every copy of the code holds it.
How to do it
- Create the key in the provider console. Claude uses the Anthropic Console, OpenAI uses the platform dashboard, Gemini uses Google AI Studio. Make one key per project or per environment, and name it so you know what to revoke later.
- Put it in an environment variable. The official SDKs look for a default name:
ANTHROPIC_API_KEY,OPENAI_API_KEYandGEMINI_API_KEY. Export it in your shell or load it from a.envfile that is listed in.gitignore. - Install the official SDK. Use the vendor's own package, not a random wrapper:
pip install anthropicornpm install @anthropic-ai/sdk, and the equivalentsopenaiand@google/genai. The SDK sets the right headers, retries some failures and gives you typed responses. - Create the client with no key in the code. The SDK reads the variable itself.
- Send one small request and print the result. If it works, authentication is fine.
# pip install anthropic (key is read from ANTHROPIC_API_KEY)
import anthropic
client = anthropic.Anthropic()
reply = client.messages.create(
model="MODEL_ID",
max_tokens=100,
messages=[{"role": "user", "content": "Say hello in five words."}],
)
print(reply.content[0].text)
Before and after
Weak: client = Anthropic(api_key="sk-ant-...redacted") committed to the repo. Fixed: client = Anthropic(), with the key in the environment and .env ignored by git. The fixed version also works unchanged in CI and in production, where the key comes from the platform's secret store.
What goes wrong
- The key is committed once. Deleting the file later does not help, because it stays in git history. Revoke the key in the console and create a new one.
- The key ships to the browser. A key used in frontend code is visible to every visitor. Call the API from your server and let the browser call your server.
- One shared key for everything. You cannot tell which app is spending, and revoking it breaks all of them.
- Hand-rolled HTTP too early. Raw requests work, but you rebuild headers, retries and error types the SDK already has.
Examples
The same setup for three providers
export ANTHROPIC_API_KEY="sk-ant-...redacted"
export OPENAI_API_KEY="sk-...redacted"
export GEMINI_API_KEY="...redacted"
Each official SDK reads its own variable, so Anthropic(), OpenAI() and the Gemini client are created with no arguments and no key in the source file.
Loading a local .env file
import "dotenv/config";
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic(); // reads ANTHROPIC_API_KEY
The .env file lives beside package.json, is listed in .gitignore, and a committed .env.example shows the variable names with empty values.
Key points
- An API key is a billing credential: treat it like a password, one key per project or environment.
- Keep the key in an environment variable such as ANTHROPIC_API_KEY and let the official SDK read it.
- Add the .env file to .gitignore before the first commit, not after.
- A key that was committed or pasted into a chat is compromised: revoke it and issue a new one.
- Call the API from a server, never from browser or mobile code that users can inspect.
Common mistakes
- Pasting the key into the source file "just for now" and committing it.
- Putting the key in frontend code or a mobile app bundle.
- Sharing one key across every project, so nothing can be revoked safely.
- Pasting a real key into a chat, ticket or screenshot while asking for help.
Check yourself · 2 of 3
You committed a file containing a live API key, then deleted it in the next commit. What is the correct next step?
Check yourself · 3 of 3
Your Python script contains Anthropic(api_key="sk-ant-...redacted"). What is the smallest fix that follows good practice?
Practice items, not counted towards your mastery.
Start here
- Claude API overview (opens in a new tab)DocsFree
Authentication headers, the Messages endpoint and the official SDK list in one place.
- Working with messages (Claude docs) (opens in a new tab)DocsFree
Roles, multi-turn requests, response fields and stop reasons with runnable examples.
- OpenAI developer quickstart (opens in a new tab)DocsFree
The same first call in the Responses API, with its SDK setup and key handling.
More for this topic
- The Twelve-Factor App: Config (opens in a new tab)ArticleFree
Why credentials belong in environment variables and never in the codebase.
- About secret scanning (GitHub Docs) (opens in a new tab)DocsFree
How leaked keys get detected in repositories and what to do when one is found.