Skip to content
mentoor

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?

Choose one answer

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

  1. 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.
  2. Put it in an environment variable. The official SDKs look for a default name: ANTHROPIC_API_KEY, OPENAI_API_KEY and GEMINI_API_KEY. Export it in your shell or load it from a .env file that is listed in .gitignore.
  3. Install the official SDK. Use the vendor's own package, not a random wrapper: pip install anthropic or npm install @anthropic-ai/sdk, and the equivalents openai and @google/genai. The SDK sets the right headers, retries some failures and gives you typed responses.
  4. Create the client with no key in the code. The SDK reads the variable itself.
  5. 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?

Choose one answer

Check yourself · 3 of 3

Your Python script contains Anthropic(api_key="sk-ant-...redacted"). What is the smallest fix that follows good practice?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic