Development
Claude API
Guide for using the Claude API effectively. Includes best practices for prompts, streaming, tool use, and building applications with Claude.
Guide for using the Claude API effectively. Includes best practices for prompts, streaming, tool use, and building applications with Claude.
Build applications with the Claude API using best practices for prompts, streaming, and tool use.
npm install @anthropic-ai/sdk
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic();
const message = await client.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Hello, Claude' }
],
});
const message = await client.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
system: 'You are a helpful assistant.',
messages: [
{ role: 'user', content: 'What is the capital of France?' }
],
});
const message = await client.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [
{
role: 'user',
content: 'Extract the name and age from: John is 25 years old.'
}
],
});
const stream = client.messages.stream({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Write a story' }
],
});
for await (const event of stream) {
if (event.type === 'content_block_delta') {
process.stdout.write(event.delta.text);
}
}
const tools = [
{
name: 'get_weather',
description: 'Get the weather for a location',
input_schema: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'City name'
}
},
required: ['location']
}
}
];
const response = await client.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
tools,
messages: [
{ role: 'user', content: 'What is the weather in Paris?' }
],
});
// Check for tool use
if (response.content[0].type === 'tool_use') {
const toolCall = response.content[0];
// Execute tool and return result
}