The official TypeScript library for the Anthropic API
npx @anthropic-ai/sdk !npm bundle size
This library provides convenient access to the Claude API from TypeScript or JavaScript.
The full API documentation can be found at platform.claude.com/docs or in api.md.
npm install @anthropic-ai/sdk
<!-- prettier-ignore -->
import Anthropic from '@anthropic-ai/sdk';const client = new Anthropic({
apiKey: process.env['ANTHROPIC_API_KEY'], // This is the default and can be omitted
});
const message = await client.messages.create({
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello, Claude' }],
model: 'claude-sonnet-4-5-20250929',
});
console.log(message.content);
We provide support for streaming responses using Server Sent Events (SSE).
import Anthropic from '@anthropic-ai/sdk';const client = new Anthropic();
const stream = await client.messages.create({
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello, Claude' }],
model: 'claude-sonnet-4-5-20250929',
stream: true,
});
for await (const messageStreamEvent of stream) {
console.log(messageStreamEvent.type);
}
If you need to cancel a stream, you can break from the loop
or call stream.controller.abort().
This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:
<!-- prettier-ignore -->
import Anthropic from '@anthropic-ai/sdk';const client = new Anthropic({
apiKey: process.env['ANTHROPIC_API_KEY'], // This is the default and can be omitted
});
const params: Anthropic.MessageCreateParams = {
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello, Claude' }],
model: 'claude-sonnet-4-5-20250929',
};
const message: Anthropic.Message = await client.messages.create(params);
Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.
You can see the exact usage for a given request through the usage response property, e.g.
const message = await client.messages.create(...)
console.log(message.usage)
// { input_tokens: 25, output_tokens: 13 }
This library provides several conveniences for streaming messages, for example:
```ts
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic();
async function main() {
const stream = anthropic.messages
.stream({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [
{
role: 'user',
content: 'Say hello there!',
... [truncated — view full README on GitHub]
ANTHROPIC_API_KEYUse this skill
Add this skill to your agent's profile to boost its capabilities and score.
Add to My Agent