Smart Library Docs

API Documentation

Complete REST API overview — base URL, authentication, dual-token system, request/response format, error codes, and endpoint index.

API Documentation

The SLMS backend exposes a RESTful API for all library operations. This page provides the complete overview; individual endpoint groups are documented in detail on their own pages.

Base URL

http://localhost:5000/api/v1

All endpoints are prefixed with /api/v1/ for versioning.

Authentication

Dual Token System

SLMS uses a dual-token architecture with short-lived access tokens and long-lived refresh tokens:

TokenLifetimePurpose
Access Token15 minutesJWT sent in Authorization header for API access
Refresh Token7 daysOpaque token used to obtain new access tokens

Using Access Tokens

All protected endpoints require a JWT Bearer token in the Authorization header:

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

Token Refresh Flow

When the access token expires, exchange the refresh token for a new access token:

POST /api/v1/auth/refresh
Content-Type: application/json

{
  "refreshToken": "a1b2c3d4e5f6..."
}

Token Lifecycle

Login → Access Token (15min) + Refresh Token (7 days)

                ├─ Access token expires
                │   └─ POST /auth/refresh → New Access Token

                └─ Logout
                    └─ Both tokens blacklisted

Token Blacklisting

On logout, both the access and refresh tokens are added to the BlacklistedToken collection. The auth middleware checks the blacklist on every request. Blacklisted tokens are automatically purged by MongoDB's TTL index after their natural expiry.

Rate Limiting

ScopeWindowMax RequestsEndpoints
Auth routes1 minute5 per IP/auth/setup, /auth/login, /auth/refresh
User creation1 minute5 per IPPOST /users
Global API15 minutes100 per IPAll /api/ routes (in src/app.js)

Request Format

  • Content-Type: application/json
  • Body: JSON payloads for POST, PUT, and PATCH requests
  • URL Parameters: Path params (:id) and query strings (?search=term)

Response Format

Success Response

{
  "books": [...],
  "accessToken": "eyJ...",
  "refreshToken": "a1b2...",
  "user": { ... }
}

Responses use domain-specific keys (e.g., books, users, transaction) rather than a generic data wrapper.

Error Response

{
  "error": "Human-readable error message"
}

HTTP Status Codes

CodeMeaningWhen Used
200OKSuccessful GET, PUT, PATCH
201CreatedSuccessful POST (resource created)
400Bad RequestInvalid input or business rule violation
401UnauthorizedMissing, invalid, or expired JWT token
403ForbiddenInsufficient role permissions
404Not FoundResource doesn't exist
429Too Many RequestsRate limit exceeded
500Internal Server ErrorUnhandled server error

Endpoint Index

Authentication

MethodEndpointAuthDescription
POST/auth/setupCreate initial Super Admin
POST/auth/loginLogin and get dual tokens
POST/auth/refreshRefresh access token
POST/auth/logoutLogout (blacklist tokens)
GET/auth/meGet current user profile

Detailed Auth Endpoints

Users

MethodEndpointAuthDescription
GET/usersAdmin, LibrarianList all users
POST/usersAdminCreate user
GET/users/meGet own profile
PATCH/users/meUpdate own profile
GET/users/:idAdmin, LibrarianGet user by ID
PATCH/users/:idAdmin, LibrarianUpdate user

Detailed Users Endpoints

Books

MethodEndpointAuthDescription
GET/books/categoriesList all categories
GET/booksList all books
GET/books/:idGet book by ID
POST/booksAdmin, LibrarianCreate book
PATCH/books/:idAdmin, LibrarianUpdate book
DELETE/books/:idAdmin, LibrarianDelete book
POST/books/:id/copiesAdmin, LibrarianAdd physical copy

Detailed Books Endpoints

Circulation

MethodEndpointAuthDescription
POST/circulation/issueAdmin, LibrarianIssue book
POST/circulation/returnAdmin, LibrarianReturn book
GET/circulation/historyGet borrowing history
GET/circulation/activeAdmin, LibrarianList active loans

Detailed Circulation Endpoints

Fines

MethodEndpointAuthDescription
GET/finesList fines
POST/fines/:id/payAdmin, LibrarianPay/resolve fine

Detailed Fines Endpoints

Dashboard

MethodEndpointAuthDescription
GET/dashboard/statsGet aggregate statistics
GET/dashboard/analyticsGet trend analytics data

Detailed Dashboard Endpoints

Reservations

MethodEndpointAuthDescription
POST/reservationsCreate a reservation
GET/reservations/meList own reservations
POST/reservations/:id/cancelCancel own reservation
GET/reservationsAdmin, LibrarianList all reservations
POST/reservations/:id/fulfillAdmin, LibrarianFulfill reservation

Detailed Reservations Endpoints

Notifications

MethodEndpointAuthDescription
GET/notificationsGet own notifications
PUT/notifications/mark-all-readMark all as read
PUT/notifications/:id/readMark single as read

Detailed Notifications Endpoints

Reports

MethodEndpointAuthDescription
GET/reports/circulationAdmin, LibrarianDownload circulation CSV
GET/reports/finesAdmin, LibrarianDownload fines CSV

Detailed Reports Endpoints

Audit

MethodEndpointAuthDescription
GET/auditAdminGet audit logs

Detailed Audit Endpoints

System Config

MethodEndpointAuthDescription
GET/configAdminList all configs
PATCH/config/:idAdminUpdate config value

Detailed System Config Endpoints

Health Check

MethodEndpointAuthDescription
GET/healthServer health status
curl http://localhost:5000/health
# { "status": "ok", "message": "SLMS API is running" }

On this page