Skip to main content

Overview

The VersionedAPIRouter 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:
  1. Runtime mode - With serveEvent for actual HTTP routing
  2. Docs-only mode - Without serveEvent for build-time spec generation

HTTP Method Registration

All HTTP methods return a *VersionedRouteChain for middleware binding.

GET

Registers a GET route with automatic documentation. Location: core/server/api/version_manager.go:358 Example:

POST

Registers a POST route with automatic documentation. Location: core/server/api/version_manager.go:377

PUT

Registers a PUT route with automatic documentation. Location: core/server/api/version_manager.go:434

PATCH

Registers a PATCH route with automatic documentation. Location: core/server/api/version_manager.go:395

DELETE

Registers a DELETE route with automatic documentation. Location: core/server/api/version_manager.go:414

Prefixed Router

SetPrefix

Creates a prefixed router that automatically prepends a path prefix to all registered routes.
string
required
Path prefix to prepend (e.g., “/api/v1”)
Returns:
  • *PrefixedRouter - Router with automatic path prefixing
Location: core/server/api/version_manager.go:453 Example:

PrefixedRouter Type

PrefixedRouter Methods

All HTTP methods work identically to VersionedAPIRouter but automatically prepend the prefix:
  • GET(path, handler) - Location: core/server/api/version_manager.go:467
  • POST(path, handler) - Location: core/server/api/version_manager.go:472
  • PUT(path, handler) - Location: core/server/api/version_manager.go:477
  • PATCH(path, handler) - Location: core/server/api/version_manager.go:482
  • DELETE(path, handler) - Location: core/server/api/version_manager.go:487

CRUD

Registers standard CRUD routes for a resource with optional authentication.
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)
Location: core/server/api/version_manager.go:492 Registered Routes:
  • GET /{resource} - List
  • POST /{resource} - Create (with auth)
  • GET /{resource}/{id} - Get
  • PUT /{resource}/{id} - Update (with auth)
  • PATCH /{resource}/{id} - Patch (with auth)
  • DELETE /{resource}/{id} - Delete (with auth)
Example:

Route Chain (Middleware Binding)

Bind

Binds middleware to the route. Accepts both *hook.Handler[*core.RequestEvent] and plain func(*core.RequestEvent) error.
...interface{}
required
Middleware handlers (hooks or plain functions)
Behavior:
  1. Stores middlewares for documentation analysis
  2. Re-registers route with middleware information in registry
  3. Binds middleware to actual PocketBase route for runtime execution
Location: core/server/api/version_manager.go:563 Example:

BindFunc

Binds plain middleware functions to the route. Ergonomic counterpart to 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

  1. Use SetPrefix: Always use SetPrefix() for cleaner route definitions
  2. Middleware on Mutations: Apply auth middleware to POST/PUT/PATCH/DELETE operations
  3. CRUD for Resources: Use CRUD() for standard REST resources to reduce boilerplate
  4. Path Parameters: Use {param} syntax for path parameters (auto-detected in docs)
  5. Consistent Naming: Use plural nouns for resources (/users, not /user)
  6. 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) or json.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
Example:
This automatically generates OpenAPI documentation with:
  • Request body schema from CreateUserRequest
  • Response schema from User
  • Tags: users, authentication
  • Description: “Creates a new user account”