Blogs

The Blogs API provides comprehensive functionality for creating, managing, and retrieving blog posts. It includes support for rich content, image uploads, and status management with role-based access controls.

Endpoints

Create Blog Post

Creates a new blog post with banner image upload. Admin role required.

Endpoint: POST /blogs/

Authentication: Required (Bearer token with admin role)

Content-Type: multipart/form-data

Form Data:

  • title (required): Blog post title (max 180 characters)

  • content (required): HTML content of the blog post

  • status (optional): "draft" or "published" (default: "draft")

  • banner_image (required): Image file (PNG/JPG/WebP, max 2MB)

Success Response (201 Created):

{
  "blog": {
    "_id": "60b7c8b4f9b3c12345678903",
    "title": "Getting Started with Node.js",
    "slug": "getting-started-with-nodejs",
    "content": "<h1>Introduction</h1><p>Node.js is a powerful runtime...</p>",
    "banner": {
      "url": "https://cloudinary.com/image/upload/v1234567890/banners/abc123.jpg",
      "width": 1200,
      "height": 630
    },
    "author": {
      "_id": "60b7c8b4f9b3c12345678901",
      "username": "admin",
      "email": "admin@example.com",
      "role": "admin"
    },
    "viewsCount": 0,
    "likesCount": 0,
    "commentsCount": 0,
    "status": "draft",
    "publishedAt": null,
    "updatedAt": "2024-01-21T10:30:00.000Z"
  }
}

Error Responses:

  • 400 - Validation errors (missing fields, invalid data)

  • 401 - Unauthorized

  • 403 - Forbidden (user is not admin)

  • 413 - File too large (over 2MB)

  • 500 - Internal server error

Usage Example:


Get All Blogs

Retrieves a paginated list of blog posts. Admins see all posts; regular users see only published posts.

Endpoint: GET /blogs/

Authentication: Required (Bearer token)

Query Parameters:

  • limit (optional): Number of blogs per page (1-50, default: 20)

  • offset (optional): Number of blogs to skip (default: 0)

Success Response (200 OK):

Error Responses:

  • 400 - Invalid query parameters

  • 401 - Unauthorized

  • 500 - Internal server error


Get Blogs by User

Retrieves blog posts created by a specific user.

Endpoint: GET /blogs/user/{userId}

Authentication: Required (Bearer token)

Path Parameters:

  • userId: MongoDB ObjectId of the user

Query Parameters:

  • limit (optional): Number of blogs per page (1-50, default: 20)

  • offset (optional): Number of blogs to skip (default: 0)

Success Response (200 OK):

Error Responses:

  • 400 - Invalid parameters

  • 401 - Unauthorized

  • 500 - Internal server error


Get Blog by Slug

Retrieves a single blog post by its unique slug. Increments view count.

Endpoint: GET /blogs/{slug}

Authentication: Required (Bearer token)

Path Parameters:

  • slug: URL-friendly slug of the blog post

Success Response (200 OK):

Error Responses:

  • 400 - Invalid slug format

  • 401 - Unauthorized

  • 403 - Forbidden (trying to access draft as regular user)

  • 404 - Blog not found

  • 500 - Internal server error


Update Blog Post

Updates an existing blog post. Banner image update is optional. Admin role required.

Endpoint: PUT /blogs/{blogId}

Authentication: Required (Bearer token with admin role)

Content-Type: multipart/form-data

Path Parameters:

  • blogId: MongoDB ObjectId of the blog post

Form Data (all fields optional):

  • title: Updated blog title (max 180 characters)

  • content: Updated HTML content

  • status: "draft" or "published"

  • banner_image: New banner image file (PNG/JPG/WebP, max 2MB)

Success Response (200 OK):

Error Responses:

  • 400 - Validation errors

  • 401 - Unauthorized

  • 403 - Forbidden (user is not admin)

  • 404 - Blog not found

  • 413 - File too large

  • 500 - Internal server error


Delete Blog Post

Permanently deletes a blog post and its banner image. Admin role required.

Endpoint: DELETE /blogs/{blogId}

Authentication: Required (Bearer token with admin role)

Path Parameters:

  • blogId: MongoDB ObjectId of the blog post

Success Response (204 No Content): Empty response body

Error Responses:

  • 401 - Unauthorized

  • 403 - Forbidden (user is not admin)

  • 404 - Blog not found

  • 500 - Internal server error

Note: This action also deletes all associated comments and likes.

Blog Status Management

Draft Status

  • Only visible to admins

  • Can be edited and updated

  • Not included in public blog listings for regular users

  • Does not have a publishedAt timestamp

Published Status

  • Visible to all authenticated users

  • Publicly accessible via slug

  • Included in all blog listings

  • Has a publishedAt timestamp set when first published

Content Guidelines

Title Requirements

  • Maximum 180 characters

  • Should be descriptive and SEO-friendly

  • Automatically generates URL slug

Content Format

  • Supports full HTML content

  • Recommended to use semantic HTML elements

  • Images should be optimized before embedding

  • External links should open in new tabs

  • Formats: PNG, JPG, WebP

  • Size Limit: 2MB maximum

  • Recommended Dimensions: 1200x630px (OG image ratio)

  • Automatically processed: Cloudinary optimizes uploaded images

Code Examples

Create Blog Post with File Upload

Get Blog Posts with Pagination

Complete Blog Management Class

SEO and Performance

Automatic Slug Generation

  • Slugs are automatically generated from blog titles

  • URL-friendly format (lowercase, hyphens instead of spaces)

  • Unique across all blog posts

Image Optimization

  • Cloudinary integration for automatic image processing

  • Multiple format support (WebP for modern browsers)

  • Responsive image delivery

Content Indexing

  • Full-text search capabilities (implementation depends on database)

  • Tag-based categorization (can be extended)

  • Related post suggestions (can be implemented)

Role-based Access Summary

Regular Users (role: "user")

  • ✅ View published blog posts

  • ✅ Search and filter blogs

  • ❌ Create new blog posts

  • ❌ Edit existing blog posts

  • ❌ View draft posts

  • ❌ Delete blog posts

Administrators (role: "admin")

  • ✅ All user permissions

  • ✅ Create new blog posts

  • ✅ Edit any blog post

  • ✅ Delete any blog post

  • ✅ View draft posts

  • ✅ Manage blog status (draft/published)

  • ✅ Upload and manage banner images

Last updated