The API, routes and scopes
Add an endpoint that is registered, documented, scoped and consistent with the existing API.
The API is a JSON HTTP API under /api, authenticated with a Bearer token. Roles map to scopes, and each mutating handler is wrapped in a scope check that returns 403 with insufficient permissions when the scope is missing.
Adding an endpoint
- Write the handler in cmd/server.
- Register the route in main.go with the correct scope wrapper.
- Add the route to the apiRoutes index so /api/openapi.json stays accurate.
- Return the project's error shape, not a bare string.
- Test as a viewer: the write must be rejected, not silently ignored.
The API index
GET /api/openapi.json serves an OpenAPI 3.0.3 document generated from the route index. The index is maintained by hand next to the registrations, which means a new route that is missing from the index is invisible to integrators. Treat the index as part of the feature.
Scopes
Scopes are fine-grained: wo:write, wo:schedule, equipment:write, pm:write, inventory:write, users:manage, system:configure, ai:use and so on. A handler that mutates must name the narrowest scope that covers the action. Do not reuse a broad scope because it is convenient.
Pagination and caps
List endpoints cap their result sets and report whether the result was truncated together with the total. An API that silently returns the first hundred rows teaches integrators to trust an incomplete answer.
The OpenAPI document is the contract. If the handler and the index disagree, the index is wrong and integrators will build against the wrong thing.
Add one read endpoint, register it, add it to the index, and fetch /api/openapi.json to confirm it appears with the right method and path.
Terms
- Bearer token
- An authorization token sent in the Authorization header.
- Scope
- A permission string checked by the server before a handler runs.
OSMAINT Academy · Developer · The API, routes and scopes · osmaint.org/academy/