How to call and test the Claude API

The Claude API's Messages endpoint is small: a model, a token limit and a list of messages. Seeing the raw request and response in an API client is the quickest way to understand it before you reach for an SDK, and those same requests make a useful regression test for prompts later.

Step 1: Key and environment

Create a key in the Claude Console. A separate workspace for testing lets you set its own spend limit and revoke its key without touching production.

VariableValue
baseUrlhttps://api.anthropic.com/v1
anthropicKeyyour key, marked as secret
modela current model ID from Anthropic's models page, for example claude-sonnet-5-5

The Claude API doesn't use Authorization: Bearer. On the collection's Authorization tab, choose API key, with the header name x-api-key and the value {{anthropicKey}}. Requests inherit it.

Step 2: Your first request

HTTP
POST {{baseUrl}}/messages
anthropic-version: 2023-06-01
Content-Type: application/json

{
  "model": "{{model}}",
  "max_tokens": 200,
  "system": "You answer in one short sentence.",
  "messages": [
    {"role": "user", "content": "What does HTTP status 429 mean?"}
  ]
}

The response:

JSON
{
  "id": "msg_…",
  "type": "message",
  "role": "assistant",
  "model": "…",
  "content": [
    {"type": "text", "text": "It means the client sent too many requests in a given time."}
  ],
  "stop_reason": "end_turn",
  "usage": {"input_tokens": 24, "output_tokens": 15}
}

Step 3: Read the text

content is a list of blocks. For a plain answer there's usually one text block, but don't assume it's always the first: with tools, there can be tool_use blocks too. Join the text blocks:

JavaScript
const body = pm.response.json();
const text = body.content.filter((b) => b.type === 'text').map((b) => b.text).join('');

Step 4: Test what's stable

JavaScript
pm.test('answer finished normally', () => {
  pm.response.to.have.status(200);
  pm.expect(body.stop_reason).to.equal('end_turn');
});

pm.test('answer is short, as instructed', () => {
  pm.expect(text).to.not.be.empty;
  pm.expect(text.length).to.be.below(300);
});

pm.test('mentions the key idea', () => {
  pm.expect(text.toLowerCase()).to.match(/too many|rate/);
});

A stop_reason of max_tokens means the answer hit your max_tokens limit and was cut off. That's the one to catch.

Step 5: Structured answers

When your code parses the reply, ask for JSON in the prompt and check that it parses:

Request body
{
  "model": "{{model}}",
  "max_tokens": 200,
  "system": "Reply only with JSON of the form {\"status\": number, \"meaning\": string}.",
  "messages": [{"role": "user", "content": "HTTP 404"}]
}
JavaScript
pm.test('reply is JSON with the expected fields', () => {
  const reply = JSON.parse(text);
  pm.expect(reply.status).to.equal(404);
  pm.expect(reply.meaning).to.be.a('string');
});

Step 6: Tokens and latency

JavaScript
pm.test('stays within the token budget', () => {
  pm.expect(body.usage.input_tokens + body.usage.output_tokens).to.be.below(500);
});

pm.test('responds within 15 seconds', () => {
  pm.expect(pm.response.responseTime).to.be.below(15000);
});

Run a data file of prompts through the request to compare models or prompt versions side by side: Data-driven API tests.

Errors to know

Statuserror.typeUsual cause
400invalid_request_errorA malformed body or a missing max_tokens
401authentication_errorMissing or wrong x-api-key
404not_found_errorA model ID that doesn't exist
429rate_limit_errorToo many requests or tokens per minute
529overloaded_errorThe API is temporarily overloaded; retry later

In CI

Terminal
requestarium run-collection "Claude API" --env Claude --env-var anthropicKey=ANTHROPIC_API_KEY