API Tool Routing
API tool routing maps the agent's tool calls to the correct API endpoints. A well-designed routing layer makes it easy to add, update, or replace APIs without changing the agent logic.
6 min•By Priygop Team•Updated 2026
Tool-to-API Router
Tool-to-API Router
# Route agent tool calls to API endpoints
class APIRouter:
"""
Maps tool names to API calls.
The agent never directly calls HTTP endpoints — it calls tool names.
"""
def __init__(self, base_url: str, api_key: str):
self.client = APIClient(base_url, api_key)
# Map tool names to (method, path_template, param_type)
self.routes = {
"get_order": ("GET", "/orders/{order_id}", "path"),
"list_orders": ("GET", "/orders", "query"),
"create_order": ("POST", "/orders", "body"),
"get_customer": ("GET", "/customers/{customer_id}", "path"),
"update_order": ("PATCH","/orders/{order_id}", "body"),
}
def execute(self, tool_name: str, **kwargs) -> dict:
if tool_name not in self.routes:
return {"status": "error", "error": f"No route for tool '{tool_name}'"}
method, path_template, param_type = self.routes[tool_name]
# Fill path parameters
try:
path = path_template.format(**kwargs)
except KeyError as e:
return {"status": "error",
"error": f"Missing required parameter {e} for {tool_name}"}
# Call the API
if param_type == "path":
return self.client.get(path)
elif param_type == "query":
return self.client.get(path, params=kwargs)
# POST/PATCH would use client.post(path, data=kwargs)
return {"status": "error", "error": "Unknown param type"}
# The agent uses tool names, not URLs
class APIClient:
def __init__(self, base_url, api_key):
self.base_url = base_url
def get(self, path, params=None):
return {"status": "success", "path": path, "params": params}
router = APIRouter("https://api.example.com", "key123")
result = router.execute("get_order", order_id="ORD-001")
print(result)