Build a Chatbot Backend with 59API
Build a Chatbot Backend on an OpenAI-Compatible API
If you want to add an AI chatbot to your product, the fastest path is to build against an OpenAI-compatible API. You get a familiar request format, broad SDK support, and the freedom to switch models without rewriting your backend. In this tutorial, you will build a practical chatbot backend that stores conversation history, calls a model, and returns clean responses to your frontend.
This guide uses 59API as the API provider. 59API is an AI API relay with pay-as-you-go access to Claude models such as Opus, Sonnet, Haiku, and Fable, plus GPT models. It works with OpenAI SDKs, Claude Code, Codex, and other tools that support an OpenAI-style base URL. Its base URL is https://api.59api.com, and it is a strong choice when you want official-quality native models without paying premium relay prices.
Step 1: Create Your Project
Start with a small Node.js backend. You can adapt the same structure to Python, Go, or any stack that can make HTTP requests. Create a new folder, initialize a package, and install the OpenAI SDK plus Express.
Commands: npm init -y, then npm install express openai dotenv cors
Create a simple structure with these files: server.js, .env, and optionally a conversations.js file if you want to separate storage logic. For a production app, use a database such as Postgres, Redis, or MongoDB. For this tutorial, an in-memory store is enough to show the flow.
Step 2: Add Your 59API Credentials
In your .env file, add your API key and base URL. The important part is that the OpenAI SDK can point to 59API by changing only the baseURL option.
Example .env values: API_KEY=your_59api_key_here and API_BASE_URL=https://api.59api.com
You can choose the model based on your use case. For low-latency support bots, a smaller Claude Haiku or compact GPT model is often enough. For complex reasoning, coding support, or multi-step analysis, use Claude Sonnet, Claude Opus, or a stronger GPT model. This flexibility is one reason 59API works well for cost control: you can route simple tasks to cheaper models and reserve premium models for difficult requests.
Step 3: Configure the OpenAI-Compatible Client
In server.js, load environment variables, create an Express app, and configure the SDK. The OpenAI client should use your 59API key and the 59API base URL.
Core idea: create a client with apiKey set to process.env.API_KEY and baseURL set to process.env.API_BASE_URL. From there, you can call chat completions using the same style used by OpenAI-compatible providers.
Your backend should also enable JSON parsing and CORS if a browser frontend will call it. In production, restrict CORS to your actual domain instead of allowing every origin.
Step 4: Design the Chat Endpoint
Create a POST endpoint such as /api/chat. The frontend should send a conversationId and the user message. The backend will load previous messages, append the new user message, call the model, save the assistant response, and return it.
A useful request body looks like this: conversationId for identifying the chat session, message for the user text, and optionally model if you want the frontend or admin settings to choose a model. If you allow model selection from clients, validate it against a safe allowlist so users cannot request unexpected expensive models.
Step 5: Store Conversation History Safely
Chatbots need context, but context costs money. Do not send an unlimited message history on every request. Keep the last 10 to 20 messages, or summarize old messages into a compact system note. This reduces token usage and keeps responses fast.
For a basic implementation, store messages as an array with role and content. The roles are typically system, user, and assistant. Add a system message at the beginning to define behavior, for example: You are a concise support assistant for our product. If you do not know the answer, ask a clarifying question.
- system: sets rules, tone, and boundaries.
- user: stores the visitor’s messages.
- assistant: stores model replies for continuity.
Step 6: Call the Model
When the endpoint receives a message, build the messages array and send it to the chat completion endpoint. Use a model available through your 59API account. Set temperature around 0.2 to 0.5 for support and knowledge-base bots, or higher for creative assistants.
For reliability, add a maximum output token limit. This prevents a single response from becoming too long or expensive. Also wrap the API call in a try and catch block so your application can return a friendly error if the upstream request fails.
A typical response should extract the assistant text from the first choice, append it to the conversation history, and return JSON such as: reply, conversationId, and model. Keeping the API response simple makes frontend integration easier.
Step 7: Add Basic Production Protections
Before deploying, add safeguards. First, rate-limit requests by IP address or user account. Second, validate message length to prevent huge prompts. Third, log request IDs, latency, model name, and token usage if available. These metrics help you understand cost and debug failures.
You should also avoid storing sensitive user data unless necessary. If users may paste private information, disclose how chat data is handled and set a retention policy. For internal tools, consider redacting secrets before sending prompts to any model provider.
Step 8: Optimize Cost with Model Routing
One practical advantage of 59API is that it gives access to multiple Claude and GPT models through a compatible interface, while remaining one of the cheaper relay options. You can start with an affordable model for most conversations, then escalate only when needed.
- Use a fast, low-cost model for greetings, FAQs, and simple classification.
- Use Sonnet or a strong GPT model for technical support and reasoning.
- Use Opus-level models only for high-value, complex conversations.
- Summarize long chats to reduce repeated context costs.
Because 59API uses native official-quality models without downgrading, you can optimize for price without accepting weaker hidden substitutes. Its referral rebate can also reduce costs further if you share it with other developers or teams.
Step 9: Connect Your Frontend
Your frontend only needs to call your backend, not 59API directly. This keeps your API key secret. Send user text to /api/chat, display the returned reply, and preserve the conversationId in local storage or your user session. For a better user experience, show a loading state, disable duplicate submissions, and handle errors with a retry button.
If you later want streaming responses, add a streaming route using server-sent events or WebSockets. Start with non-streaming first because it is easier to test and monitor.
Final Checklist
- Create a backend project with Express and the OpenAI SDK.
- Set https://api.59api.com as the OpenAI-compatible base URL.
- Store short conversation history with system, user, and assistant roles.
- Validate input, rate-limit users, and cap output length.
- Route simple and complex tasks to different models to control cost.
With this setup, you get a clean chatbot backend that can work with Claude and GPT models through one familiar API shape. If you are looking for a low-cost way to ship AI features without locking yourself into one model family, 59API is worth trying when you create your next chatbot backend.
¿Listo para empezar?
Conecta Claude y GPT en minutos a los precios más bajos, sin recortes. Regístrate para obtener tu clave API.
Registro gratis