A RESTful API for a simple blog platform built with FastAPI and PostgreSQL.
- User authentication with JWT tokens
- Create, read, update, and delete blog posts
- Add, read, update, and delete comments on posts
- Pagination for listing posts and comments
- Containerized with Docker
- Interactive API documentation with Swagger UI
- Docker and Docker Compose
- Python 3.9+ (for local development without Docker)
-
Clone the repository:
git clone <repository-url> cd blog_api
-
Copy the example environment file and update the values if needed:
cp .env.example .env
-
Build and start the services:
docker-compose up --build
-
The API will be available at
http://localhost:8000 -
Access the interactive API documentation at
http://localhost:8000/docs -
Access pgAdmin at
http://localhost:5050(default credentials: admin@admin.com/admin)
-
Clone the repository:
git clone <repository-url> cd blog_api
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: .\venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
cp .env.example .env # Update the .env file with your database credentials -
Initialize the database:
python -m app.db.init_db
-
Run the development server:
uvicorn app.main:app --reload
-
The API will be available at
http://localhost:8000
POST /api/v1/register- Register a new userPOST /api/v1/token- Get access token (login)GET /api/v1/users/me- Get current user details
GET /api/v1/posts/- List all posts (paginated)POST /api/v1/posts/- Create a new post (authenticated)GET /api/v1/posts/{post_id}- Get a specific postPUT /api/v1/posts/{post_id}- Update a post (authenticated, author only)DELETE /api/v1/posts/{post_id}- Delete a post (authenticated, author only)
GET /api/v1/posts/{post_id}/comments/- List all comments for a post (paginated)POST /api/v1/posts/{post_id}/comments/- Add a comment to a post (authenticated)GET /api/v1/posts/{post_id}/comments/{comment_id}- Get a specific commentPUT /api/v1/posts/{post_id}/comments/{comment_id}- Update a comment (authenticated, author only)DELETE /api/v1/posts/{post_id}/comments/{comment_id}- Delete a comment (authenticated, author only)
DATABASE_URL- Database connection stringSECRET_KEY- Secret key for JWT token generationALGORITHM- Algorithm for JWT (default: HS256)ACCESS_TOKEN_EXPIRE_MINUTES- Token expiration time in minutes (default: 30)
You can test the API using the provided Postman collection and environment files.
- Postman installed on your machine
- The API server running locally
-
Import the Collection:
- Open Postman
- Click "Import" and select the
Blog_API.postman_collection.jsonfile - This will import all the API endpoints with example requests
-
Import the Environment:
- In Postman, go to the "Environments" tab
- Click "Import" and select the
Blog_API_Environment.postman_environment.jsonfile - Select the imported environment from the dropdown in the top-right corner
The environment includes these variables:
base_url: Set tohttp://localhost:8000by defaultauth_token: Will be automatically set after login- Test user credentials (username:
testuser, password:testpass123)
-
Authentication
- Start with the "Register User" request to create a new account
- Use the "Login" request to get an authentication token
- The token will be automatically saved to the
auth_tokenenvironment variable
-
Testing Posts
- Use the "Create Post" request to add a new post
- Test the "List Posts" endpoint to see all posts
- Try updating and deleting posts (requires authentication)
-
Testing Comments
- Use the "Create Comment" request to add a comment to a post
- Test listing, updating, and deleting comments
- Make sure your API server is running
- In Postman, open the "Blog API" collection
- Click the "Run" button next to the collection name
- Select the environment and click "Run Blog API"
- View the test results in the "Test Results" tab