Skip to main content
API rules provide declarative, expression-based access control for your PocketBase collections. They determine who can list, view, create, update, and delete records through the API.

Overview

Each collection has five API rule types:
  • ListRule - Controls access to list/search operations
  • ViewRule - Controls access to viewing individual records
  • CreateRule - Controls who can create new records
  • UpdateRule - Controls who can update existing records
  • DeleteRule - Controls who can delete records

Rule syntax

API rules use a simple expression language:
Rules use nil (not set) to deny all access, empty string "" to allow all access, and expressions for conditional access.

Request context

Rules have access to request information via @request:

Authentication

Request data

Request method

Query parameters

Headers

Record fields

Rules can reference record fields directly:

Operators

Comparison operators

Logical operators

String operators

Array operators

Relation operators

Special variables

@now

Current date/time:

@todayStart / @todayEnd

Today’s date boundaries:

@monthStart / @monthEnd

Current month boundaries:

@yearStart / @yearEnd

Current year boundaries:

Common patterns

Public read, authenticated write

Owner-only access

Published content

Admin override

Verified users only

Prevent self-deletion

Rule validation

Validate rules before saving:

Testing rules

Test rules programmatically:

CreateRule specifics

CreateRule has special behavior for owner fields:
This ensures users can only create records assigned to themselves.

UpdateRule specifics

UpdateRule evaluates against the original record state:

Performance considerations

Create database indexes for fields used in API rules to improve query performance.
Deep relation traversals (e.g., post.author.team.organization.owner) can be slow. Keep rules simple when possible.
Specific rules like author = @request.auth.id are faster than complex OR conditions.
For CreateRule and UpdateRule, validate submitted data efficiently by checking required fields first.

Security best practices

Begin with strict rules (nil) and gradually relax them as needed. It’s easier to grant access than to remove it.
Always verify record ownership in UpdateRule and DeleteRule to prevent unauthorized modifications.
Use field-level visibility settings alongside API rules to hide sensitive data.
Test all rule combinations with different user roles and authentication states.
Log and review changes to API rules, especially for production collections.
Manage rules grant elevated permissions. Apply them conservatively and validate admin status properly.
Ensure ListRule and ViewRule don’t expose records users shouldn’t see, even if they guess IDs.

Advanced examples

Multi-tenant system

Time-based access

Role-based permissions

Collaborative editing

Draft vs published

Debugging rules

Enable query logging to debug rule evaluation:
Use the PocketBase Admin UI to test API rules interactively. The “API Preview” feature shows exactly how rules affect API responses.