Two Registration Approaches
pb-ext provides two ways to register API routes:- Manual Registration — explicit control over each route
- CRUD Helper — convention-based registration for resource routes
- PocketBase’s router (for runtime handling)
- The documentation registry (for OpenAPI spec generation)
You can mix both approaches in the same application. Use manual registration for custom endpoints and CRUD helpers for standard resource operations.
Manual Route Registration
Basic Usage
HTTP Methods
All methods return a
*VersionedRouteChain that supports middleware binding.
Middleware Binding
.Bind() — Hook Handlers
Bind PocketBase hook handlers or plain functions:
.Bind() accepts both *hook.Handler[*core.RequestEvent] and func(*core.RequestEvent) error. Plain functions are automatically wrapped in a hook handler..BindFunc() — Plain Functions Only
Ergonomic alternative to .Bind() when you only have plain functions:
.BindFunc():
- You have simple middleware functions (not hook handlers)
- You want cleaner syntax without manual wrapping
- Middleware doesn’t need hook priority or ID
SetPrefix Usage
Reduce repetition by setting a path prefix:SetPrefix() returns a *PrefixedRouter that automatically prepends the prefix to all paths.
CRUD Convenience Method
Basic Usage
Register all standard resource routes in one call:Middleware (third argument) is applied only to mutating operations:
Create, Update, Patch, and Delete. Read operations (List, Get) remain public.CRUDHandlers Structure
Multiple Auth Middlewares
Path Parameters with Syntax
Both manual and CRUD registration support path parameters:- AST parser extracts parameters from
c.Request.PathValue("id")calls - Parameters are automatically marked as
required: truein OpenAPI spec - Multiple path params are supported (e.g.,
/users/{userID}/posts/{postID})
Example Handler with Path Parameter
Example Handler with Path Parameter
Full Example from routes.go
Here’s the complete registration code from the demo app:Comparison: Manual vs CRUD
Most applications use both:
- CRUD helper for standard resources (
/todos,/users,/posts) - Manual registration for custom endpoints (
/auth/login,/stats/summary,/webhooks/stripe)
Advanced Patterns
Nested Resources
Conditional Middleware
Custom Resource Names
Best Practices
1
Use SetPrefix for version isolation
Always prefix routes with
/api/v1, /api/v2, etc.2
Choose the right pattern
CRUD for standard resources, manual for custom logic.
3
Apply auth to mutating operations
Protect
POST, PUT, PATCH, DELETE with middleware.4
Extract common middleware
Define reusable middleware functions for auth, logging, rate limiting.
5
Document handlers with directives
Add
// API_DESC and // API_TAGS for better OpenAPI docs.Next Steps
Annotations Reference
Complete guide to source file directives
OpenAPI System
Deep dive into AST parsing and schema detection