Skip to main content

APIVersionManager Concept

The APIVersionManager coordinates multiple API versions, each with its own:
  • APIRegistry — stores endpoints and generates OpenAPI specs
  • ASTParser — analyzes Go source for that version’s handlers
  • SchemaGenerator — generates component schemas
  • Configuration — title, description, status, public Swagger access
This isolation means v1 and v2 can have completely different endpoints, schemas, and documentation.
Each version gets a separate OpenAPI 3.0.3 spec. Component schemas are pruned per-version — only schemas referenced by that version’s endpoints are included.

VersionedAPIRouter Usage

The VersionedAPIRouter wraps PocketBase’s router and provides version-aware route registration. It automatically:
  • Registers routes to both the runtime router and the documentation registry
  • Tracks request/response schemas via AST analysis
  • Handles middleware binding (.Bind() and .BindFunc())
  • Extracts path parameters from route patterns

Basic Example

Each method (GET, POST, PUT, PATCH, DELETE) returns a *VersionedRouteChain that supports middleware binding.

Managing Multiple API Versions

Version Configurations

APIDocsConfig holds version-specific settings:

Version Status Levels

The Status field is informational only — it appears in the OpenAPI spec’s server description but doesn’t affect routing or access control.

Real Example from routes.go

Here’s how the demo app initializes a versioned system:

Server Registration

The version manager must be registered with the PocketBase app to serve endpoints:
This sets up:
  • All version routes (e.g., /api/v1/todos, /api/v2/time)
  • Version listing endpoint (/api/docs/versions)
  • Per-version OpenAPI endpoints (/api/docs/v1, /api/docs/v2)
  • Public Swagger UI endpoints (if PublicSwagger: true)
  • Debug AST endpoint (/api/docs/debug/ast)

Public vs Private Swagger Access

Public Swagger (PublicSwagger: true)

Exposes two endpoints without authentication:
Ideal for public APIs where you want developers to explore the documentation without creating an account.

Private Swagger (PublicSwagger: false)

Requires superuser authentication to access:
If PublicSwagger: false, the /swagger endpoint is not registered at all. Use the authenticated /api/docs/v2 endpoint to retrieve the spec programmatically.

Version Endpoints Reference

List All Versions

Get Version-Specific OpenAPI Spec

Both return the same OpenAPI 3.0.3 JSON spec.

Access Swagger UI

Only available if PublicSwagger: true. Returns an HTML page with embedded Swagger UI (dark mode CSS included).

Advanced Configuration

Dynamic Server URLs

The system automatically constructs server URLs based on the request:
If BaseURL is not localhost, the configured value is used. This ensures production specs use the correct domain.

Custom Version Validation

Version strings are validated when registering:
Valid formats: v1, v2, v1.0, v2.0.0, etc.

Migration Workflow

When introducing a new API version:
1

Create new version config

Set Status: "testing" and PublicSwagger: false initially.
2

Register routes

Create a new registerV2Routes() function and add it to the version manager.
3

Test internally

Use the authenticated OpenAPI endpoint to validate the spec.
4

Enable public access

Set PublicSwagger: true and Status: "stable" when ready.
5

Deprecate old version

Update v1 to Status: "deprecated" and add a sunset date to the description.

Next Steps

Route Registration

Learn manual and CRUD registration patterns

Annotations Reference

Complete guide to source file directives