This project is a simple blog API for a school, built using FastAPI and MongoDB with Motor for asynchronous database interactions. It features data validation using Pydantic.
- Create, read, update, and delete blog posts.
- Asynchronous MongoDB interaction.
- Data validation with Pydantic models.
- Auto-generated API documentation via FastAPI.
school_blog/
├── __pycache__/ # Compiled Python files
├── screenshot/ # Directory containing screenshots of the API documentation
├── venv/ # Virtual environment for the project
├── main.py # Main FastAPI application file
├── models.py # Defines Pydantic models for data validation
├── database.py # Handles MongoDB connections and interactions
└── routes.py # API route definitions
├── requirements.txt # Lists required Python packages
└── README.md # Project documentation
- FastAPI: For building the web API.
- Motor: For asynchronous MongoDB interactions.
- Pydantic: For data validation.
- Uvicorn: ASGI server to run the FastAPI app.
- Python 3.7 or later
- MongoDB installed and running on your machine (default port 27017)
-
Clone the repository:
git clone https://github.com/01Prathamesh/school_blog_API.git cd school_blog -
Create a virtual environment and activate it:
python -m venv venv venv\Scripts\activate # On Windows # source venv/bin/activate # On macOS/Linux
-
Install the required packages:
pip install -r requirements.txt
-
Start your MongoDB server if it’s not already running.
-
Run the FastAPI application:
python -m uvicorn school_blog.main:app --reload
-
Access the API documentation: Open your browser and go to
http://127.0.0.1:8000/docsto see the interactive API documentation.
-
Create a post
- Endpoint:
POST /posts/ - Request Body:
{ "title": "Your Post Title", "content": "The content of the post", "author": "Author Name", "created_at": "2024-10-27T12:00:00Z" } - Endpoint:
-
Get a post by ID
- Endpoint:
GET /posts/{post_id}
- Endpoint:
-
Example Responses:
- Success:
{ "_id": "60d5ec49f0d0b30017d4b5e1", "title": "Your Post Title", "content": "The content of the post", "author": "Author Name", "created_at": "2024-10-27T12:00:00Z" }- Not Found:
{ "detail": "Post not found" } -
Update a post
- Endpoint:
PUT /posts/{post_id} - Request Body:
{ "title": "Updated Title", "content": "Updated content", "author": "Updated Author Name" } - Endpoint:
-
Delete a post
- Endpoint:
DELETE /posts/{post_id}
- Endpoint:
Here are some screenshots of the FastAPI Swagger UI:




