Building a scalable blog backend requires a well-structured architecture, authentication system, and properly documented APIs. The NestJS MongoDB Blog API is a production-ready backend template designed to help developers quickly build modern blog platforms using NestJS, MongoDB, JWT authentication, and Swagger documentation.
This project provides a complete RESTful API including modules for users, posts, categories, tags, comments, authentication, and role-based access control.
Repository: https://github.com/bfotool/nestjs-mongodb-blog-base
What is NestJS MongoDB Blog API
The NestJS MongoDB Blog API is an open-source backend template built with NestJS 11 and MongoDB that provides a fully functional blog REST API.
It is designed to serve as a foundation for building:
-
Blog platforms
-
Headless CMS systems
-
Content publishing APIs
-
Documentation websites
-
Developer portfolio blogs
The project follows the modular architecture recommended by NestJS, ensuring scalability and maintainability.
Repository: https://github.com/bfotool/nestjs-mongodb-blog-base
Key Features
Authentication and Authorization
The API includes a secure authentication system using JWT tokens.
Main authentication features include:
-
JWT Access and Refresh Tokens
-
Role-Based Access Control (RBAC)
-
Password hashing with bcrypt
-
Protected API routes using Guards
-
Token refresh mechanism
Three default user roles are supported:
-
Admin
-
Author
-
Reader
This makes it easy to manage permissions across different API endpoints.
Full Blog Modules
The project includes several fully implemented modules that cover all core blog functionality.
Auth Module
Handles authentication and user sessions.
Endpoints include:
-
POST /auth/register -
POST /auth/login -
POST /auth/refresh -
POST /auth/logout -
GET /auth/profile
Users Module
Manages user accounts and profiles.
Endpoints:
-
GET /users -
GET /users/:id -
PATCH /users/:id -
DELETE /users/:id
Write operations require Admin permissions.
Posts Module
The posts module manages blog content.
Features include:
-
Pagination
-
Filtering by category, tag, or author
-
Sorting
-
Full-text search
-
Featured posts
Endpoints:
-
GET /posts -
GET /posts/featured -
GET /posts/:slug -
POST /posts -
PATCH /posts/:id -
DELETE /posts/:id
Categories Module
Organizes blog posts into categories.
Endpoints:
-
GET /categories -
GET /categories/:slug -
POST /categories -
PATCH /categories/:id -
DELETE /categories/:id
Tags Module
Allows flexible tagging for blog posts.
Endpoints:
-
GET /tags -
GET /tags/:slug -
POST /tags -
PATCH /tags/:id -
DELETE /tags/:id
Comments Module
Supports comment threads and nested replies.
Endpoints:
-
GET /comments/post/:postId -
GET /comments/:id/replies -
POST /comments -
PATCH /comments/:id -
DELETE /comments/:id
API Capabilities
Pagination
All list endpoints support pagination.
Example:
GET /posts?page=1&limit=6
Filtering
Posts can be filtered by category, tag, or author.
Example:
GET /posts?category=web-development
Full-Text Search
Search functionality is implemented on posts.
Example:
GET /posts?search=nestjs+mongodb
Sorting
API responses can be sorted by different fields.
Example:
GET /posts?sortBy=createdAt&sortOrder=desc
Automatic Slug Generation
When creating posts, the system automatically generates a URL-friendly slug from the title using slugify.
Example:
Building REST APIs with NestJS
→ building-rest-apis-with-nestjs
Reading Time Calculation
The API automatically calculates estimated reading time based on the content length of posts.
Consistent API Response Format
All responses follow a standard structure:
{
"success": true,
"data": {},
"timestamp": "2026-01-01T10:00:00Z"
}
This improves frontend integration and debugging.
Technology Stack
The backend is built using a modern Node.js stack.
| Layer | Technology |
|---|---|
| Framework | NestJS 11 |
| Language | TypeScript 5.7 |
| Database | MongoDB 8 |
| ODM | Mongoose 8 |
| Authentication | Passport.js + JWT |
| Validation | class-validator |
| API Documentation | Swagger OpenAPI |
| Password Security | bcrypt |
| Slug Generator | slugify |
| Testing | Jest |
Project Structure
The project follows a modular architecture.
src/
├── auth/
├── users/
├── posts/
├── categories/
├── tags/
├── comments/
├── common/
└── seed/
Important components include:
-
DTO validation
-
Mongoose schemas
-
Global exception filters
-
Response interceptors
-
JWT guards
-
Role decorators
This structure keeps the codebase organized and scalable.
How to Install and Run the Project
Prerequisites
Before installing, make sure you have:
-
Node.js 18.17+
-
MongoDB 6+
-
npm, yarn, or pnpm
Clone the Repository
git clone https://github.com/bfotool/nestjs-mongodb-blog-base
cd nestjs-mongodb-blog-base
Install Dependencies
npm install
Configure Environment Variables
Copy the example environment file:
cp .env.example .env
Update the following values:
MONGODB_URI=
JWT_ACCESS_SECRET=
JWT_REFRESH_SECRET=
Seed the Database
Populate the database with sample data:
npm run seed
To reset and reseed:
npm run seed:refresh
Start the Development Server
npm run start:dev
API base URL:
http://localhost:3000/api/v1
Swagger documentation:
http://localhost:3000/api/docs
Default Seeded Data
The seed script generates sample data for testing.
| Entity | Count |
|---|---|
| Users | 5 |
| Categories | 5 |
| Tags | 28 |
| Posts | 10 |
| Comments | 6 |
Default Login Credentials
Password for all seeded users:
admin123
Accounts:
| Role | |
|---|---|
| Admin | [email protected] |
| Author | [email protected] |
| Author | [email protected] |
| Author | [email protected] |
| Reader | [email protected] |
Swagger API Documentation
Interactive API documentation is available at:
http://localhost:3000/api/docs
Swagger provides:
-
Interactive endpoint testing
-
JWT authentication support
-
Request and response schema documentation
Why Use This Repository
The NestJS MongoDB Blog API is a powerful starting point for developers building a modern blog backend.
Advantages include:
-
Clean NestJS modular architecture
-
Built-in authentication and RBAC
-
Ready-to-use Swagger API documentation
-
MongoDB integration using Mongoose
-
Database seeding system
-
Production-ready project structure
Repository: https://github.com/bfotool/nestjs-mongodb-blog-base



