Here's a detailed documentation of the API endpoints along with usage examples for your Flask application.
- URL:
/register - Method:
POST - Description: Register a new user in the system.
Request:
-
Headers:
Content-Type:application/json
-
Body (JSON):
{ "username": "john_doe", "email": "john@example.com", "password": "securePassword123" }
Response:
-
Success:
- Status Code:
201 Created - Body:
{ "message": "User registered successfully" }
- Status Code:
-
Error:
- Status Code:
400 Bad Request - Possible Errors:
- Missing fields:
{ "message": "Missing required fields" } - Invalid email format:
{ "message": "Invalid email format" } - User already exists:
{ "message": "User already exists" }
- Missing fields:
- Status Code:
Usage Example (cURL):
curl -X POST http://localhost:5000/register \
-H "Content-Type: application/json" \
-d '{"username": "john_doe", "email": "john@example.com", "password": "securePassword123"}'- URL:
/login - Method:
POST - Description: Authenticate a user and provide a JWT token.
Request:
-
Headers:
Content-Type:application/json
-
Body (JSON):
{ "email": "john@example.com", "password": "securePassword123" }
Response:
-
Success:
- Status Code:
200 OK - Body:
{ "access_token": "your_jwt_token_here" }
- Status Code:
-
Error:
- Status Code:
400 Bad Request - Possible Errors:
- Missing fields:
{ "message": "Missing required fields" }
- Missing fields:
- Status Code:
401 Unauthorized - Error:
- Invalid credentials:
{ "message": "Invalid email or password" }
- Invalid credentials:
- Status Code:
Usage Example (cURL):
curl -X POST http://localhost:5000/login \
-H "Content-Type: application/json" \
-d '{"email": "john@example.com", "password": "securePassword123"}'- URL:
/logout - Method:
POST - Description: Invalidate the JWT token for the current user.
Request:
-
Headers:
Content-Type:application/jsonAuthorization:Bearer your_jwt_token_here
-
Body: None
Response:
- Success:
- Status Code:
200 OK - Body:
{ "message": "Successfully logged out" }
- Status Code:
Usage Example (cURL):
curl -X POST http://localhost:5000/logout \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_jwt_token_here"- URL:
/add_expense - Method:
POST - Description: Add a new expense for the authenticated user.
Request:
-
Headers:
Content-Type:application/jsonAuthorization:Bearer your_jwt_token_here
-
Body (JSON):
{ "type_expense": "Food", "description_expense": "Lunch at restaurant", "date_purchase": "2024-08-19", "amount": 15.99 }
Response:
-
Success:
- Status Code:
201 Created - Body:
{ "message": "Expense added successfully" }
- Status Code:
-
Error:
- Status Code:
400 Bad Request - Possible Errors:
- Missing fields or validation errors:
{ "message": "Validation failed: missing or incorrect fields" }
- Missing fields or validation errors:
- Status Code:
Usage Example (cURL):
curl -X POST http://localhost:5000/add_expense \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_jwt_token_here" \
-d '{"type_expense": "Food", "description_expense": "Lunch at restaurant", "date_purchase": "2024-08-19", "amount": 15.99}'- URL:
/expenses - Method:
GET - Description: Retrieve a list of expenses for a specific user.
Request:
-
Headers:
Content-Type:application/jsonAuthorization:Bearer your_jwt_token_here
-
Query Parameters:
user: The username whose expenses are to be retrieved.
Response:
- Success:
- Status Code:
200 OK - Body:
[ { "expense_id": 1, "type_expense": "Food", "description_expense": "Lunch at restaurant", "date_purchase": "2024-08-19", "amount": 15.99, "user_name": "john_doe" }, ... ]
- Status Code:
Usage Example (cURL):
curl -X GET "http://localhost:5000/expenses?user=john_doe" \
-H "Authorization: Bearer your_jwt_token_here"- URL:
/mod_expense - Method:
POST - Description: Modify an existing expense or delete it.
Request:
-
Headers:
Content-Type:application/jsonAuthorization:Bearer your_jwt_token_here
-
Body (JSON):
{ "Type": "Food", "Description": "Lunch at a cafe", "Date": "2024-08-19", "Amount": 12.50, "Delete": false }
Response:
-
Success:
- Status Code:
200 OK - Body:
{ "message": "Expense modified successfully" }
- Status Code:
-
Error:
- Status Code:
400 Bad Request - Possible Errors:
- Expense not found or validation errors:
{ "message": "Expense not found or validation failed" }
- Expense not found or validation errors:
- Status Code:
Usage Example (cURL):
curl -X POST http://localhost:5000/mod_expense \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_jwt_token_here" \
-d '{"Type": "Food", "Description": "Lunch at a cafe", "Date": "2024-08-19", "Amount": 12.50, "Delete": false}'