BaasixBaasix

Notification Routes

← Back to Documentation Home

Table of Contents

  1. Overview
  2. Send Notifications
  3. Get User Notifications
  4. Mark Notifications as Seen
  5. Delete Notifications
  6. Get Notification Statistics
  7. 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

ParameterTypeRequiredDescription
typestringYesNotification type (e.g., 'info', 'warning', 'success')
titlestringYesNotification title
messagestringYesNotification message content
dataobjectNoAdditional data payload
userIdsarrayYesArray of user IDs to send notification to
tenant_IdstringNoTenant 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

ParameterTypeDescription
userIdstringGet notifications for specific user (admin only)
seenbooleanFilter by seen status (true/false)
typestringFilter by notification type
limitnumberMaximum number of notifications to return
pagenumberPage number for pagination
sortstringSort 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

ParameterTypeRequiredDescription
notificationIdsarrayYesArray 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

ParameterTypeRequiredDescription
notificationIdsarrayYesArray 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

CodeMessageDescription
400Invalid notification parametersMissing required fields
401UnauthorizedAuthentication required
403ForbiddenInsufficient permissions
404Notification not foundNotification ID doesn't exist
422Validation failedInvalid 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"
    }
  }
}

Real-time Delivery

Authentication

Automation

Multi-tenant

Reference

← Back to Documentation Home

On this page