59API

← Voltar aos guias

Build a Chatbot Backend with an OpenAI-Compatible API

API · EN · 2026-07-09

Build a Production-Ready Chatbot Backend on an OpenAI-Compatible API

Building a chatbot backend is much easier when your provider supports the OpenAI API format. You can use familiar SDKs, switch between GPT and Claude models, and avoid rewriting your application every time you change models. In this tutorial, you will build a practical Node.js backend that sends chat messages to an OpenAI-compatible endpoint using 59API, a low-cost AI API relay with pay-as-you-go access to Claude Opus, Sonnet, Haiku, Fable, and GPT models.

59API is useful for developers because its base URL works with standard OpenAI SDK patterns: https://api.59api.com. That means you can connect existing tools like Claude Code, Codex-style workflows, or any OpenAI SDK with minimal configuration. It also uses native official-quality models, so you are not trading quality for lower pricing. If you are building prototypes, internal tools, or customer support bots, the cheaper relay pricing and referral rebate can significantly reduce operating costs.

Step 1: Create Your Backend Project

Start with a small Express server. This example uses Node.js because it is common for chatbot backends and works well with streaming responses.

Install dependencies:

Your backend will expose one route, such as /api/chat, that receives a conversation from your frontend, forwards it to 59API, and returns the assistant response.

Step 2: Configure the OpenAI SDK for 59API

The key idea is that you do not need a custom client. You can use the OpenAI SDK and change the base URL to 59API.

Create an environment file with your API key:

In your server code, initialize the client with the custom base URL. The exact code depends on your SDK version, but the important settings are the API key and base URL. This pattern lets you keep your application portable. If you later test a GPT model, Claude Sonnet, or Claude Haiku, most of your backend logic stays the same.

Step 3: Design a Clean Chat Request Format

Your frontend should send an array of messages, not just a single prompt. A reliable format looks like this:

On the backend, validate the messages before sending them to the model. Reject empty messages, extremely long input, or unsupported roles. This protects your API budget and reduces accidental misuse. For most chatbots, you should also set a maximum number of previous messages, such as the last 10 to 20 turns, to control token usage.

Step 4: Create the Chat Completion Route

Your /api/chat route should perform five tasks: read the request body, validate the message list, add or enforce a system prompt, call the model, and return the response.

For a customer support bot, your system prompt might say: You are a helpful support assistant. Answer only from approved product knowledge. If you are unsure, ask a clarifying question. Keeping this instruction on the server is important because users cannot easily remove or overwrite it from the frontend.

When calling the API, choose a model based on your use case. Claude Haiku or a smaller GPT model may be excellent for fast, low-cost replies. Claude Sonnet is often a strong middle ground for reasoning and writing quality. Higher-end models such as Opus can be reserved for premium workflows or difficult escalations. With 59API, testing across model families is straightforward because the API remains OpenAI-compatible.

Step 5: Add Streaming for a Better User Experience

For chat interfaces, streaming is often better than waiting for a full response. Users see the answer appear token by token, which makes the app feel faster. On the backend, enable streaming in your model request and forward chunks to the browser using Server-Sent Events or a readable stream.

If you are just starting, build a non-streaming version first. Once it works reliably, add streaming. This makes debugging simpler because you can confirm authentication, model names, request format, and response parsing before handling incremental chunks.

Step 6: Manage Cost, Rate Limits, and Safety

A good chatbot backend should not blindly forward every request. Add basic production safeguards:

This is where a cheap relay matters. Chatbots can generate many small requests throughout the day, and costs can grow quickly. 59API’s pay-as-you-go pricing helps keep experiments affordable while still giving access to official-quality Claude and GPT models. If you are evaluating providers, it is worth signing up for 59API and running the same prompts across several models to compare quality, latency, and cost in your own application.

Step 7: Prepare for Deployment

Before deploying, move configuration into environment variables, enable CORS only for your frontend domain, and add structured error responses. Avoid returning raw provider errors to users. Instead, show a friendly message such as: The assistant is temporarily unavailable. Please try again.

Deploy the backend to a platform such as Render, Fly.io, Railway, Vercel serverless functions, or a container host. After deployment, test these flows: a normal short message, a long message, a conversation with history, an invalid request, and a simulated provider failure. These tests catch most issues before real users do.

Conclusion

An OpenAI-compatible API makes chatbot development faster because you can use existing SDKs, standard message formats, and familiar deployment patterns. By setting the base URL to https://api.59api.com, your backend can access Claude and GPT models through a low-cost relay without sacrificing native model quality. Start with a simple validated chat route, add streaming, control costs with limits, and then iterate on prompts and model choice based on real user conversations.

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