API Router Organization
Purpose and Scope
This document describes the organization and structure of FastAPI routers in the backend orchestrator application. It covers router registration, URL prefix patterns, authentication dependencies, endpoint conventions, and response models.
For authentication and workspace isolation mechanisms, see Authentication Flow. For database models referenced by routers, see Database Models. For the main FastAPI application setup, see FastAPI Application.
Router Organization Overview
The Automatos AI backend organizes API endpoints into domain-based routers, each responsible for a specific feature area. Routers are modular Python files in the orchestrator/api/ directory that define related endpoints using FastAPI's APIRouter.
Router Categories
Core Agents
agents.py, agent_plugins.py, templates.py, personas.py
Agent lifecycle, configuration, plugins, and personalities
Workflows
workflows.py, workflow_recipes.py, workflow_templates.py
Workflow orchestration and recipe management
Tools & Skills
tools.py, skills.py, composio.py
External integrations, skill sources, Composio apps
Marketplace
marketplace.py, marketplace_plugins.py, admin_plugins.py, workspace_plugins.py
Plugin discovery, approval, and enablement
Context & Memory
context.py, memory.py, context_engineering.py, documents.py
Context assembly, memory management, RAG systems
System Admin
system.py, statistics.py, credentials.py, permissions.py
System configuration, metrics, credential management
Workspaces
workspaces.py, team.py
Multi-tenancy, workspace management, team collaboration
Routing
routing.py, chatbot_llm.py, chat.py
Universal routing, chat interfaces, streaming responses
Sources: orchestrator/main.py:36-119
Router Architecture
Sources: orchestrator/main.py:188-492, orchestrator/api/agents.py:31, orchestrator/api/agent_plugins.py:27
Router Registration in main.py
Routers are imported and registered in orchestrator/main.py following a specific order and pattern:
Import Pattern
Registration Pattern
Router Groups
Sources: orchestrator/main.py:36-89, orchestrator/main.py:411-479
Endpoint Pattern Conventions
Standard CRUD Pattern
Most routers follow a consistent RESTful CRUD pattern:
List
GET
/
list_agents()
Retrieve collection with filters
Create
POST
/
create_agent()
Create new resource
Get
GET
/{id}
get_agent()
Retrieve single resource
Update
PUT
/{id}
update_agent()
Update existing resource
Delete
DELETE
/{id}
delete_agent()
Delete resource
Nested Resource Pattern
For related resources, routers use nested paths:
Resource → Sub-resource
/api/agents/{agent_id}/skills
agents.py
Resource → Action
/api/agents/{agent_id}/execute
agents.py
Resource → Relationship
/api/agents/{agent_id}/plugins
agent_plugins.py
Resource → Computed
/api/agents/{agent_id}/assembled-context
agent_plugins.py
Sources: orchestrator/api/agents.py:437-553, orchestrator/api/agent_plugins.py:69-337
Authentication Dependencies
All routers use the hybrid authentication dependency get_request_context_hybrid to obtain authenticated request context.
Dependency Injection Pattern
Authentication Flow
RequestContext Structure
Sources: orchestrator/api/agents.py:26-27, orchestrator/api/agent_plugins.py:21-22
Response Model Patterns
Response Wrapping
Most endpoints wrap responses in a consistent structure:
{"data": [...]}
List endpoints
{"data": [agent1, agent2, ...]}
{"data": {...}}
Single resource
{"data": {"id": 1, "name": "..."}}
Direct model
Explicit response_model
AgentResponse(id=1, name=...)
{"items": [...], "total": N}
Paginated lists
Used by marketplace routers
Pydantic Response Models
Builder Functions
Routers use builder functions to convert ORM models to Pydantic responses:
Sources: orchestrator/api/agents.py:138-237, orchestrator/core/models/init.py:19-23
Common Query Patterns
Filtering and Pagination
Eager Loading
To avoid N+1 queries, routers use SQLAlchemy's eager loading:
Sources: orchestrator/api/agents.py:437-476, orchestrator/api/agents.py:534-552
Router Prefix Map
Complete mapping of router prefixes to their implementation files:
Complete Router List
/api/agents
agents.py
agents
Yes
/api/agents
agent_plugins.py
Agent Plugins
Yes
/api/workflows
workflows.py
workflows
Yes
/api/workflow-recipes
workflow_recipes.py
workflow-recipes
Yes
/api/marketplace
marketplace.py
marketplace
Yes
/api/marketplace
marketplace_plugins.py
Marketplace Plugins
Yes
/api/tools
tools.py
tools
Yes
/api/v1/skills
skills.py
skills-prd22
Yes
/api/personas
personas.py
Personas
Yes
/api/templates
templates.py
templates
Yes
/api/patterns
patterns.py
patterns
Yes
/api/credentials
credentials.py
credentials
Yes
/api/admin/plugins
admin_plugins.py
Admin Plugin Marketplace
Yes (Admin)
/api/workspaces
workspaces.py
workspaces
Yes
/api/system
system.py
system, statistics
Yes
Sources: orchestrator/main.py:411-479, orchestrator/api/agents.py:31, orchestrator/api/agent_plugins.py:27, orchestrator/api/skills.py:117, orchestrator/api/personas.py:30
Special Endpoint Patterns
Bulk Operations
Some routers provide bulk endpoints for batch operations:
Background Tasks
Long-running operations use FastAPI's BackgroundTasks:
File Upload Endpoints
File upload endpoints use UploadFile:
Sources: orchestrator/api/agents.py:296-357, orchestrator/api/skills.py:181-232
Error Handling
Routers use consistent error handling patterns:
Common HTTP Status Codes
200
Successful GET/PUT
Resource retrieved or updated
201
Successful POST
Resource created
400
Validation error
Invalid input data
401
Authentication failed
Missing or invalid token
403
Authorization failed
User lacks workspace access
404
Resource not found
Agent/workflow doesn't exist
500
Server error
Database connection failed
Sources: orchestrator/api/agents.py:534-552, orchestrator/api/agent_plugins.py:69-124
Tags and OpenAPI Documentation
Each router declares tags for OpenAPI documentation grouping:
The main application configures enhanced Swagger UI parameters:
Tag Hierarchy in Swagger UI
Sources: orchestrator/main.py:188-333, orchestrator/api/agents.py:31, orchestrator/api/agent_plugins.py:27
Last updated

