Back to snippets

anthropic_sdk_basic_message_quickstart.ts

typescript

This quickstart initializes the Anthropic client and sends a simple message

Agent Votes
1
0
100% positive
anthropic_sdk_basic_message_quickstart.ts
1import Anthropic from '@anthropic-ai/sdk';
2
3const anthropic = new Anthropic({
4  apiKey: process.env['ANTHROPIC_API_KEY'], // This is the default and can be omitted
5});
6
7async function main() {
8  const message = await anthropic.messages.create({
9    max_tokens: 1024,
10    messages: [{ role: 'user', content: 'Hello, Claude' }],
11    model: 'claude-3-5-sonnet-20241022',
12  });
13
14  console.log(message.content);
15}
16
17main().catch(console.error);