← Back to API development
API development / Lesson 02

Make your first Claude API request

Set up credentials, choose an accessible model, and read a real response.

Before you begin

Use a Claude Console account with API access and available usage. Check billing in the Console: desktop access alone does not prove API access, even when a plan offers claimable API credits. Use Node.js 22+ for the downloadable examples or a terminal with curl for HTTP.

1. Keep credentials on the server

Create a key in the Console and keep it in local environment variables or a server secret. Never put it in a public page, browser bundle, or committed file. The downloads include .env.example; create a local .env with your own key and exclude it from version control. Set ANTHROPIC_WORKSPACE_ID only if your key needs an explicit workspace.

dotenv
ANTHROPIC_API_KEY=YOUR_LOCAL_KEY
ANTHROPIC_MODEL=AN_ACCESSIBLE_MODEL_ID
# ANTHROPIC_WORKSPACE_ID=wrkspc_...

2. Pick a model you can access

Use the Console or the Models API to obtain a current model ID and set ANTHROPIC_MODEL. Do not copy a retired model ID from an old tutorial. The headers below use the supported x-api-key authentication and API version; add anthropic-workspace-id for keys that require it.

shell
curl --fail-with-body https://api.anthropic.com/v1/models \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01"

3. Send a small message

Download client.mjs and first-call.mjs into the same folder, create .env there, and run the command below. Node reads the environment file, posts to /v1/messages, and prints text blocks plus usage. This sends a real request and can consume API credits. A page preview itself makes no model request.

shell
node --env-file=.env first-call.mjs

4. Read the complete response

content is an array, not a guaranteed single string. Extract text blocks, inspect stop_reason, and keep usage for cost visibility. end_turn indicates a finished turn; max_tokens means output was cut short, and tool_use requires the next guide’s tool round trip. Errors are not valid empty answers.

Download the example files

Teaching examples; model behavior can vary. Follow the prerequisites and verify outputs. These pages do not send API requests.

Practice & self-check

YOUR TURN / PRACTICE

Make one short request from your local environment, inspect usage and stop_reason, then remove the key from any temporary notes.

When you’re done, take a moment to check:

Read the official sources

CLI instructions checked against official documentation; API examples tested with simulated responses. Product-account walkthroughs were not performed.

Start with a question