This is a simple FastAPI application that demonstrates basic CRUD operations for blog posts using in-memory storage (a Python list). It does not use any views or templates β purely an API backend.
.
βββ main.py # The main FastAPI application
βββ README.md # Project documentation
βββ .gitignore # Ignored files and folders
- π’ Get all posts
- π‘ Create a new post
- π΅ Get a single post by ID
- π΄ Delete a post by ID
- Python 3.7+
- FastAPI
- Uvicorn
Install dependencies:
pip install fastapi uvicornUse uvicorn to run the FastAPI app:
uvicorn main:app --reload--reload: enables hot-reload on code changes (useful for development)
| Method | Endpoint | Description |
|---|---|---|
| GET | / |
Root message |
| GET | /posts |
Retrieve all posts |
| POST | /posts |
Create a new post |
| GET | /posts/{id} |
Retrieve a post by ID |
| DELETE | /posts/{id} |
Delete a post by ID |
{
"title": "Sample Title",
"content": "Sample content goes here",
"published": true,
"rating": 5
}- The application uses in-memory storage (
my_postslist), so data resets every time the server restarts. - ID is randomly generated using Python's
randrange.
Happy coding! π