The Expense Tracker API is a RESTful service built with the Gin framework in Go. It provides endpoints to manage user credentials and track expenses, including features like JWT-based authentication, secure password hashing, and CRUD operations on expense records.
- Authentication:
- User Signup and Login with JWT-based token generation.
- Expense Management:
- Add, update, delete, and retrieve expenses.
- Filter expenses by categories like Groceries, Electronics, etc.
- Security:
- Passwords are hashed for secure storage.
- Token validation with middleware for protected routes.
- Go (v1.20+ recommended)
- PostgreSQL/MySQL/SQLite (as the database backend)
- cURL (for testing API endpoints)
- Git (for version control)
-
Clone the repository:
git clone https://github.com/username/expense-tracker-api.git cd expense-tracker-api
-
Install dependencies:
go mod download
-
Set up the database:
- Create a database for the project.
- Update the connection string in the
config.yml
file or as an environment variable.
-
Run the server:
go run main.go
Method | Endpoint | Description | Auth Required |
---|---|---|---|
POST | /signup |
User Signup | No |
POST | /login |
User Login (JWT Token) | No |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
POST | /expenseAPI/expense |
Add a new expense | Yes |
GET | /expenseAPI/expense |
Get all expenses | Yes |
GET | /expenseAPI/expense/:id |
Get expense by ID | Yes |
PUT | /expenseAPI/expense/:id |
Update expense by ID | Yes |
DELETE | /expenseAPI/expense/:id |
Delete expense by ID | Yes |
Variable | Description |
---|---|
PORT |
Server listening port (default: 8080) |
DATABASE_URL |
Connection string for the database |
JWT_SECRET |
Secret key for signing JWT tokens |
-
Start the server:
go run main.go
-
Access the API: Visit
http://localhost:8080
to access the API.
curl -X POST http://localhost:8080/signup \
-H "Content-Type: application/json" \
-d '{"username": "testuser", "password": "testpassword"}'
curl -X POST http://localhost:8080/login \
-H "Content-Type: application/json" \
-d '{"username": "testuser", "password": "testpassword"}'
curl -X POST http://localhost:8080/expenseAPI/expense \
-H "Authorization: Bearer <JWT_TOKEN>" \
-H "Content-Type: application/json" \
-d '{"category": "Groceries", "amount": 50.75, "description": "Lunch", "date": "2024-11-13"}'
curl -X GET http://localhost:8080/expenseAPI/expense \
-H "Authorization: Bearer <JWT_TOKEN>"
This project is licensed under the MIT License.
Feel free to contribute to this project or use it as a reference for your own projects! 🚀