Site

Tools — SDK, API Key, Environment

Tutorial 2.0  •  AI / Learn

2.0 What This Teaches

Before writing any AI application you need three things: Python, the Anthropic SDK, and a valid API key. This tutorial covers:

2.1 Python 3.10 and Virtual Environments

The Anthropic SDK requires Python 3.8 or newer; Python 3.10 or later is recommended. Always work inside a virtual environment so SDK versions are isolated per project.
# create and activate a virtual environment
python -m venv .venv

# Windows PowerShell
.venv\Scripts\Activate.ps1

# macOS / Linux
source .venv/bin/activate
Your prompt will change to show (.venv) when the environment is active. All pip install commands from this point install into .venv only, not system Python.

2.2 Installing the SDK

pip install anthropic
For web application tutorials also install Flask:
pip install anthropic flask
For loading API keys from a .env file (covered in section 2.5):
pip install python-dotenv
To record exact versions for reproducibility, freeze your environment after installing:
pip freeze > requirements.txt
Anyone restoring the project later runs:
pip install -r requirements.txt

2.3 Getting an API Key

Navigate to console.anthropic.com, create an account, and open the API Keys section. Click Create Key, give it a name, and copy the value immediately - it is shown only once. The key starts with sk-ant-. Treat it like a password: never put it in source code, never commit it to version control, and revoke it if you believe it was exposed.

2.4 Setting the Environment Variable

The SDK reads ANTHROPIC_API_KEY from the environment automatically. Set it for the current terminal session:
# macOS / Linux
export ANTHROPIC_API_KEY="sk-ant-your-key-here"

# Windows PowerShell
$env:ANTHROPIC_API_KEY = "sk-ant-your-key-here"
To set it permanently on macOS/Linux, add the export line to ~/.bashrc or ~/.zshrc. On Windows, use System Properties → Environment Variables.

2.5 Using a .env File

A .env file in the project root stores the key so you don't have to set the environment variable every time you open a terminal.
# .env  (never commit this file)
ANTHROPIC_API_KEY=sk-ant-your-key-here
Add .env to .gitignore immediately:
# .gitignore
.env
.venv/
__pycache__/
Load it at the top of your script with python-dotenv:
# Load .env before creating the client.
from dotenv import load_dotenv
load_dotenv()

import anthropic
client = anthropic.Anthropic()   # ANTHROPIC_API_KEY is now in the environment

2.6 Verifying the Setup

# verify.py - confirms SDK and API key are working.
import anthropic

client = anthropic.Anthropic()
response = client.messages.create(
    model="claude-haiku-4-5-20251001",
    max_tokens=32,
    messages=[{"role": "user", "content": "Say OK."}]
)
print(response.content[0].text)
OK
Using claude-haiku-4-5-20251001 here keeps the verification call cheap and fast. Any response means the key, network, and SDK are all working.

2.7 Exercise

Exercise Create a fresh project folder with a virtual environment. Install anthropic and python-dotenv. Create a .env file with your API key, add it to .gitignore, and run verify.py. Then run pip freeze > requirements.txt and inspect the file to see which packages were installed.

2.8 Common Mistakes

Installing without a virtual environment

Installing into system Python pollutes the global environment and can break other tools. Always create and activate .venv before running any pip install in a project.

Committing the .env file or the key in source code

Once an API key is in version control history it must be treated as compromised even after deletion - history is recoverable. Revoke the key on console.anthropic.com and create a new one.

Forgetting to activate the virtual environment

Running python verify.py without activating .venv uses system Python, which does not have the anthropic package. The error is ModuleNotFoundError: No module named 'anthropic'.

2.9 Key Terms

TermMeaning
virtual environmentAn isolated Python installation for one project; packages installed here don't affect others
pipPython's package installer; downloads packages from PyPI
requirements.txtA file listing exact package versions; used to recreate an environment
API keyA secret token that authenticates your requests to Anthropic's servers
ANTHROPIC_API_KEYThe environment variable the SDK reads for the API key
.env fileA plain-text file storing environment variables; loaded by python-dotenv
python-dotenvA package that loads .env into the process environment at startup