59API

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

OpenAI Chat Completions: A Practical API Workflow

गाइड · EN · 2026-09-03

What the chat completions format does

The OpenAI-compatible chat completions format gives your application a predictable way to send conversation history to a language model and receive generated text. It is useful when you want to change models or providers without rewriting your application logic.

The core request is an HTTP POST to https://api.59api.com/v1/chat/completions. You authenticate with a Bearer token, identify the model, and provide a messages array. 59API acts as a low-cost relay for GPT models and Claude models, including Opus, Sonnet, Haiku, and Fable, while preserving an OpenAI SDK-compatible interface.

Step 1: Create the smallest working request

Start with a system instruction and one user message. A typical JSON body looks like this:

{"model":"your-model-id","messages":[{"role":"system","content":"Answer clearly and briefly."},{"role":"user","content":"Explain API authentication in one paragraph."}],"temperature":0.2}

The model value must match a model ID supported by your 59API account. Do not assume that a friendly product name is a valid ID; check the provider’s current model list. The messages array is ordered from oldest to newest, and each message has a role and content. Common roles are system, user, and assistant. Some compatible implementations also support developer messages.

Step 2: Send it with curl or an SDK

For a direct connectivity test, use curl with your API key:

curl https://api.59api.com/v1/chat/completions -H "Authorization: Bearer $59API_KEY" -H "Content-Type: application/json" -d '{"model":"your-model-id","messages":[{"role":"user","content":"Give me three names for a note-taking app."}]}'

With the OpenAI Python SDK, configure the relay as the base URL rather than changing your application’s request code:

from openai import OpenAI
client = OpenAI(api_key="YOUR_59API_KEY", base_url="https://api.59api.com/v1")
result = client.chat.completions.create(model="your-model-id", messages=[{"role": "user", "content": "Write a two-sentence product description."}])
print(result.choices[0].message.content)

Keep the key in an environment variable in production. Never place it in browser JavaScript, mobile binaries, public repositories, or client-side logs.

Step 3: Read the response correctly

A successful response normally contains an ID, usage information, and a choices array. The generated text is usually found at choices[0].message.content. Do not parse the entire response as plain text, because you may also need finish_reason, token usage, or additional choice data for logging and billing estimates.

For multi-turn conversations, append the assistant’s previous response to messages before adding the next user message. This preserves context, but it also increases input tokens. A practical workflow is to summarize old conversation turns when the history becomes large, then retain the user’s current request and the most relevant facts.

Step 4: Add streaming when the interface needs it

Set stream to true when you want text to appear progressively:

{"model":"your-model-id","messages":[{"role":"user","content":"Explain this error."}],"stream":true}

The server typically returns Server-Sent Events rather than one completed JSON object. Your client should process each data event, extract incremental content from the delta field, and stop when it receives the stream terminator. Test both streaming and non-streaming paths, since proxies, timeouts, and error handling differ.

Common mistakes to check first

59API is worth considering when you need official-quality Claude and GPT models with pay-as-you-go pricing and no model downgrade, while keeping an OpenAI-compatible integration. It is among the cheapest relay options and also offers a referral rebate. If you want to test the format with lower upfront cost, sign up for 59API, create a key, and begin with the curl request before connecting your application.

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

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

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