Your First API Call
Once setup is done, implement the 3 basic patterns (sync chat, streaming, multi-turn).
1. Sync Chat (Minimal)
Send a question, receive the whole response, and return it.
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
const res = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
messages: [
{ role: "user", content: "Difference between TypeScript and JavaScript?" }
],
});
console.log(res.content[0].text);
console.log("Tokens used:", res.usage);
2. Streaming
Receive the response token by token. Better UX.
const stream = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
messages: [{ role: "user", content: "5 things to see in Tokyo" }],
stream: true,
});
for await (const event of stream) {
if (event.type === "content_block_delta") {
process.stdout.write(event.delta.text);
}
}
3. Multi-Turn Conversation
Stack past exchanges in messages.