How it works
How to connect your REST API to an AI agent
By the RoutMe team. Last updated .
You connect a REST API to an AI agent by exposing selected endpoints as tools, each with a name, a plain-language description and a typed input schema, then letting the agent call them on demand. An OpenAPI document gives you most of that for free. Start with read-only GET endpoints, a scoped credential and a short list of tools, and widen only after you have watched real conversations.
Why a REST API is a good starting point
Most businesses already have an API, because their website, mobile app or partners need one. That API already enforces authentication, validation and business rules. Pointing an agent at it means the agent inherits those rules instead of bypassing them. This is the main advantage over giving a model direct access to storage, which the guide on connecting a database safely covers separately. For how this compares with retrieval and protocol approaches, see MCP vs REST API vs RAG for support.
Step 1: List the questions, not the endpoints
Write down the ten questions customers ask most. For a store: where is my order, what is your return window, is this in stock. For a SaaS product: which plan am I on, why was I charged. Then map each question to one endpoint. You will usually find that a small number of endpoints cover most of the traffic. Everything else stays unexposed, which keeps the tool list short and the model's choices easy.
Step 2: Read the OpenAPI document with a filter
An OpenAPI document lists every operation with an identifier, parameters and response shapes. Treat it as a catalog and apply a filter:
- Keep only operations whose method is GET.
- Drop anything that returns data about other customers without a customer identifier in the request.
- Drop admin, export and bulk endpoints.
- Keep operations whose response you would be comfortable showing to the verified customer.
If your spec is out of date or missing, writing the few operations you need by hand is faster than generating a full spec.
Step 3: Shape the tool
A tool has three parts that matter. The name should say what it does, such as get_order_status. The description should say when to use it and when not to. The input schema should be strict: required fields, types, allowed values and formats.
Good descriptions carry weight. "Look up one order by order number and the email used at checkout. Use only when the customer has supplied both. Do not use to list orders." tells the model what to ask the customer for before calling. Vague descriptions produce vague calls.
Also decide what the response looks like. A raw endpoint may return internal notes, cost prices or fields the customer should never see. Put a thin layer in front, or an MCP server that selects fields, so the agent only receives what it may repeat.
Step 4: Authentication and scope
Create a dedicated credential for the agent. Give it the narrowest scope your API supports, ideally read-only, and make it revocable on its own. Keep it in a secrets store, never in a prompt or a client-side script. Where possible, require a customer-level check as part of the call, such as matching an order number to an email, so the agent cannot be talked into fetching someone else's record. The reasoning behind this is in what read-only AI means and how an agent looks up order status safely.
Step 5: Handle errors and limits
Agents call tools in loops, so plan for failure. Return clear error messages the model can act on, such as "order not found, ask the customer to check the number". Set timeouts. Apply rate limits to the agent credential so a runaway conversation cannot hammer your service. If a tool fails twice, the agent should stop and hand off rather than guess. See human handoff best practices.
Step 6: Test with real and hostile inputs
Before launch, run a list of real questions and check that the right tool is called with the right arguments. Then try awkward ones: a missing order number, an order that belongs to someone else, a question that needs a write action, text that tells the agent to ignore its rules. The agent should refuse or hand off, and your API logs should show nothing unexpected. The article on prompt injection and read-only tools explains why the read-only credential matters when a message tries to manipulate the agent.
When to add write access
Writes, such as cancelling an order or changing an address, carry real consequences. Add them last, one at a time, behind explicit confirmation and ideally behind a flow where your own rules decide what is allowed. Many teams find the read-only set resolves most volume on its own.
How RoutMe approaches this
RoutMe connects to a business's systems through MCP, APIs and databases, and agents are read-only by default. You can expose existing endpoints by wrapping them in an MCP server or connecting them as described in the developer documentation. Review the transcripts to see what the agent answered. If you want a simple first step, the order status automation job is a common read-only starting point, and the security page describes how credentials are handled.
Sources
Explore the product: RoutMe for Shopify, solutions and the documentation.