Skip to main content

Anthropic SDK Setup: Python and JavaScript

5/28
Chapter 2 Claude API Fundamentals

Anthropic SDK Setup: Python and JavaScript

18 min read Lesson 5 / 28

Setting Up the Anthropic SDK

The Anthropic SDK is available for Python and JavaScript/TypeScript. Both are first-party, well-maintained, and offer identical feature sets.

Python Installation

pip install anthropic

# Or with uv (recommended for new projects)
uv add anthropic
import anthropic

# The SDK reads ANTHROPIC_API_KEY from environment automatically
client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, Claude. What can you help me build today?"}
    ],
)

print(message.content[0].text)

JavaScript / TypeScript Installation

npm install @anthropic-ai/sdk

# Or with yarn
yarn add @anthropic-ai/sdk
import Anthropic from "@anthropic-ai/sdk";

// API key read from ANTHROPIC_API_KEY environment variable
const client = new Anthropic();

async function main() {
  const message = await client.messages.create({
    model: "claude-sonnet-4-5",
    max_tokens: 1024,
    messages: [
      {
        role: "user",
        content: "Hello, Claude. What can you help me build today?",
      },
    ],
  });

  console.log(message.content[0].text);
}

main();

API Key Management

Never hardcode API keys. Use environment variables:

# .env
ANTHROPIC_API_KEY=sk-ant-api03-...
# Python — using python-dotenv
from dotenv import load_dotenv
import os

load_dotenv()

# Or pass explicitly
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
// Node.js — using dotenv
import "dotenv/config";

const client = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

Verifying Your Setup

import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-haiku-4-5",
    max_tokens=64,
    messages=[{"role": "user", "content": "Say 'SDK working' and nothing else."}],
)

assert "SDK working" in response.content[0].text
print("Setup verified successfully.")

Store your API key in a password manager and rotate it if it is ever exposed in version control.