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:UpdateRule specifics
UpdateRule evaluates against the original record state:Performance considerations
Index filtered fields
Index filtered fields
Create database indexes for fields used in API rules to improve query performance.
Avoid complex relation chains
Avoid complex relation chains
Deep relation traversals (e.g.,
post.author.team.organization.owner) can be slow. Keep rules simple when possible.Use specific rules over wildcards
Use specific rules over wildcards
Specific rules like
author = @request.auth.id are faster than complex OR conditions.Optimize @request.data checks
Optimize @request.data checks
For CreateRule and UpdateRule, validate submitted data efficiently by checking required fields first.
Security best practices
Start restrictive
Start restrictive
Begin with strict rules (nil) and gradually relax them as needed. It’s easier to grant access than to remove it.
Validate ownership
Validate ownership
Always verify record ownership in UpdateRule and DeleteRule to prevent unauthorized modifications.
Protect sensitive fields
Protect sensitive fields
Use field-level visibility settings alongside API rules to hide sensitive data.
Test rules thoroughly
Test rules thoroughly
Test all rule combinations with different user roles and authentication states.
Audit rule changes
Audit rule changes
Log and review changes to API rules, especially for production collections.
Use manage rules carefully
Use manage rules carefully
Manage rules grant elevated permissions. Apply them conservatively and validate admin status properly.
Prevent data leakage
Prevent data leakage
Ensure ListRule and ViewRule don’t expose records users shouldn’t see, even if they guess IDs.