Call the Claude Messages API via 59API in Minutes
Start with the right endpoint and credentials
Calling the Claude Messages API requires three essentials: an API key, an available model ID, and a POST request containing messages. This quick-start uses 59API, an AI API relay with the base URL https://api.59api.com, so you can access Claude through a low-cost, pay-as-you-go account.
59API offers native, official-quality models without downgraded replacements, including Claude and GPT options. Its compatibility with Claude Code, Codex, and OpenAI SDKs is useful if you use several development tools. However, SDK compatibility does not make every API format interchangeable: Claude Messages requests have their own schema.
Create a 59API account when you are ready to try it, generate an API key, and check the current model catalog and pricing. Copy an exact Claude model ID from your dashboard rather than guessing an alias. Confirm that the model supports the Claude Messages-compatible route used below.
1. Send a minimal Messages request
In a Bash-compatible terminal, set these environment variables, replacing both placeholders. Use a 59API key, not an Anthropic-issued key. Keep credentials out of source control and browser-side applications.
export API_KEY='YOUR_59API_KEY'; export CLAUDE_MODEL='EXACT_MODEL_ID_FROM_YOUR_DASHBOARD'
Send a POST request to the standard Messages path, /v1/messages. The anthropic-version header identifies the API version; max_tokens limits generated output, not the size of your input.
curl --silent --show-error --fail-with-body https://api.59api.com/v1/messages -H "x-api-key: $API_KEY" -H "anthropic-version: 2023-06-01" -H "content-type: application/json" --data "{\"model\":\"$CLAUDE_MODEL\",\"max_tokens\":256,\"messages\":[{\"role\":\"user\",\"content\":\"Explain HTTP caching in three concise bullet points.\"}]}"
This example uses the native Messages authentication convention, x-api-key. If your relay account documents a different authentication requirement, follow that documentation. The --fail-with-body option preserves error details while making curl exit unsuccessfully for HTTP errors.
2. Read content blocks, not OpenAI choices
A successful Messages response contains a content array. For a simple text request, look for blocks whose type is text and read their text fields. Do not look for choices[0].message.content: that belongs to the OpenAI Chat Completions response format.
- content: May contain multiple blocks. Preserve their structure if you later add tool use or other supported features.
- stop_reason: Explains why generation stopped. A value of max_tokens means the output limit was reached; end_turn normally indicates a completed assistant turn.
- usage: Reports token accounting, including input_tokens and output_tokens. Additional fields may appear for features such as prompt caching.
Do not assume that a successful HTTP response guarantees a complete answer. Check stop_reason before displaying or processing the output.
3. Add instructions and conversation history
Place application-wide instructions in the top-level system field, such as “Answer concisely and include runnable examples.” In the native Messages format, do not insert a message with role system.
For a follow-up question, send the earlier user and assistant messages followed by the new user message. The basic Messages endpoint is stateless: it does not remember your previous request automatically. Retain relevant history, but trim unnecessary turns because repeated context increases input-token usage.
4. Handle failures and streaming deliberately
- Authentication or permission errors: Verify the key, account status, and model access before retrying.
- Invalid requests: Check the model ID, message structure, token limits, and any model-specific requirements.
- Rate limits and temporary server failures: Use bounded exponential backoff with jitter, and respect Retry-After when supplied. Blind retries can create duplicate work and additional charges.
- Streaming: Add stream: true to receive server-sent events. Parse the event stream incrementally rather than treating each network chunk as a complete JSON response.
Keep the first integration inexpensive
59API is among the cheapest relay options, but compare current per-model rates against your actual workload. Start with short prompts and modest output limits, then benchmark a less expensive model against a stronger one using representative tasks. Check usage records rather than estimating cost from request counts alone.
Its pay-as-you-go access avoids a fixed subscription commitment, and a referral rebate can provide additional savings under the current program terms. Once your first request works, move the same validated request structure into your backend, add timeouts and usage logging, and keep API keys server-side.
Prêt à commencer ?
Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.
Inscription gratuite