Claude TypeScript SDK Installation Guide

Written by Michael Lip · Solo founder of Zovo · $400K+ on Upwork · 100% JSS Join 50+ builders · More at zovo.one

The Anthropic TypeScript SDK provides type-safe access to the Claude API with built-in streaming, retries, and error handling. This guide covers installation, runtime support, and your first API call.

Quick Fix

Install and run your first Claude API call:

npm install @anthropic-ai/sdk
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();
const message = await client.messages.create({
  model: "claude-sonnet-4-6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Hello, Claude" }]
});
console.log(message.content[0].type === "text" ? message.content[0].text : "");

What You Need

Full Solution

Install the SDK

# npm
npm install @anthropic-ai/sdk

# pnpm
pnpm add @anthropic-ai/sdk

# yarn
yarn add @anthropic-ai/sdk

Platform-Specific SDKs

For cloud provider integrations, install the platform-specific package:

# Amazon Bedrock
npm install @anthropic-ai/bedrock-sdk

# Google Vertex AI
npm install @anthropic-ai/vertex-sdk

# Microsoft Foundry
npm install @anthropic-ai/foundry-sdk

Set Your API Key

The SDK reads ANTHROPIC_API_KEY from the environment:

# Linux / macOS
export ANTHROPIC_API_KEY="sk-ant-your-key-here"

# Windows PowerShell
$env:ANTHROPIC_API_KEY = "sk-ant-your-key-here"

Or pass it explicitly:

const client = new Anthropic({ apiKey: "sk-ant-your-key-here" });

Basic Usage

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

const message = await client.messages.create({
  model: "claude-sonnet-4-6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Explain quantum computing in 3 sentences." }]
});

for (const block of message.content) {
  if (block.type === "text") {
    console.log(block.text);
  }
}

console.log(`Tokens: ${message.usage.input_tokens} in, ${message.usage.output_tokens} out`);

Streaming

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

// Stream helper with event callbacks
const stream = client.messages.stream({
  model: "claude-sonnet-4-6",
  max_tokens: 4096,
  messages: [{ role: "user", content: "Write a short story" }]
}).on("text", (text) => {
  process.stdout.write(text);
});

const message = await stream.finalMessage();
console.log(`\nDone. Output tokens: ${message.usage.output_tokens}`);

Error Handling

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

try {
  const message = await client.messages.create({
    model: "claude-sonnet-4-6",
    max_tokens: 1024,
    messages: [{ role: "user", content: "Hello" }]
  });
} catch (err) {
  if (err instanceof Anthropic.AuthenticationError) {
    console.error("Invalid API key");
  } else if (err instanceof Anthropic.RateLimitError) {
    console.error("Rate limited -- retry later");
  } else if (err instanceof Anthropic.APIError) {
    console.error(`API error: ${err.status} ${err.message}`);
  }
}

Configure Retries and Timeouts

import Anthropic from "@anthropic-ai/sdk";

// Custom retries (default: 2)
const client = new Anthropic({ maxRetries: 5 });

// Custom timeout (default: 10 minutes)
const client2 = new Anthropic({ timeout: 20 * 1000 }); // 20 seconds

For large max_tokens values without streaming, the SDK dynamically calculates timeouts up to 60 minutes.

Enable Debug Logging

# Via environment variable
ANTHROPIC_LOG=debug node your_script.js

# Or set logLevel in the client
const client = new Anthropic({ logLevel: "debug" });

The SDK can run in browsers but this exposes your API key:

const client = new Anthropic({
  apiKey: "sk-ant-...",
  dangerouslyAllowBrowser: true  // Required flag
});

Use a backend proxy in production instead of exposing your API key client-side.

Prevention

  1. Pin your SDK version: Use exact versions in package.json for production stability.
  2. Use environment variables: Never commit API keys to source control.
  3. Check TypeScript version: Run npx tsc --version and verify it is 4.9 or higher.
  4. Use Node.js 20+: Older Node.js versions may have issues with the SDK’s streaming implementation.