Calling Claude Messages API: A Practical 2026 Guide
Start With the Right Protocol
Calling the Claude Messages API means sending a POST request to /v1/messages with authentication headers and a JSON body. Do not confuse it with OpenAI Chat Completions: using an OpenAI-compatible client does not automatically make its request format compatible with Claude’s native endpoint. ([platform.claude.com](https://platform.claude.com/docs/en/api/overview?utm_source=openai))
This 2026 guide starts with a direct request, then covers production handling and where the low-cost relay 59API fits.
1. Prepare Credentials and Model Configuration
For direct access, create an Anthropic API key and store it server-side as ANTHROPIC_API_KEY. Never embed it in browser JavaScript, mobile application bundles, or public repositories. Use a secrets manager in production and rotate exposed credentials. Direct requests support Authorization: Bearer authentication; the legacy x-api-key header remains supported. ([platform.claude.com](https://platform.claude.com/docs/en/manage-claude/authentication?utm_source=openai))
Set CLAUDE_MODEL_ID to the exact API identifier your provider documents. A display name such as Claude Sonnet 5.5 is not necessarily the request identifier. Keep model selection configurable rather than scattering it across application code. ([platform.claude.com](https://platform.claude.com/docs/en/api/messages/create))
2. Send a Minimal Request
With both environment variables configured, run this shell command against Anthropic’s direct endpoint:
curl --silent --show-error --fail-with-body --max-time 60 https://api.anthropic.com/v1/messages -H "Authorization: Bearer $ANTHROPIC_API_KEY" -H "anthropic-version: 2023-06-01" -H "content-type: application/json" --data "{\"model\":\"$CLAUDE_MODEL_ID\",\"max_tokens\":256,\"system\":\"Answer concisely.\",\"messages\":[{\"role\":\"user\",\"content\":\"Explain exponential backoff in three sentences.\"}]}"
The request includes a model, an output-token ceiling, and conversation messages. Put application instructions in the top-level system field for this versioned request format. The version header selects API behavior, not the model generation; pin a version supported by your provider and test migrations separately. ([platform.claude.com](https://platform.claude.com/docs/en/api/overview?utm_source=openai))
3. Parse Content and Preserve Conversation State
The response contains a content array, not an OpenAI-style choices array. Collect text from blocks whose type is text rather than assuming the first block always contains the entire answer. Inspect stop_reason: end_turn indicates completion, while max_tokens means generation reached its ceiling. ([platform.claude.com](https://platform.claude.com/docs/en/api/messages/create))
The Messages API is stateless. For follow-up questions, send the relevant earlier user and assistant messages again, followed by the new user message. Preserve structured blocks when continuing tool interactions instead of flattening everything into plain text. ([platform.claude.com](https://platform.claude.com/docs/en/api/messages/create))
4. Add Streaming and Bounded Retries
- Stream deliberately: Set stream to true to receive server-sent events. Handle content deltas and completion events; an individual network chunk is not necessarily a complete event or JSON document. ([platform.claude.com](https://platform.claude.com/docs/en/build-with-claude/streaming?trk=article-ssr-frontend-pulse_little-text-block&utm_source=openai))
- Inspect errors: Check request structure after a 400, credentials after a 401, and permissions after a 403. Repeating an unchanged invalid request will not fix it. ([platform.claude.com](https://platform.claude.com/docs/en/api/errors))
- Retry transient failures: Use bounded exponential backoff with jitter for temporary rate limits and server failures. Honor retry-after when supplied. Check existing SDK retries before adding another retry layer. ([platform.claude.com](https://platform.claude.com/docs/en/api/errors))
- Handle interrupted streams: A streaming response can fail after HTTP 200. Track whether completion actually arrived, and record available request IDs for troubleshooting. ([platform.claude.com](https://platform.claude.com/docs/en/api/errors))
5. Control Costs and Evaluate 59API
Start with modest max_tokens values and record usage alongside task success. An output ceiling is not a fixed charge: responses can finish earlier. Avoid retaining irrelevant conversation history, and measure cost per successful task rather than price alone. ([platform.claude.com](https://platform.claude.com/docs/en/api/messages/create))
59API offers budget-friendly, pay-as-you-go access, advertises official-quality model capability, and provides a referral rebate. Its wider platform supports workflows involving Claude Code, Codex, Gemini CLI, and OpenAI-compatible clients, making it convenient when evaluating multiple model providers. Compare current model-specific prices before estimating savings. ([59api.com](https://59api.com/))
There is an important routing distinction: 59API’s current documentation restricts Claude access to supported Claude Code and Claude Desktop applications. Do not assume an arbitrary raw Messages API request or OpenAI SDK call can access Claude through the relay. Its documented Claude Code setup is:
export ANTHROPIC_BASE_URL="https://api.59api.com"; export ANTHROPIC_AUTH_TOKEN="YOUR_59API_KEY"; claude
Confirm current client requirements before deployment. If that supported workflow fits your project, consider signing up for 59API and running a small, representative quality-and-cost benchmark first. ([59api.com](https://59api.com/docs))
Ready to get started?
Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.
Sign up free