Overview
TheVersionedAPIRouter provides version-specific route registration with automatic documentation generation. It wraps PocketBase’s router while maintaining isolated registries for each API version.
Type Definition
The
VersionedAPIRouter can operate in two modes:- Runtime mode - With
serveEventfor actual HTTP routing - Docs-only mode - Without
serveEventfor build-time spec generation
HTTP Method Registration
All HTTP methods return a*VersionedRouteChain for middleware binding.
GET
core/server/api/version_manager.go:358
Example:
POST
core/server/api/version_manager.go:377
PUT
core/server/api/version_manager.go:434
PATCH
core/server/api/version_manager.go:395
DELETE
core/server/api/version_manager.go:414
Prefixed Router
SetPrefix
string
required
Path prefix to prepend (e.g., “/api/v1”)
*PrefixedRouter- Router with automatic path prefixing
core/server/api/version_manager.go:453
Example:
PrefixedRouter Type
PrefixedRouter Methods
All HTTP methods work identically toVersionedAPIRouter but automatically prepend the prefix:
GET(path, handler)- Location:core/server/api/version_manager.go:467POST(path, handler)- Location:core/server/api/version_manager.go:472PUT(path, handler)- Location:core/server/api/version_manager.go:477PATCH(path, handler)- Location:core/server/api/version_manager.go:482DELETE(path, handler)- Location:core/server/api/version_manager.go:487
CRUD
string
required
Resource name (e.g., “users”, “posts”)
CRUDHandlers
required
CRUD operation handlers
...interface{}
Optional auth middleware applied to mutating operations (Create, Update, Patch, Delete)
core/server/api/version_manager.go:492
Registered Routes:
GET /{resource}- ListPOST /{resource}- Create (with auth)GET /{resource}/{id}- GetPUT /{resource}/{id}- Update (with auth)PATCH /{resource}/{id}- Patch (with auth)DELETE /{resource}/{id}- Delete (with auth)
Route Chain (Middleware Binding)
Bind
*hook.Handler[*core.RequestEvent] and plain func(*core.RequestEvent) error.
...interface{}
required
Middleware handlers (hooks or plain functions)
- Stores middlewares for documentation analysis
- Re-registers route with middleware information in registry
- Binds middleware to actual PocketBase route for runtime execution
core/server/api/version_manager.go:563
Example:
BindFunc
Bind().
Location: core/server/api/version_manager.go:594
Example:
Complete Examples
Basic Route Registration
Using Prefixed Router
CRUD Resource Registration
Middleware Chaining
Multi-Version API
Best Practices
- Use SetPrefix: Always use
SetPrefix()for cleaner route definitions - Middleware on Mutations: Apply auth middleware to POST/PUT/PATCH/DELETE operations
- CRUD for Resources: Use
CRUD()for standard REST resources to reduce boilerplate - Path Parameters: Use
{param}syntax for path parameters (auto-detected in docs) - Consistent Naming: Use plural nouns for resources (
/users, not/user) - Version Prefixes: Include version in path prefix (
/api/v1)
Automatic Documentation
The router automatically extracts documentation from your code:- Request Body: Detected from
c.BindBody(&req)orjson.Decode - Response Schema: Detected from
c.JSON(status, response) - Path Parameters: Extracted from
{param}patterns - Query Parameters: Detected from
e.Request.URL.Query().Get("param") - Auth Requirements: Detected from
apis.RequireAuth()middleware
- Request body schema from
CreateUserRequest - Response schema from
User - Tags:
users,authentication - Description: “Creates a new user account”
Related
- APIVersionManager - Multi-version API management
- APIRegistry - Endpoint registry and spec generation
- Middleware Guide - Creating custom middleware