How to Add an AI Agent to Your SaaS That Calls Your Own Backend
To put an AI agent inside your product, keep your interface and your business logic where they are and add one server-side call: your backend sends the user's message to the agent, and the agent calls your API to do the work. You either build that agent loop yourself or call a hosted agent through one endpoint.
6 min read / Updated:
Create an agent and an API keyShort answer: an in-app agent has three parts. Your interface (a chat panel, a command bar, a support widget) collects the request. Your server forwards it, with a secret key, to the agent. The agent decides which operations of your API to call, with which data, and answers. Your API keeps validating and executing every action, so the business rules stay in your code. This guide shows the architecture, the decisions to make and how it works with Qaalia's agent endpoint.
What does an AI agent inside a SaaS actually need?
- A tool for each operation: the agent can only act through operations you describe, such as
create_customerorlist_orders. An OpenAPI spec already describes most of them. - A permission per operation: reads can run freely; refunds and deletions should wait for a person; some operations should be invisible to the model.
- Credentials outside the model: the key the agent uses to call your API is attached at call time and never appears in prompts.
- Conversation state: a conversation id so follow-up messages keep their context, and a user id if each person should be remembered separately.
- Streaming: users expect to see the answer and the steps as they happen, not a spinner for 40 seconds.
- A plan for big jobs: "import these 500 rows" outlives a single HTTP request, so it needs a background task you can poll.
Should you build the agent loop or call a hosted agent?
| Approach | What you build | Good fit when |
|---|---|---|
| Model API with tool calling | The loop, tool schemas, retries, memory, streaming, approvals and logs | The agent is your core product and you have engineers to maintain it |
| Agent runtime API (for example Claude Managed Agents or the OpenAI Agents API) | Your tools, the approval flow and the product around the runtime; one model vendor | You want a managed loop but still design every rule yourself |
| Hosted agent platform behind one endpoint | Your interface and one server-side call; tools, permissions and memory are configured | The agent should operate your existing API without becoming a new codebase |
None of these is wrong. Building the loop yourself gives you total control and the most maintenance. A runtime removes the loop but not the surrounding product, as our comparisons of Claude Managed Agents and the OpenAI Agents API explain. A hosted platform is the shortest path when what you need is an agent that uses the API you already have.
How does it work with one endpoint and an API key?
In Qaalia every agent is reached through the same endpoint, POST …/functions/v1/agent-chat (the full address is in the API reference), called with Authorization: Bearer qlk_live_…; the key selects the agent. The request carries the user's mensaje and, optionally, a conversation_id to continue a conversation, a user_id for per-person memory, adjuntos (files uploaded first through the attachments API) and sistemas to limit which of the agent's systems it may use. The model is not sent: the agent answers with the model configured on it.
- With `stream: true` you get Server-Sent Events, the same events Qaalia Chat uses:
text_deltachunks,paso(what the agent is doing),tool_startandtool_result,ask_user,task_startedandchat_endwith theconversation_id. - Without `stream` you get one JSON when the turn ends, of type
respuesta(the answer),tarea(the job continues in the background) orpregunta(the agent needs more information). - Long tasks are polled with
{ "consultar_tarea": "<task_id>" }and stopped with{ "cancelar_tarea": "<task_id>" }. They keep running even if your server disconnects.
How do you add the agent to your app, step by step?
- Create the agent and connect your AI model with your own provider key (BYOK); without a connected model the agent does not answer.
- Connect your backend as a system: paste your OpenAPI spec or your API's address, or connect a local API with the Qaalia Connector. Store your API's credential in the system; it is encrypted and the model never sees it.
- Set a permission on each operation: allow, requires approval or blocked.
- Create an API key for that agent. It starts with
qlk_live_, is shown once, and belongs to one agent. Keep it in an environment variable on your server. - Add a server route in your app that receives the user's message, checks your own session and permissions, and calls the agent endpoint with the key. The browser never sees the key.
- Stream the answer to your interface with
stream: true, show thepasoevents as progress, and save theconversation_idwith your user's session for follow-ups. - Handle the other outcomes: show
preguntaas a question to the user, and fortareapollconsultar_tareauntil it reachescompleted,failedorcancelled.
What happens when the agent wants to do something risky?
The agent follows the same permissions it has in Qaalia Chat. Allowed operations run. Operations that require approval wait for the agent's owner, who approves or rejects them in Qaalia Chat with the exact data the agent wants to send. Blocked operations are not even known to the model. Your systems' credentials never travel in the request and never reach the model. The approval is answered by the owner, not by the end user of your app, so mark as "requires approval" only the operations you want your team to review. The MCP tool approval guide covers how to choose them.
What should you watch for in production?
- Keep the key on the server: an API key gives full access to the agent. Never put it in web or mobile client code.
- One key per application: your website, your app and your support bot each get their own key, so you can deactivate one without touching the others and see in the logs which one made each request. Plans allow 3, 10 or unlimited active keys per agent.
- Expiration: keys can last 1 hour, 1 day, 7, 30, 90 or 180 days, 1 year or never. An expired, deactivated or unknown key answers
401with a reason code. - Authorize before forwarding: your server decides who may talk to the agent and what they may ask;
user_ididentifies the person for memory, it is not an authentication token. - Bulk work: one long task runs at a time per conversation, and it uses a stable idempotency key per row, so each row is applied exactly once, even if the process restarts mid-batch, as long as your API honors the key. See importing Excel or CSV through your API.
FAQ
Can I call the agent directly from the browser?
You should not. The API key gives full access to the agent, so it belongs on your server. Your frontend talks to your own server route, and that route calls the agent endpoint.
Does the agent remember previous messages?
Yes, within a conversation. Send back the conversation_id the first response returned and the agent continues with its context. If you turn on the agent's memory and send a user_id, it also remembers each person separately.
Can users send files to the agent from my app?
Yes. Upload each file through the attachments API, process it, and include its ruta_storage in adjuntos. Spreadsheets, PDFs, DOCX, text and images are supported, and attachments are deleted after 24 hours unless a running task still needs them.
Is there a ready-made chat widget I can embed?
The documented integration is the endpoint: you build the chat or widget with your own design, and your server calls Qaalia with the key. The owner of the agent also has Qaalia Chat to talk to it directly.
Which AI model answers my users?
The one configured on the agent, with your own key from OpenRouter, OpenAI, Anthropic, Google, xAI, DeepSeek, Moonshot, Qwen, Mistral, Groq or any OpenAI-compatible API. You do not send the model in the request, so changing it does not touch your integration.
Related resources
How to Connect an AI Agent to Your API (Safely) in 2026
Step-by-step guide to let an AI agent call your own API: describe operations, set permissions, keep credentials safe, handle retries and test it, with or without code.
How to Import Excel or CSV Into Your App Through Its API Without Duplicates
How to import Excel or CSV rows into your app through its API: an idempotency key per row, resumable progress, rate limits and what your API should return.
BYOK AI Agent Platforms Compared: Which Let You Use Your Own Model Key?
Which AI agent platforms let you use your own model key, on which plan, with which providers and fees, as of November 2026.

