3Accessing Claude with the API

Getting an API key

3 min read559 words

You want to start writing code. You need two things first: a working API key and a place to put it where it won't end up on GitHub. This lesson takes five minutes and you'll never have to think about it again.

1. Create a key in the Anthropic console

Head to console.anthropic.com and sign in. Under API Keys, click Create Key. Give it a name that tells future-you what it's for — course-dev, local-laptop, whatever is obvious — and copy the generated string somewhere safe.

A few things worth knowing:

  • The key is shown once. Close the modal and you can't retrieve it — you can only rotate to a new one.
  • Keys start with sk-ant-. If a string doesn't start with that prefix, it isn't an Anthropic key.
  • Treat the key like a password, not a username. It's a bearer credential — whoever has it, is you.

2. Put the key in a .env file

Create a .env file in the root of your project:

ANTHROPIC_API_KEY="sk-ant-..."

The quotes are optional but harmless, and they save you from occasional shell escaping surprises if the key ever contains characters your loader doesn't like.

Install python-dotenv alongside the Anthropic SDK so your code can read the file:

pip install anthropic python-dotenv

Then in your Python code, load the file once at startup:

from dotenv import load_dotenv
load_dotenv()

load_dotenv() reads .env and sets each key as an environment variable for the current process. The Anthropic SDK automatically picks up ANTHROPIC_API_KEY from the environment, so you don't need to pass it explicitly to the client — Anthropic() with no arguments just works.

3. Keep .env out of version control

Add .env to .gitignore before your first commit:

.env
.env.local
.env.*.local

This one line is the difference between "five-minute setup" and "emergency key rotation and a Slack message to the security channel." Keys committed to public repos are scraped within minutes by automated scanners. Assume any key that touches your git history is already compromised and rotate it immediately.

If your repo already exists and you're adding .env to it now, double-check that .env isn't already tracked: git ls-files | grep .env. If it is, remove it with git rm --cached .env, commit the removal, rotate the key, and only then add the new one to your local file.

4. Verify the setup

A one-liner confirms everything is wired up:

import os
from dotenv import load_dotenv
load_dotenv()
print("key loaded" if os.environ.get("ANTHROPIC_API_KEY") else "not found")

If that prints key loaded, you're ready to make your first real API call in the next lesson.

Key Takeaways

  • 1Create keys in the Anthropic console and copy them immediately — keys are only shown once and must be rotated if lost.
  • 2Store the key in a .env file and load it with python-dotenv; the Anthropic SDK reads ANTHROPIC_API_KEY from the environment automatically.
  • 3Add .env to .gitignore before your first commit — keys committed to git are effectively compromised the moment they're pushed.
  • 4Treat the API key as a bearer credential: whoever holds it can spend against your Anthropic organisation until you rotate it.