Skip to main content

Two Registration Approaches

pb-ext provides two ways to register API routes:
  1. Manual Registration — explicit control over each route
  2. CRUD Helper — convention-based registration for resource routes
Both approaches automatically register routes to:
  • 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:
When to use .BindFunc():
  • You have simple middleware functions (not hook handlers)
  • You want cleaner syntax without manual wrapping
  • Middleware doesn’t need hook priority or ID
.BindFunc() is only for func(*core.RequestEvent) error. Hook handlers must use .Bind().

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:
This registers:
Middleware (third argument) is applied only to mutating operations: Create, Update, Patch, and Delete. Read operations (List, Get) remain public.

CRUDHandlers Structure

All fields are optional. Omit handlers you don’t need:

Multiple Auth Middlewares

Variadic arguments allow any number of middlewares.

Path Parameters with Syntax

Both manual and CRUD registration support path parameters:
Path parameter detection:
  • AST parser extracts parameters from c.Request.PathValue("id") calls
  • Parameters are automatically marked as required: true in OpenAPI spec
  • Multiple path params are supported (e.g., /users/{userID}/posts/{postID})
OpenAPI spec will include:

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