Getting an API Key

Create an Anthropic API key and store it as an environment variable, never in your code.

To call Claude from your own code you need an API key. You create one in the Anthropic Console, then keep it secret.

Steps

  1. Sign in to the Anthropic Console and open the API keys section.
  2. Create a new key and copy it once — you cannot view it again.
  3. Store it as an environment variable called ANTHROPIC_API_KEY.

Golden rule

Never paste your key into frontend code, commit it to git, or hardcode it in a source file. Anyone who sees the key can spend money on your account. Keep it on the server side only.

How the SDKs find it

The official SDKs automatically read ANTHROPIC_API_KEY from the environment, so you usually do not pass the key in code at all.

Example

Example · bash
# Set the key for your shell session (never commit this)
export ANTHROPIC_API_KEY="sk-ant-..."

# Verify it is set (prints the variable name, not the value in real logs)
echo "Key is set: ${ANTHROPIC_API_KEY:+yes}"

# In a project, put it in .env and add .env to .gitignore:
#   ANTHROPIC_API_KEY=sk-ant-...

When to use it

  • Store your API key in a .env file so it is available to your local Node dev server without hard-coding it.
  • Inject the key as an environment variable in a Docker container so it never appears in the image layers.
  • Rotate a compromised key in the Anthropic Console without touching any code, just update the env variable.

More examples

Store key in a .env file

Keeps the secret out of source code by storing it in an environment file.

Example · bash
# .env  (never commit this file)
ANTHROPIC_API_KEY=sk-ant-api03-...

Load key with dotenv in Node

Uses dotenv to inject the .env file, then lets the SDK pick up the key automatically.

Example · javascript
import "dotenv/config";
import Anthropic from "@anthropic-ai/sdk";

// SDK automatically reads ANTHROPIC_API_KEY from process.env
const client = new Anthropic();
console.log("Client ready:", !!client.apiKey);

Export key for a shell session

Exports the key for the current shell so scripts and CLIs can read it without a .env file.

Example · bash
# Set temporarily in your terminal session
export ANTHROPIC_API_KEY=sk-ant-api03-...

# Verify it is set (value is hidden)
echo ${ANTHROPIC_API_KEY:0:8}...

Discussion

  • Be the first to comment on this lesson.