This is a RESTful API for managing a to-do list application. It allows users to register, log in, create, read, update, and delete to-do items. The API is secured with JWT authentication.
- Endpoint:
/register
- Method:
POST
- Description: Registers a new user with a username and password.
- Request Body:
username
: String (required) - The username of the user.password
: String (required) - The password of the user.
- Response:
- Status: 200 OK
- Content:
{ "message": "User registered successfully" }
- Endpoint:
/token
- Method:
POST
- Description: Logs in a user and returns an access token.
- Request Body:
username
: String (required) - The username of the user.password
: String (required) - The password of the user.
- Response:
- Status: 200 OK
- Content:
{ "access_token": "JWT_TOKEN", "token_type": "bearer" }
- Endpoint:
/todos/
- Method:
POST
- Description: Creates a new to-do item.
- Request Body:
title
: String (required) - The title of the to-do.description
: String (required) - The description of the to-do.done
: Boolean (optional) - The status of completion (default isFalse
).
- Response:
- Status: 200 OK
- Content:
{ "message": "Todo created successfully", "todo_id": "TODO_ID" }
- Endpoint:
/todos/
- Method:
GET
- Description: Retrieves all to-do items.
- Response:
- Status: 200 OK
- Content: List of to-do items.
- Endpoint:
/todos/{todo_title}
- Method:
GET
- Description: Retrieves a specific to-do item by its title.
- Response:
- Status: 200 OK
- Content: The to-do item.
- Endpoint:
/todos/{todo_title}
- Method:
PUT
- Description: Updates a specific to-do item.
- Request Body:
title
: String (optional) - The new title of the to-do.description
: String (optional) - The new description of the to-do.done
: Boolean (optional) - The new status of completion.
- Response:
- Status: 200 OK
- Content:
{ "message": "Todo updated successfully" }
- Endpoint:
/todos/{todo_title}
- Method:
DELETE
- Description: Deletes a specific to-do item by its title.
- Response:
- Status: 200 OK
- Content:
{ "message": "Todo deleted successfully" }
To access the above endpoints, the user must include an access token in the request header. The access token can be obtained by logging in via the /token
endpoint.
The access token must be included in the Authorization
header of the request in the format Bearer JWT_TOKEN
.
- FastAPI: The main framework used for building the API.
- PyJWT: For encoding and decoding JSON Web Tokens (JWT) for authentication.
- Passlib: For secure password hashing.
- Pymongo: A MongoDB driver for Python, used for interacting with the MongoDB database.
- OAuth2PasswordBearer: A FastAPI security dependency for handling OAuth2 password flow for token-based authentication.
- BaseModel: From the Pydantic library, used for defining data models with validation and serialization capabilities.
- OAuth2PasswordRequestForm: A FastAPI class for handling OAuth2 password request form data.
- Depends: A FastAPI class used for dependency injection.
- status: A module from FastAPI for defining HTTP status codes.
- timedelta: From the datetime module, used for calculating token expiration time.
- Download All libraries from above requirements
- Run the FastAPI application using the following command:
uvicorn main:app --reload
. - Create MongoDb Dabase URI to connect database.