REST API & Routing Registry
Relevant source files
The following files were used as context for generating this wiki page:
The REST API and Routing Registry provides a structured mechanism for defining, registering, and managing web endpoints within the Superglue project. It centralizes route definitions and permission requirements, enabling consistent API behavior across different modules such as system documentation, tool execution, and authentication.
This system facilitates the modular expansion of the API by allowing separate features to register their own routes through a unified registry. It uses Fastify for request handling and implements a standardized approach to authentication, request validation, and error reporting.
Sources: CLAUDE.md, packages/core/api/registry.ts
Architecture and Registry Pattern
The project employs a registration pattern to decouple API module definitions from the core server logic. Developers add new endpoints by creating a module and calling registerApiModule. This function accepts a configuration object containing the module name and an array of route definitions.
API Module Registration Flow
The registration process involves defining a RouteHandler and specifying metadata such as HTTP methods, paths, and required permissions.
Sources: CLAUDE.md, packages/core/api/registry.ts
Key Data Structures
The registry utilizes specific types to ensure type safety and consistent request handling across the backend.
| Type | Description |
|---|---|
RouteHandler | An asynchronous function: (request, reply) => Promise<any>. |
AuthenticatedFastifyRequest | An extension of the standard request containing authInfo, datastore, workerPools, and traceId. |
RoutePermission | Defines the access requirements, including type (read/write/delete), resource, and allowedBaseRoles. |
Sources: CLAUDE.md
Implementation of Documentation Endpoints
The systems-documentation module demonstrates the registry pattern by providing endpoints for managing external system integration data. It handles searching documentation, fetching OpenAPI specifications, and triggering web scrapes.
Endpoint Definitions
The following table summarizes the routes registered for system documentation:
| Method | Path | Resource | Permissions |
|---|---|---|---|
| POST | /systems/:systemId/documentation/search | system | read (admin, member, enduser) |
| POST | /systems/:systemId/documentation/openapi | system | write (admin, member) |
| POST | /systems/:systemId/documentation/scrape | system | write (admin, member) |
| POST | /systems/:systemId/file-references | system | write (admin, member) |
| GET | /systems/:systemId/file-references | system | read (admin, member, enduser) |
| DELETE | /systems/:systemId/file-references/:fileId | system | delete (admin, member) |
Sources: packages/core/api/systems-documentation.ts:468-525
OpenAPI Fetching Sequence
When an OpenAPI spec is requested via the REST API, the system attempts multiple discovery strategies before persisting the result.
Request Handling and Validation Helpers
A suite of utility functions supports the API registry to ensure data integrity and standardized responses.
Error Handling
The system uses the sendError helper to return standardized error objects. It requires a status code and a message, and automatically attaches the X-Trace-Id for debugging. Developers are instructed never to throw raw errors in handlers.
Sources: CLAUDE.md, packages/core/api/response-helpers.ts:121-125
Validation Utilities
The API enforces strict validation for request bodies through specialized helpers:
validateScrapeRequestBody: Ensures a URL is provided and validates it against external URL constraints.validateUploadDocumentationBody: Restricts file arrays to between 1 and 20 files and checks for valid extensions.validateCreateSystemBody: Normalizes and checks for required fields likeid,name, andurl.
Sources: packages/core/api/response-helpers.ts:34-100, packages/core/api/response-helpers.ts:175-195
Superglue Client Integration
The SuperglueClient serves as the primary TypeScript interface for interacting with the REST API. It wraps standard fetch requests with authentication headers and provides methods mapping to the registry's endpoints.
Request Pipeline
The client implements a restRequest method that handles:
- Endpoint resolution using
apiEndpoint. - Authorization via Bearer tokens.
- Automatic
Content-Type: application/jsonheaders for non-GET requests. - Infrastructure error detection (e.g., 5xx status codes or fetch failures).
Sources: packages/shared/superglue-client.ts:41-86
Standardized Execution Methods
The client facilitates tool and system operations through specialized methods:
// Example: Executing a tool via REST API
const result = await client.runTool({
toolId: "my-tool",
payload: { key: "value" },
options: { async: false }
});
Sources: packages/shared/superglue-client.ts:98-132
Summary
The REST API and Routing Registry provides a scalable foundation for the Superglue backend. By combining a centralized registration pattern with strict permission enforcement and standardized request helpers, it ensures that features like system documentation and tool execution remain consistent and maintainable as the project grows.