Notification Routes
Table of Contents
- Overview
- Send Notifications
- Get User Notifications
- Mark Notifications as Seen
- Delete Notifications
- Get Notification Statistics
- Examples
Overview
The Notification API provides comprehensive user notification functionality. It allows you to send notifications to users, manage notification status, and retrieve notification statistics.
Base URL: /notifications
Authentication: All notification endpoints require authentication.
Send Notifications
Send notifications to one or more users.
- URL:
/notifications/send - Method:
POST - Auth required: Yes
Request Body
| Parameter | Type | Required | Description |
|---|---|---|---|
| type | string | Yes | Notification type (e.g., 'info', 'warning', 'success') |
| title | string | Yes | Notification title |
| message | string | Yes | Notification message content |
| data | object | No | Additional data payload |
| userIds | array | Yes | Array of user IDs to send notification to |
| tenant_Id | string | No | Tenant ID for multi-tenant setup |
Success Response
{
"data": {
"notificationIds": [
"uuid-1",
"uuid-2",
"uuid-3"
],
"message": "Notifications sent successfully",
"count": 3
}
}Example Request
curl -X POST http://localhost:3000/notifications/send \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>" \
-d '{
"type": "info",
"title": "System Update",
"message": "The system will be updated tonight at 11 PM.",
"data": {
"updateVersion": "1.2.3",
"maintenanceWindow": "11:00 PM - 12:00 AM"
},
"userIds": ["user-id-1", "user-id-2", "user-id-3"]
}'Get User Notifications
Retrieve notifications for the current user or a specific user.
- URL:
/notifications - Method:
GET - Auth required: Yes
Query Parameters
| Parameter | Type | Description |
|---|---|---|
| userId | string | Get notifications for specific user (admin only) |
| seen | boolean | Filter by seen status (true/false) |
| type | string | Filter by notification type |
| limit | number | Maximum number of notifications to return |
| page | number | Page number for pagination |
| sort | string | Sort order ('createdAt:desc' or 'createdAt:asc') |
Success Response
{
"data": [
{
"id": "notification-id",
"type": "info",
"title": "System Update",
"message": "The system will be updated tonight at 11 PM.",
"data": {
"updateVersion": "1.2.3",
"maintenanceWindow": "11:00 PM - 12:00 AM"
},
"seen": false,
"userId": "user-id",
"createdAt": "2025-01-15T10:30:00.000Z",
"updatedAt": "2025-01-15T10:30:00.000Z"
}
],
"totalCount": 15,
"unseenCount": 3
}Example Request
# Get all notifications for current user
curl -X GET "http://localhost:3000/notifications" \
-H "Authorization: Bearer <token>"
# Get only unseen notifications
curl -X GET "http://localhost:3000/notifications?seen=false" \
-H "Authorization: Bearer <token>"
# Get notifications with pagination
curl -X GET "http://localhost:3000/notifications?limit=10&page=1" \
-H "Authorization: Bearer <token>"Mark Notifications as Seen
Mark one or more notifications as seen.
- URL:
/notifications/seen - Method:
PATCH - Auth required: Yes
Request Body
| Parameter | Type | Required | Description |
|---|---|---|---|
| notificationIds | array | Yes | Array of notification IDs to mark as seen |
Success Response
{
"data": {
"message": "Notifications marked as seen",
"updatedCount": 3
}
}Example Request
curl -X PATCH http://localhost:3000/notifications/seen \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>" \
-d '{
"notificationIds": ["notification-id-1", "notification-id-2"]
}'Delete Notifications
Delete one or more notifications.
- URL:
/notifications - Method:
DELETE - Auth required: Yes
Request Body
| Parameter | Type | Required | Description |
|---|---|---|---|
| notificationIds | array | Yes | Array of notification IDs to delete |
Success Response
{
"data": {
"message": "Notifications deleted successfully",
"deletedCount": 2
}
}Example Request
curl -X DELETE http://localhost:3000/notifications \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>" \
-d '{
"notificationIds": ["notification-id-1", "notification-id-2"]
}'Get Notification Statistics
Get notification statistics for the current user.
- URL:
/notifications/stats - Method:
GET - Auth required: Yes
Success Response
{
"data": {
"total": 50,
"seen": 35,
"unseen": 15,
"byType": {
"info": 30,
"warning": 15,
"success": 5
},
"recent": {
"today": 3,
"thisWeek": 8,
"thisMonth": 22
}
}
}Example Request
curl -X GET "http://localhost:3000/notifications/stats" \
-H "Authorization: Bearer <token>"Examples
Complete Notification Workflow
// 1. Send a notification to multiple users
const sendResponse = await fetch('/notifications/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: JSON.stringify({
type: 'info',
title: 'Welcome!',
message: 'Welcome to our platform. Get started by exploring the features.',
data: {
actionUrl: '/dashboard',
priority: 'normal'
},
userIds: ['user-1', 'user-2']
})
});
// 2. Get user notifications
const notifications = await fetch('/notifications?limit=20', {
headers: { 'Authorization': 'Bearer ' + token }
});
// 3. Mark notifications as seen
await fetch('/notifications/seen', {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: JSON.stringify({
notificationIds: ['notif-1', 'notif-2']
})
});
// 4. Get notification statistics
const stats = await fetch('/notifications/stats', {
headers: { 'Authorization': 'Bearer ' + token }
});Real-time Notification Integration
When using Socket.IO, notifications are automatically sent to connected users:
// Client-side Socket.IO listener
socket.on('notification.new', (notification) => {
console.log('New notification received:', notification);
// Update UI to show new notification
showNotificationBadge(notification);
// Show toast/popup
showToast(notification.title, notification.message);
});
// Listen for notification updates
socket.on('notification.seen', (data) => {
console.log('Notification marked as seen:', data.notificationId);
updateNotificationUI(data.notificationId, { seen: true });
});Error Responses
Common Error Codes
| Code | Message | Description |
|---|---|---|
| 400 | Invalid notification parameters | Missing required fields |
| 401 | Unauthorized | Authentication required |
| 403 | Forbidden | Insufficient permissions |
| 404 | Notification not found | Notification ID doesn't exist |
| 422 | Validation failed | Invalid data format or values |
Example Error Response
{
"error": {
"message": "Invalid notification parameters",
"code": "VALIDATION_ERROR",
"details": {
"field": "userIds",
"message": "userIds must be a non-empty array"
}
}
}Related Documentation
Real-time Delivery
- Socket.IO Integration - Real-time notification delivery via WebSocket
Authentication
- Authentication Routes - User authentication
- Permission Routes - Notification access control
Automation
- Hooks System - Send notifications from hooks
- Hooks and Endpoints Guide - Notification service in hooks
- Workflow Routes - Send notifications from workflows
Multi-tenant
- Multi-tenant Guide - Tenant-scoped notifications
Reference
- API Routes Reference - Complete endpoint listing
- Integration Guide - Client-side notification handling