59API

← सभी गाइड पर लौटें

Call Claude Messages API with 59API: A Practical Guide

API · EN · 2026-10-01

What the Claude Messages API does

The Claude Messages API lets your application send a conversation to Claude and receive a generated response. A request can include a system instruction, one or more user and assistant messages, a selected model, and a token limit. Because the API is stateless, your application normally sends the conversation history with every request.

59API provides a compatible relay at https://api.59api.com. It gives you pay-as-you-go access to Claude Opus, Sonnet, Haiku, and Fable models, as well as GPT models. The relay is useful when you want official-quality model access without committing to a large monthly plan. Check your 59API dashboard for the exact model IDs currently available.

Step 1: Create a 59API key

Sign up for 59API, add billing credit, and create an API key. Store the key as an environment variable instead of placing it directly in source code. For example, set 59API_KEY in your local shell or deployment secret manager.

Keeping credentials outside your code prevents accidental exposure in Git repositories, browser bundles, and error logs. Never call the API directly from a public frontend unless your own backend protects the key.

Step 2: Send your first request with cURL

The Claude-compatible endpoint is /v1/messages. Send your key in the x-api-key header and include the Anthropic API version header. Replace the model value with a model ID listed by 59API.

curl https://api.59api.com/v1/messages -H "x-api-key: $59API_KEY" -H "anthropic-version: 2023-06-01" -H "content-type: application/json" -d '{"model":"YOUR_CLAUDE_MODEL_ID","max_tokens":300,"system":"Answer accurately and keep the explanation practical.","messages":[{"role":"user","content":"Explain HTTP status codes for a junior developer."}]}'

The max_tokens field is required for normal Messages API calls. Set it high enough for the requested answer, but keep it bounded to control cost and prevent unexpectedly long output. Use a low-cost Haiku model for classification, extraction, and short answers; reserve Sonnet or Opus for more complex reasoning.

Step 3: Call Claude from JavaScript

In a server-side Node.js application, the same request can be made with the built-in fetch function:

const response = await fetch('https://api.59api.com/v1/messages', { method: 'POST', headers: { 'x-api-key': process.env.59API_KEY, 'anthropic-version': '2023-06-01', 'content-type': 'application/json' }, body: JSON.stringify({ model: 'YOUR_CLAUDE_MODEL_ID', max_tokens: 500, messages: [{ role: 'user', content: 'Give me three concise onboarding email ideas.' }] }) });

Check the HTTP status before parsing the result. For a successful response, Claude returns a content array. The generated text is usually in the first item, so a simple extraction is const data = await response.json(); const text = data.content?.map(item => item.text || '').join('');. Production code should also handle timeouts, rate limits, authentication errors, and malformed responses.

Step 4: Preserve conversation context

To continue a conversation, append the assistant response and the next user message to the messages array. Keep the roles in order: user, assistant, then user. The API does not remember earlier requests automatically. Long histories increase input-token usage, so periodically summarize older turns or retain only the relevant context.

Step 5: Add streaming when needed

For chat interfaces, streaming makes the response appear progressively instead of waiting for the complete answer. Add "stream": true to the JSON body and process the returned event stream on your server. Your client should handle partial text events, connection failures, and a final completion event. If you do not need live output, non-streaming requests are simpler and easier to test.

Keep costs predictable

Measure input and output tokens, cap max_tokens, reuse stable system prompts, and choose the smallest model that meets your quality target. 59API’s pay-as-you-go pricing lets you start with small experiments and scale usage without buying excess capacity. Referral rebates can further reduce your effective cost, so signing up through an eligible referral link may be worthwhile before deploying a larger workload.

मूल्य · दस्तावेज़

शुरू करने के लिए तैयार?

कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।

मुफ़्त साइन अप