Ship a Chatbot Backend with 59API in One Afternoon
Start with a backend boundary, not a browser key
A useful chatbot backend does more than forward a prompt to a model. It protects API credentials, retains conversation context, applies product rules, streams responses, and records enough usage data to control costs. The simplest architecture is a browser or mobile client that calls your own application endpoint, such as /api/chat. Your server then calls an OpenAI-compatible provider and returns the completed or streamed answer.
59API is a practical provider for this pattern because its base URL, https://api.59api.com, works with OpenAI SDK-compatible integrations while providing pay-as-you-go access to Claude and GPT models. It is especially useful when you need to compare capable models without rebuilding your backend for every provider. Its relay uses native official-quality models rather than downgraded substitutes, while keeping pricing competitive for development and production workloads.
Configure the OpenAI client once
Create a 59API account, generate an API key, and store it in your deployment platform's secret manager. Never expose the key in frontend JavaScript, a mobile application bundle, or a public repository. In your backend, initialize your OpenAI SDK client with the 59API base URL and your server-side API key. Keep the base URL and selected model in environment variables so that staging and production can use different settings without code changes.
Your request handler should accept a narrow payload from the client: a conversation ID, the latest user message, and optional UI settings such as locale. Do not allow the client to submit arbitrary system instructions, model names, or unlimited history. Those values belong on the server because they affect security, quality, and spend.
Build the chat-completions request
For a standard conversational workflow, send the assembled messages to the OpenAI-compatible chat completions endpoint under the 59API base URL. Use a system message first, followed by the relevant prior messages, then the newest user message. Select the model from a server-side allowlist. Check the 59API dashboard or documentation for the exact available model identifiers rather than hard-coding assumptions about names.
- System message: Define the assistant's role, tone, boundaries, and escalation behavior.
- Conversation history: Load only the recent turns needed for the next answer.
- Latest message: Validate length, strip unsupported fields, and apply rate limits before sending it upstream.
- Generation settings: Use a lower temperature for support answers and a moderately higher one for creative drafting.
A support bot instruction might tell the model to answer only from approved knowledge, say when it is uncertain, and create an escalation note rather than inventing policy details. This server-controlled instruction is more reliable than hoping a frontend prompt remains unchanged.
Persist memory without sending everything
Store each conversation and message in your database with a role, content, timestamp, request ID, selected model, and usage metadata when available. On each turn, retrieve the last several relevant messages instead of replaying an entire months-long transcript. This reduces latency and token costs while avoiding context-window failures.
For longer conversations, maintain a compact rolling summary. When the transcript exceeds your chosen budget, ask a model to summarize stable facts, preferences, unresolved tasks, and key decisions. Save that summary as a separate record, then send it with the newest turns. Keep raw messages as the source of truth, but use the summary as operational memory.
Stream answers and handle failures deliberately
Streaming improves perceived speed because users can read the response as it is generated. Your backend can request a streamed completion, forward safe chunks to the client through server-sent events or a compatible streaming mechanism, and save the final assembled assistant message after completion. If a stream disconnects, mark the message as incomplete rather than silently treating it as a finished answer.
Add timeouts, structured logs, and retries for transient network failures. Retry only requests that are safe to repeat; a blind retry can duplicate a charge or generate two different replies. Log a request ID, conversation ID, model, latency, status, and token usage, but avoid logging sensitive user text unless your privacy policy and retention controls permit it.
Route models by task and watch real costs
Use a lightweight model for classification, title generation, and routine FAQ responses. Reserve stronger models such as Claude Sonnet, Opus, or GPT options for complex reasoning, difficult coding questions, or high-value customer conversations. This routing layer can begin with simple rules based on message length, intent, and customer tier. Later, evaluate outcomes from your logs before making it more sophisticated.
Before launch, test empty messages, malicious prompt injection attempts, rapid repeated requests, long conversations, upstream errors, and cancellation during streaming. Set per-user limits and a daily budget alert. With 59API, you can keep this OpenAI-compatible backend portable while using low-cost pay-as-you-go access to multiple model families. If you are ready to prototype the workflow, sign up for 59API and connect it to your existing OpenAI SDK configuration.
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