This is a blogging platform api that allows users to interact with posts. The project is built using FastAPI and includes user authentication, post/comments creation, fetch endpoints.
- Python3
- PostgreSQL
- Clone the repository
git clone git@github.com:Tgithinji/blog_api.git
cd blog_api- Create and start a virtual environment
python3 -m venv venv
source venv/bin/activate- Install the required dependencies
pip install -r requirements.txt- Create a .env file and setup environment variables requires to connect to your database in the following order
- SECRET_KEY: string
- ALGORITHM: string
- TOKEN_EXPIRY: int
- DB_USERNAME: str
- DB_PASSWORD: str
- DB_HOSTNAME: str
- DB_PORT: str
- DB_NAME: str
- The API uses Alembic for migrations so run the following
alembic revision --autogenerate -m "Migration message"
alembic upgrade head- Start the application
fastapi dev app/main.pyYou can test endpoints with curl in the terminal after the application has started successfully
Or visit http://127.0.0.1:8000/docs on your browser to view the api documentation by swagger UI
Or you can use postman like demonstrated in the demo video
- Login
Post/loginExample:
curl -X POST "http://127.0.0.1:8000/login" \
-H "Content-Type: application/json" \
-d '{"username": "newuser", "password": "securepassword"}'Response Body:
{
"access_token": "your-jwt-token",
"token_type": "bearer"
}- Create User
Post/users- Authentication required Example
curl -X POST "http://127.0.0.1:8000/login" \
-H "Content-Type: application/json" \
-d '{
"username": "newuser", "email": "newuser@gmail.com" "password": "securepassword"
}'Response body:
{
"username": "user1",
"email": "user1@example.com",
"id": "1",
"created_at": "time created"
}- Get User
GET/users/{id}Example
curl -X GET http://127.0.0.1:8000/users/1Response body:
{
"user": {
"id": 1,
"username": "john_doe",
"email": "john_doe@example.com"
},
"post": 3
}- Get All Posts:
GET/posts
Example
curl -X GET http://127.0.0.1:8000/posts-
Returns all posts with their comments count
-
Pagination is supported
-
search query is supported to search by title
-
Get Single Post:
GET/posts/{id}Response body: Example
curl -X GET http://127.0.0.1:8000/posts/1{
"post": {
"id": 1,
"title": "Understanding FastAPI Relationships",
"content": "This post explains how to use FastAPI to manage relationships...",
"created_at": "2024-10-15T10:00:00Z",
"author": {
"username": "jane_doe",
"email": "jane_doe@example.com"
},
"comments": 5
}
}- Create Post:
POST/posts- Requires Authentication Response body same as getting a single post Example usage:
curl -X POST http://127.0.0.1:8000/posts \
-H "Authorization: Bearer <TOKEN>" \
-H "Content-Type: application/json" \
-d '{"title": "My Post", "content": "This is my first post"}'- Update Post
Put/posts/{id} - Requires authentication Request body same as Create Post Example usage:
curl -X PUT http://127.0.0.1:8000/posts/1 \
-H "Authorization: Bearer <TOKEN>" \
-H "Content-Type: application/json" \
-d '{"title": "Changed Post", "content": "This is my changed post"}'- Delete Post:
Delete/post{id}- Deletes a post by id
curl -X GET http://127.0.0.1:8000/posts/1- Get comments for a single post:
GET/posts/{id}/commentsExample
curl -X GET http://127.0.0.1:8000/posts/1/commentsReturns a list of comments each with the following Response body:
{
"id": 101,
"content": "This post was really helpful!",
"user_id": 42,
"post_id": 1
}- Create Comment:
POSTposts/{id}/comments- Requires Authentication Response body same as above
curl -X POST http://127.0.0.1:8000/posts/1/comments \
-H "Authorization: Bearer <TOKEN>" \
-H "Content-Type: application/json" \
-d '{"content": "comment"}'- Delete Comment:
Delete/post{id}/comments/{id}- Deletes a comment by id
curl -X DELETE http://127.0.0.1:8000/posts/1comments/1