The official TypeScript and Node.js SDK for the Anthropic API, providing seamless access to the Claude model family, including Claude 3.5 Sonnet, Haiku, and the Claude Opus 4.6 series. The SDK features a unified architecture, native streaming, structured data handling, and full support for advanced features like Prompt Caching, Tool Use (Function Calling), Computer Use, and the Model Context Protocol (MCP). It is optimized for server-side JavaScript runtimes (Node.js, Deno, Bun, Cloudflare Workers). It acts as the standard method for interacting with Anthropic's APIs, abstracting raw HTTP calls while providing robust type definitions and automatic retries.
Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Anthropic
✓ import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic();
✗ import { Anthropic } from '@anthropic-ai/sdk';
const anthropic = new AnthropicApi();
The SDK exports the Anthropic client as a default export. Destructuring or using non-existent named exports like AnthropicApi will fail in ESM environments.
AWS Bedrock / Google Vertex AI
✓ import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';
import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';
✗ import { AnthropicBedrock } from '@anthropic-ai/sdk';
If you are using Anthropic models through AWS Bedrock or Google Cloud Vertex AI, you must install and import from their specific companion packages (@anthropic-ai/bedrock-sdk or @anthropic-ai/vertex-sdk), not the standard @anthropic-ai/sdk package.
MessageParam
✓ import Anthropic from '@anthropic-ai/sdk';
const msg: Anthropic.MessageParam = { role: 'user', content: '...' };
✗ import { MessageParam } from '@anthropic-ai/sdk';
Anthropic strictly namespaces its types under the default export object. It is highly recommended to use the namespace pattern (Anthropic.MessageParam) rather than attempting to destructure type definitions directly from the root.
A basic Messages API request using the claude-opus-4-6 model, illustrating the required max_tokens parameter and the correct placement of the system prompt.
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
async function main() {
const message = await anthropic.messages.create({
model: 'claude-opus-4-6',
max_tokens: 1024,
system: 'You are a helpful and concise assistant.',
messages: [{ role: 'user', content: 'Explain quantum computing in one sentence.' }],
});
console.log(message.content[0].type === 'text' ? message.content[0].text : 'Non-text response');
}
main();
Errors
Common errors & fixes
400 Bad Request: max_tokens: required field
Unlike OpenAI, which often defaults to infinity or a specific model limit, the Anthropic Messages API strictly requires the developer to explicitly define the maximum number of tokens to generate for every request.
fixAdd the `max_tokens` parameter to your request body, ensuring it is an integer up to the model's maximum output limit (e.g., `max_tokens: 4096`).
400 Bad Request: roles must alternate between 'user' and 'assistant'
Anthropic's Messages API enforces strict conversational turn-taking. You cannot send two 'user' messages sequentially, start a conversation with an 'assistant' message, or leave trailing 'assistant' messages without specific pre-fill techniques.
fixCombine sequential 'user' messages into a single message object, or ensure your messages array strictly alternates the sequence: `user -> assistant -> user`.
Error: It looks like you're running in a browser-like environment. This is disabled by default
The SDK detected a browser environment (like React, Vue, or Next.js client-components). Anthropic blocks this to prevent developers from accidentally exposing their secret `ANTHROPIC_API_KEY` in client-side JavaScript bundles.
fixMove the API call to a backend Node.js server or a serverless API route. If you must run it in the client for testing, instantiate the client with `new Anthropic({ dangerouslyAllowBrowser: true })`. Audit
Dependencies
No dependency data recorded yet.
Agent activity
0 hits · last 30 days
No traffic data recorded yet.