Authentication
All Notifications API methods require authentication via theAuthMiddleware. Include a valid bearer token in your requests.
Notification Structure
Notifications in Hazel Chat use a flexible resource-based model:- memberId: The organization member who should receive the notification
- targetedResourceId/Type: The primary resource (e.g., a message that mentions the user)
- resourceId/Type: Related secondary resource (e.g., the channel containing the message)
- readAt: Timestamp when notification was read (null if unread)
Methods
Create Notification
Create a new notification for an organization member. Used for mentions, reactions, system alerts, etc.ID of the organization member who should receive the notification
ID of the primary resource that triggered the notification (e.g., message ID)
Type of the targeted resource (e.g., “message”, “reaction”, “mention”)
ID of a related secondary resource (e.g., channel ID)
Type of the secondary resource (e.g., “channel”)
The created notification object
Transaction ID for optimistic updates
UnauthorizedError- User lacks permission to create notificationsInternalServerError- Unexpected server error
- Policy checks ensure users can only create notifications for authorized members
- Performed within a database transaction
Update Notification
Update an existing notification. Typically used to mark notifications as read.ID of the notification to update
Timestamp when notification was read. Set to mark as read, or null to mark as unread.
The updated notification object
Transaction ID for optimistic updates
NotificationNotFoundError- Notification with the specified ID doesn’t existUnauthorizedError- User lacks permission to update this notificationInternalServerError- Unexpected server error
- Only the notification owner or authorized users can update
- Performed within a database transaction
- Policy checks ensure proper authorization
Delete Notification
Delete a notification. Only the notification owner or users with appropriate permissions can delete.ID of the notification to delete
Transaction ID for optimistic updates
NotificationNotFoundError- Notification with the specified ID doesn’t existUnauthorizedError- User lacks permission to delete this notificationInternalServerError- Unexpected server error
- Performed within a database transaction
- Policy checks ensure only authorized users can delete
Delete Notifications by Message IDs
Bulk delete notifications for the current user by message IDs. Used when messages become visible in the viewport to automatically clear their notifications.Array of message IDs to clear notifications for
ID of the channel containing the messages (used for authorization)
Number of notifications that were deleted
Transaction ID for optimistic updates
UnauthorizedError- User is not a member of the channel’s organizationInternalServerError- Unexpected server error
- Automatically determines the organization member from the current user and channel
- Only deletes notifications belonging to the current user
- If
messageIdsis empty, returns immediately with deletedCount of 0 - Verifies channel exists and user is an organization member
- Performed within a database transaction
Usage Patterns
Common Use Cases
Message Mentions
Create notifications when users are mentioned in messages:Message Reactions
Notify users when someone reacts to their message:System Alerts
Send system notifications to users:Error Handling
Related APIs
- Users API - Manage user accounts
- User Presence Status API - User status affects notification delivery
- Messages API - Messages often trigger notifications