Your First Claude Call

Install the JavaScript SDK and make a first request to Claude in a few lines of Node.

Let's make Claude say hello from a Node script. This is the smallest possible program that talks to the Messages API.

Install the SDK

The official JavaScript/TypeScript SDK is @anthropic-ai/sdk. Install it with npm.

What the code does

  • Creates a client that reads ANTHROPIC_API_KEY from the environment.
  • Calls messages.create with a model, a token limit, and one user message.
  • Reads the text out of the response.

Every response's content is a list of blocks. For plain text, find the block whose type is text and read its text field.

Example

Example · javascript
// npm install @anthropic-ai/sdk
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic(); // reads ANTHROPIC_API_KEY from the environment

const response = await client.messages.create({
  model: "claude-opus-4-8",
  max_tokens: 256,
  messages: [
    { role: "user", content: "Say hello to a web developer in one sentence." },
  ],
});

// content is a list of blocks; print the text ones
for (const block of response.content) {
  if (block.type === "text") console.log(block.text);
}

When to use it

  • Verify your API key is working by running a minimal Node script before building your full feature.
  • Use the SDK default environment variable detection to avoid passing the key manually in every project.
  • Print the usage field from the response to understand how many tokens your first call consumed.

More examples

Install the Anthropic SDK

Adds the official SDK package that wraps the REST API for Node and TypeScript projects.

Example · bash
npm install @anthropic-ai/sdk
# or
pnpm add @anthropic-ai/sdk

Hello world first Claude call

The smallest complete program that authenticates with the SDK and prints Claude's reply.

Example · javascript
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic(); // reads ANTHROPIC_API_KEY
const msg = await client.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 32,
  messages: [{ role: "user", content: "Say hello!" }],
});
console.log(msg.content[0].text);

Inspect token usage from response

Reads the usage object to see input and output token counts for cost tracking.

Example · javascript
const msg = await client.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 64,
  messages: [{ role: "user", content: "What is 2 + 2?" }],
});
console.log("Answer:", msg.content[0].text);
console.log("Tokens used:", msg.usage);

Discussion

  • Be the first to comment on this lesson.