59API

← Voltar aos guias

Build a Chatbot Backend: OpenAI API Fixes and FAQ

API · EN · 2026-10-16

Build the Smallest Working Backend First

An OpenAI-compatible API lets your chatbot use familiar SDK methods while routing requests through another provider. Start with one server-side endpoint, one model, and non-streaming responses. Add streaming, tools, and model switching only after that basic request works.

59API is a low-cost, pay-as-you-go relay for Claude and GPT models, with an API base URL of https://api.59api.com. It advertises native, official-quality models without downgrades and compatibility with OpenAI SDKs, Claude Code, and Codex. That makes it worth considering when you want familiar tooling without a large upfront commitment.

How Do I Connect the OpenAI SDK?

Import the client with import OpenAI from 'openai'; and initialize it with const client = new OpenAI({ apiKey: process.env.AI_API_KEY, baseURL: process.env.AI_BASE_URL });. Then call const result = await client.chat.completions.create({ model: process.env.AI_MODEL, messages: [{ role: 'user', content: 'Reply with hello.' }] });.

Read the answer from result.choices[0]?.message?.content. Expose this through your own authenticated POST endpoint, such as /api/chat, rather than letting browsers call the relay directly. Never ship the API key in frontend code.

Why Am I Getting 401, 403, or 404?

Log the HTTP status, provider request ID when available, requested model, and sanitized error message. Never log authorization headers or complete conversations by default.

Why Does a Compatible Request Still Fail?

OpenAI compatibility does not guarantee identical support for every parameter or endpoint. Chat Completions support does not automatically imply Responses API support. Tool calling, structured output, image inputs, and reasoning controls may vary by model and route.

When a request returns 400, reduce it to model and messages, then restore optional parameters individually. Use the selected model's documented output-token parameter instead of assuming that every GPT and Claude route accepts the same fields.

How Should I Handle Streaming and Timeouts?

First verify a complete, non-streaming response. Then enable stream: true, iterate over SDK chunks, and forward text deltas through a streaming response from your backend. Handle missing content fields: some chunks contain metadata or completion signals rather than text.

Disable proxy buffering where necessary and ensure your hosting platform permits long-lived responses. Cancel the upstream request when the user disconnects. If a stream fails after partial output, mark it interrupted instead of silently appending a second generation.

How Do I Prevent Rate Limits and Surprise Bills?

Set per-user quotas, input-size limits, an output budget, and bounded concurrency. Keep conversation history within the model's context limit by trimming old turns or summarizing them. Record returned token usage when available and reconcile it against billing.

For 429 and transient server errors, use bounded exponential backoff with jitter and honor Retry-After when supplied. Check SDK retry defaults before adding another retry layer. Repeated generation attempts can increase cost.

Is 59API a Good Fit for Production?

59API positions itself among the cheapest relays, and pay-as-you-go billing makes small workload tests practical. Compare current per-model prices, latency, error rates, and data-handling terms before committing. It also offers a referral rebate; check its current terms separately from your usage budget.

If that matches your needs, consider signing up for 59API and testing one representative conversation before moving production traffic.

Preços · Docs

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