Overview
Guard functions are serverless Appwrite functions that act as validation and moderation layers between client applications and the database. They enforce business logic, rate limiting, authorization, and data validation before allowing operations to proceed. All guard functions:- Run on Node.js 22 runtime
- Have 15 second timeout
- Use s-0.5vcpu-512mb specification
- Execute synchronously (
async: false) - Require user authentication via
x-appwrite-user-idheader
Function List
| Function | Purpose | Rate Limit |
|---|---|---|
posts-guard | Manage post creation, updates, deletion, likes, and views | 10 req/min per action |
comments-guard | Manage comment operations | 60 req/min per action |
rooms-guard | Manage chat room lifecycle | 10 req/min per action |
room-message-guard | Handle live chat messages | 60 req/min |
notifications-guard | Fetch user notifications | No rate limit |
leaderboard-guard | Retrieve user rankings | No rate limit |
user-push-token-guard | Manage push notification tokens | No rate limit |
Common Configuration
All guard functions share these configuration settings fromappwrite.config.json:
posts-guard
Function ID:6967383500111e42ec97
Purpose
Manages all post-related operations including creation, updates, deletion, likes, and view tracking with rate limiting and authorization.Actions
create
Creates a new post with user validation and rate limiting. Rate Limit: 10 requests per minute per user Request Body:- Read: All authenticated users
- Update: Post author only
- Delete: Post author only
update
Updates existing post content and images. Rate Limit: 10 requests per minute per user Authorization: Must be post author Request Body:delete
Removes a post from the database. Rate Limit: 10 requests per minute per user Authorization: Must be post author Request Body:like
Toggles like status on a post (like/unlike). Rate Limit: None Request Body:- If user already liked: Decrements likes count and removes user from
likedBy - If user hasn’t liked: Increments likes count and adds user to
likedBy
view
Records a unique view from a user. Rate Limit: None Request Body:- Only increments view count once per user (checks
viewedByarray) - Does not increment if user already viewed the post
Client Usage
Fromservices/posts.service.ts:
comments-guard
Function ID:697231ae0003e880b55b
Purpose
Handles comment creation, updates, and deletion with validation and rate limiting.Actions
add
Creates a new comment on a post. Rate Limit: 60 requests per minute per user Request Body:- Content must be a non-empty string
- Post ID must be provided and valid
- Read: All authenticated users
- Update: Comment author only
- Delete: Comment author only
update
Modifies an existing comment. Rate Limit: 60 requests per minute per user Authorization: Must be comment author Request Body:- Sets
isEdited: trueon the comment - Validates ownership before updating
delete
Removes a comment. Rate Limit: 60 requests per minute per user Authorization: Must be comment author Request Body:Client Usage
Fromservices/comments.service.ts:
rooms-guard
Function ID:6966ec4f00039df360a0
Purpose
Manages cricket match chat rooms with time-based status validation.Actions
create
Creates a new chat room for a cricket match. Rate Limit: 10 requests per minute per user Request Body:- Read: All authenticated users
- Update: Room creator only
- Delete: Room creator only
update
Updates room details. Rate Limit: 10 requests per minute per user Authorization: Must be room creator Validation:- Cannot update if room status is “finished”
- Status is calculated based on current time vs start/end times:
finished: Current time > end timeupcoming: Current time < start timelive: Between start and end time
delete
Deletes a chat room. Rate Limit: 10 requests per minute per user Authorization: Must be room creator Request Body:Client Usage
Fromservices/rooms.service.ts:
room-message-guard
Function ID:695e8e91000d6c2bde1f
Purpose
Handles real-time chat messages in live cricket match rooms with push notifications.Actions
create
Sends a message in a live room. Rate Limit: 60 requests per minute per user Validation:- Room must have status “live” (between start and end time)
- User must be authenticated
- Creates message with
isEdited: false - Increments user’s
messageCountin users table - Sends push notification to room creator (if not the sender)
- Stores notification in notifications table
- Read: All authenticated users
- Update: Message author only
- Delete: Message author only
update
Edits an existing message. Rate Limit: 60 requests per minute per user Authorization: Must be room owner Validation:- Room must be “live”
delete
Deletes a message from the room. Rate Limit: 60 requests per minute per user Authorization: Must be room owner Validation:- Room must be “live”
notifications-guard
Function ID:6970a9c400040f0cb857
Purpose
Retrieves user-specific notifications with no rate limiting.Actions
fetchByUserId
Fetches all notifications for the authenticated user. Rate Limit: None Request Body:- Automatically filters by authenticated user’s ID
- Results ordered by creation date (newest first)
leaderboard-guard
Function ID:6963302c0021374fee8d
Purpose
Retrieves top 10 users ranked by message count.Execution
Rate Limit: None Authentication: Required but no user-specific filtering Request: No action parameter needed Response:- Orders by
messageCountdescending - Limits to 10 results
user-push-token-guard
Function ID:69704fe8001df0792591
Purpose
Manages push notification tokens for mobile devices.Actions
send
Adds or updates a push token for the authenticated user. Rate Limit: None Request Body:- Uses
upsertRowto create or update user entry - Avoids duplicate tokens (checks before adding)
- Maintains array of all user’s device tokens
delete
Removes a specific push token. Rate Limit: None Request Body:- Filters out the specified token from user’s
pushTokensarray - Preserves other tokens
Error Handling
All guard functions throw errors in these scenarios:Unauthorized (401)
Forbidden (403)
Rate Limit Exceeded (429)
Validation Errors (400)
Generic Error Response
Authentication
All guard functions require:- User ID Header:
x-appwrite-user-idfrom Appwrite session - API Key Header:
x-appwrite-keyfor server-side permissions
Best Practices
Synchronous Execution
Always useasync: false for guard functions to ensure operations complete before returning to the client:
Error Checking
Always check execution status:Rate Limit Awareness
Design UI to prevent users from hitting rate limits:- Disable submit buttons temporarily after actions
- Show cooldown timers for high-frequency actions
- Batch operations when possible