OpenAI Chat Completions: A Practical API Workflow
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
- Wrong base URL: use https://api.59api.com/v1 for OpenAI SDK configuration and the corresponding /chat/completions endpoint.
- Invalid model ID: copy the exact supported identifier from your account documentation.
- Missing headers: send both Authorization and Content-Type: application/json.
- Context growth: trim or summarize old messages when requests become slow or exceed limits.
- Unreliable parsing: handle empty content, refusals, multiple choices, and non-200 responses explicitly.
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.
Pronto para começar?
Conecte Claude e GPT em minutos pelos menores preços, sem cortes. Cadastre-se e obtenha sua chave API.
Cadastro grátis