Quickstart: Messages

Purpose and Scope

This quickstart shows the shortest path from an installed SDK to a working Claude response using the Messages API. The repository presents the TypeScript SDK as the official server-side TypeScript and JavaScript library for the Claude API, and its README starts new users with a single default import, a client instance, and a call to the messages resource. The example program in the repository follows the same pattern, which makes this workflow the canonical first smoke test for credentials, runtime setup, and request shape before moving into streaming, tools, batches, or Managed Agents. Sources: README.md, examples/demo.ts

The Messages API is the direct prompting interface for Claude. In this SDK, you create an Anthropic client, then call the messages resource with a model, a token budget, and an ordered array of conversation messages. Each message has a role and content. For a first request, a single user message is enough. That basic structure matters because the same conversation array later expands naturally into multi-turn chat, system instructions, tool-use loops, file inputs, vision inputs, and structured output patterns without changing the client entry point. Sources: README.md, src/resources/messages/index.ts

Relevant Source Files

  • README.md — introduces the package, installation command, supported runtimes, and the minimal Messages API getting-started snippet.
  • examples/demo.ts — provides a runnable TypeScript example that constructs the client from the environment and calls the messages resource.
  • src/resources/messages/index.ts — re-exports the generated Messages resource and the public message-related TypeScript types used by callers.
  • api.md — provides generated API reference context for the SDK surface covered by the quickstart.

Core Primitives

The first primitive is the Anthropic client. It represents SDK configuration and owns the resource namespaces, including the messages resource used here. The README shows passing an API key explicitly from the process environment, while the demo intentionally omits constructor options because the client reads the standard environment variable by default. For a beginner, the important rule is simple: set credentials in the environment for local development, avoid putting secrets in source control, and only pass constructor options when you need to override defaults such as authentication, headers, transport, or runtime behavior. Sources: README.md, examples/demo.ts

The second primitive is the request payload. The quickstart payload needs three meaningful fields: a model identifier, a maximum output token count, and an array of messages. The README example uses a greeting prompt and a current Claude model name, while the demo uses the same shape with another model. The SDK sends this object through the generated messages resource, so the TypeScript surface is not a hand-written wrapper around a special quickstart path; it is the same resource family that later exposes content blocks, stream events, token counting, citations, thinking blocks, and other message types. Sources: README.md, examples/demo.ts, src/resources/messages/index.ts

The third primitive is the response message. A successful call returns a message object whose content can be logged or inspected. The README logs the response content directly, which is useful for a minimal terminal check. The demo prints the whole result, which is better when you want to see metadata, stop information, usage details, and the exact content block structure. For production code, treat the response as structured data rather than as a plain string; Claude outputs text inside content blocks, and later features may introduce additional block types that require deliberate handling. Sources: README.md, examples/demo.ts, src/resources/messages/index.ts

Install and Prepare Credentials

Install the primary package with npm, then set the API key in your shell before running the example. The README gives the installation command and states that the SDK is intended for server-side TypeScript or JavaScript applications. It also documents supported runtimes, including modern Node.js, Deno, Bun, Workers, and edge-style runtimes, while warning that browser usage is disabled by default to avoid exposing secret credentials. That constraint is part of the quickstart: begin in a trusted server-side environment, confirm that environment variables are available, and only revisit browser settings after you understand the security tradeoff. Sources: README.md

npm install @anthropic-ai/sdk
export ANTHROPIC_API_KEY=your_api_key_here

If you are using TypeScript, the repository states that TypeScript version 4.9 or newer is supported. The example file is written as a TypeScript script and imports the default SDK export directly from the package name. In a normal application, the same import can live in a route handler, background worker, server action, command-line tool, or test harness. The key operational detail is that the client creation should happen in server-owned code where the environment variable is available and where logs, errors, and retries can be observed safely. Sources: README.md, examples/demo.ts

First Request

Create a file such as a local quickstart script and use the same shape shown in the README. The constructor may include an API key field, but that field is the default environment behavior and can be omitted when the variable is set. The payload includes a single user turn, a model, and a token limit. The request is asynchronous, so call it from an async function, a top-level await environment, or a framework handler that already supports promises. Sources: README.md, examples/demo.ts

import Anthropic from '@anthropic-ai/sdk';
 
const client = new Anthropic({
  apiKey: process.env['ANTHROPIC_API_KEY'],
});
 
const message = await client.messages.create({
  model: 'claude-opus-4-6',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Hello, Claude' }],
});
 
console.log(message.content);

The repository demo is a useful second version because it demonstrates the environment-only constructor. It creates the client with no arguments, adds a single user message, sets a model and token budget, and prints the full result with directory-style inspection. Use that style when diagnosing whether your program is receiving the response structure you expect. Use the README style when teaching the simplest possible flow or when you want credentials to be visible in configuration code without hard-coding the secret itself. Sources: README.md, examples/demo.ts

import Anthropic from '@anthropic-ai/sdk';
 
const client = new Anthropic();
 
async function main() {
  const result = await client.messages.create({
    messages: [{ role: 'user', content: 'Hey Claude!?' }],
    model: 'claude-sonnet-5',
    max_tokens: 1024,
  });
 
  console.dir(result);
}
 
main();

System-to-Code Mapping

Quickstart concernSDK surfaceSource grounding
Install the official SDKPackage installation shown in the READMEREADME.md
Configure credentialsClient reads the standard API key environment variable by defaultREADME.md, examples/demo.ts
Construct the clientDefault import creates an Anthropic client instanceREADME.md, examples/demo.ts
Send a promptCall the messages resource create method with model, token limit, and messagesREADME.md, examples/demo.ts
Understand available typesGenerated messages index re-exports message, content block, event, and parameter typessrc/resources/messages/index.ts
Look up API detailsGenerated API reference path for deeper method and model detailsapi.md

The generated messages index is important even though beginners usually start from the README. It shows that the public messages surface is broad and typed: message parameters, content block parameters, stream event types, token usage types, model aliases, stop reasons, and specialized blocks are exported from the messages package. This confirms that the quickstart call is not a toy endpoint; it is the base of the stable SDK resource that supports richer Claude interactions. When you add capabilities later, prefer extending the same request object and using exported types instead of inventing untyped local shapes. Sources: src/resources/messages/index.ts

Common Next Steps and Troubleshooting

If the first request fails, check the basics in the order the quickstart uses them. Confirm the package is installed, the script is running in a supported runtime, the API key environment variable is present in the same process, and the request includes a model, a positive token budget, and at least one user message. If the call succeeds but logging looks surprising, print the entire response once before extracting text. That helps distinguish transport problems from content parsing assumptions, especially because message content is structured as blocks rather than guaranteed to be one plain string. Sources: README.md, examples/demo.ts, src/resources/messages/index.ts

After the first response, choose the next guide based on control needs. Stay with the Messages API when you want direct prompting, custom conversation state, your own tool loop, or fine-grained streaming behavior. Move to Managed Agents when you need long-running asynchronous work, managed infrastructure, sessions, deployments, and related agent resources. Within this repository’s docs, the natural next pages are the Messages API reference for request and response detail, Streaming Responses for incremental output, Tool Use Overview for function-style integrations, and Authentication and Client Configuration for production client setup. Sources: README.md, src/resources/messages/index.ts, api.md