๐ Blog API
A RESTful blogging platform API built with Flask and MongoDB.
Create, manage, search and organise blog posts through a clean and lightweight backend service.
๐ Overview
Blog API is a RESTful backend application that provides all the core functionality required to power a blogging platform.
The application allows users to create, retrieve, update, delete and search blog posts while storing data in MongoDB. It was built using Flask and follows a simple separation-of-concerns architecture where route handling and database operations are kept separate.
This project was developed as part of the roadmap.sh Backend Projects collection and demonstrates practical backend development concepts including:
- REST API design
- CRUD operations
- Database integration
- Environment variable management
- Request validation
- Rate limiting
- Search functionality
- API testing with Postman
โจ Features
Blog Post Management
- Create blog posts
- Retrieve all blog posts
- Retrieve a specific blog post by ID
- Update existing blog posts
- Delete blog posts
Search Functionality
Search blog posts by:
Searches are case-insensitive and use MongoDB regular expressions.
Data Persistence
All blog posts are stored in MongoDB, ensuring data remains available between application restarts.
Timestamp Tracking
Every post contains:
allowing changes to be tracked over time.
Rate Limiting
Most endpoints are protected using Flask-Limiter:
This helps prevent excessive requests and basic abuse.
๐ Technology Stack
| Technology |
Purpose |
| Python |
Programming language |
| Flask |
Web framework |
| MongoDB |
NoSQL database |
| PyMongo |
MongoDB driver |
| Flask-Limiter |
Rate limiting |
| python-dotenv |
Environment variable management |
๐ Architecture
The application follows a layered structure:
Client
โ
โผ
Flask Routes (app.py)
โ
โผ
Database Service Layer (services/db.py)
โ
โผ
MongoDB Database
Why This Structure?
Separating route logic from database logic makes the code:
- Easier to maintain
- Easier to test
- Easier to expand
- More readable
The route handlers focus on:
- Receiving requests
- Validating input
- Returning HTTP responses
The service layer focuses on:
- Database operations
- Query execution
- Data retrieval
- Data updates
๐ Project Structure
Blog-API/
โ
โโโ app.py
โโโ services/
| โโโ app.py
โ โโโ db.py
โ
โโโ requirements.txt
โโโ LICENSE
โโโ README.md
โ
โโโ venv/
File Breakdown
app.py
Contains:
- Flask application setup
- Route definitions
- Request validation
- Rate limiting configuration
- HTTP responses
services/db.py
Contains:
- MongoDB connection
- CRUD operations
- Search functionality
- Timestamp handling
requirements.txt
Lists all Python dependencies required to run the project.
I just used pip freeze > requirements.txt to make the file.
LICENSE
Contains the project's MIT Licence.
โ๏ธ Installation
1. Clone the Repository
git clone https://github.com/Sheikh-H/blog-api.git
cd blog-api
2. Create a Virtual Environment
Windows
python -m venv venv
venv\Scripts\activate
Linux / macOS
python -m venv venv
source venv/bin/activate
3. Install Dependencies
pip install -r requirements.txt
๐ Environment Variables
Create a .env file in the project root:
MONGO_URI=your_mongodb_connection_string
Example:
MONGO_URI=mongodb+srv://username:password@cluster.mongodb.net/
The application loads this value automatically using python-dotenv.
๐ MongoDB Database
The application creates and uses:
Database:
rdmpsh-blog
Collection:
blog-posts
Each blog post follows this structure:
{
"id": 1,
"title": "My First Post",
"content": "Hello World",
"category": "Technology",
"tags": ["python", "flask"],
"createdAt": "2026-06-03 15:30:00",
"updatedAt": "Null"
}
โถ Running the Application
Start the server:
or
The API will be available at:
or
๐ฌ API Endpoints
Create a Blog Post
POST /posts
Creates a new blog post.
Request Body
{
"title": "Learning Flask",
"content": "Flask is a lightweight Python framework.",
"category": "Programming",
"tags": ["python", "flask"]
}
Success Response
{
"id": 1,
"title": "Learning Flask",
"content": "Flask is a lightweight Python framework.",
"category": "Programming",
"tags": ["python", "flask"],
"createdAt": "2026-06-03 15:30:00",
"updatedAt": "Null"
}
Status Code
Retrieve All Posts
GET /posts
Returns every blog post stored in the database.
Browser Example
Status Code
Search Posts
GET /posts?term=python
Searches:
Browser Example
localhost:5000/posts?term=python
Status Code
Retrieve a Single Post
GET /posts/<id>
Example:
Browser:
Status Code
Update a Post
PUT /posts/<id>
Does support partial updates.
Example:
{
"title": "Updated Title"
}
Only the supplied field is modified.
Example:
Success Response
{
"id": 1,
"title": "Updated Title",
"content": "Flask is a lightweight Python framework.",
"category": "Programming",
"tags": ["python", "flask"],
"createdAt": "2026-06-03 15:30:00",
"updatedAt": "2026-06-04 09:15:00"
}
Status Code
Delete a Post
DELETE /posts/<id>
Example:
Success Status
๐ Postman Usage
Postman provides a simple way to test every endpoint.
Create a Post
- Select POST
- Enter:
- Choose:
- Paste:
{
"title": "My First Post",
"content": "Hello World",
"category": "Technology",
"tags": ["python", "mongodb"]
}
- Click Send
๐ป cURL Examples
Create a Post
curl -X POST localhost:5000/posts \
-H "Content-Type: application/json" \
-d '{
"title":"My First Post",
"content":"Hello World",
"category":"Technology",
"tags":["python","mongodb"]
}'
Get All Posts
curl localhost:5000/posts
Get a Single Post
curl localhost:5000/posts/1
Search Posts
curl "localhost:5000/posts?term=python"
Update a Post
curl -X PUT localhost:5000/posts/1 \
-H "Content-Type: application/json" \
-d '{
"title":"Updated Post Title"
}'
Delete a Post
curl -X DELETE localhost:5000/posts/1
๐ Function Breakdown
Database Functions (services/db.py)
add_post()
Creates a new blog post and stores it in MongoDB.
get_post()
Retrieves a single blog post by its ID.
get_posts()
Returns every blog post from the collection.
update_post()
Updates one or more fields and refreshes the updatedAt timestamp.
delete_post()
Removes a blog post from the database.
search_posts()
Searches title, content and category fields using MongoDB regular expressions.
Route Functions (app.py)
POST /posts
Validates request data before creating a post.
GET /posts
Returns all posts or performs a search if a term is supplied.
GET /posts/<id>
Returns a single blog post.
PUT /posts/<id>
Updates selected fields of a blog post.
DELETE /posts/<id>
Deletes a blog post by ID.
โ Error Responses
Examples:
Missing Required Fields
{
"error": "unable to add post, please use all required fields"
}
Post Not Found
{
"error": "post not found"
}
Invalid Update Request
{
"error": "unable to update"
}
๐ก Rate Limiting
Uses Flask-Limiter to restrict traffic.
Current configuration:
Applied to:
- POST /posts
- GET /posts
- GET /posts/
- DELETE /posts/
This helps prevent accidental or excessive API usage.
๐ฎ Future Improvements
Potential enhancements include:
- User authentication
- JWT authorisation
- User accounts
- Pagination
- Sorting and filtering
- Comment system
- Categories collection
- API versioning
- Docker support
- Automated testing
- Swagger/OpenAPI documentation
๐ฏ Roadmap.sh Project
This project was built as part of the Blogging Platform API project from roadmap.sh.
The goal was to design and implement a fully functional REST API capable of managing blog content using modern backend development practices.
๐ Licence
This project is licensed under the MIT Licence โ see the LICENCE file for details.