Expense Tracker RESTful API built with Laravel 11.
- General Info
- Technologies Used
- Features
- Setup
- Usage
- Authentication
- HTTP Response Codes
- Project Status
- Acknowledgements
Laravel Expense Tracker API is a simple RESTful API that allow users to create, read, update, and delete expenses. It supports categorization and filtering by amount, date, and creation time. The API uses JWT for authentication powered by tymondesigns/jwt-auth. This project is designed to explore and practice working with the CRUD Operation, RESTful API, Data Modeling, and User Authentication in PHP.
- PHP - version 8.4.1
- MySQL - version 8.0.4
- Laravel 11
- User registration: Register a new user using the
POST
method. - User login: Authenticate the user using the
POST
method. - Create a new expense: Create a new expense using the
POST
method. - Update an existing expense: Update an existing expense using the
PUT
method. - Delete an existing expense: Delete an existing expense using the
DELETE
method. - List all past expenses: Get the list of to-do items with pagination using the
GET
method. - Filter past expenses: Get the list of past expenses by category or by specific date range using the
start_date
andend_date
query params.
To run this CLI tool, you’ll need:
- PHP: Version 8.4 or newer
- MySQL: Version 8.0 or newer
- Composer: Version 2.7 or newer
How to install:
-
Clone the repository
git clone https://github.com/krisnaajiep/laravel-expense-tracker-api.git
-
Change the current working directory
cd laravel-expense-tracker-api
-
Install dependecies
composer install
-
Configure
.env
file for configuration.cp .env.example .env
Add
auth
configuration to the.env
fileAUTH_GUARD=api AUTH_PASSWORD_BROKER=users
-
Generate application key
php artisan key:generate
-
Generate JWT secret key
php artisan jwt:secret
-
Run the database migration
php artisan migrate
-
Run the local server
php artisan serve
Example API Endpoints:
-
User Registration
-
Method:
POST
-
Endpoint:
/api/register
-
Request Body:
name
(string) - The name of the user.email
(string) - The email address of the user.password
(string) - The password of the user account.
-
Example Request:
POST /api/register { "name": "John Doe", "email": "john@doe.com", "password": "example_password", }
-
Response:
- Status:
201 Created
- Content-Type:
application/json
- Status:
-
Example Response:
{ "message": "User register successfully", "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9", "token_type": "Bearer", "expires_in": 3600 }
-
-
User Login
-
Method:
POST
-
Endpoint:
/api/login
-
Request Body:
email
(string) - The email address of the user.password
(string) - The password of the user account.
-
Example Request:
POST /api/login { "email": "john@doe.com", "password": "example_password", }
-
Response:
- Status:
200 OK
- Content-Type:
application/json
- Status:
-
Example Response:
{ "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9", "token_type": "Bearer", "expires_in": 3600 }
-
-
Create a new expense
-
Method:
POST
-
Endpoint:
/api/expenses
-
Request Header:
Authorization
(Bearer) - The access token.
-
Request Body:
amount
(string) - The amount of the expense.category
(string) - The category of the expense.description
(string) - The description of the expense.date_time
(string) - The date of the expense
-
Example Request:
POST /api/expenses { "amount": 50, "category": "Groceries", "description": "Buy milk, eggs, and bread", "date_time": "2025-01-30" }
-
Response:
- Status:
201 Created
- Content-Type:
application/json
- Status:
-
Example Response:
{ "message": "Data stored successfully", "data": { "amount": 50, "category": "Groceries", "description": "Buy milk, eggs, and bread", "date_time": "2025-01-30", "user_id": 1, "updated_at": "2025-01-30T15:46:34.000000Z", "created_at": "2025-01-30T15:46:34.000000Z", "id": 2 } }
-
-
Update an existing expense
-
Method:
PUT
-
Endpoint:
api/expenses/{id}
-
Request Header:
Authorization
(Bearer) - The access token.
-
Request Body:
amount
(string) - The amount of the expense.category
(string) - The category of the expense.description
(string) - The description of the expense.date_time
(string) - The date of the expense
-
Example Request:
POST /api/expenses/30 { "amount": 10, "category": "Groceries", "description": "Buy milk, eggs, and bread, and cheese", "date_time": "2025-01-30" }
-
Response:
- Status:
200 OK
- Content-Type:
application/json
- Status:
-
Example Response:
{ "message": "Data updated successfully", "data": { "id": 2, "user_id": 1, "amount": "10.00", "category": "Groceries", "description": "Buy milk, eggs, and bread, and cheese", "date_time": "2025-01-30 00:00:00", "created_at": "2025-01-30T15:46:34.000000Z", "updated_at": "2025-01-30T15:48:57.000000Z" } }
-
-
Delete an existing expense
-
Method:
DELETE
-
Endpoint:
/api/expenses/{id}
-
Request Header:
Authorization
(Bearer) - The access token.
-
Response:
- Status:
204 No Content
- Content-Type:
text/xml
- Status:
-
-
List and filter all past expenses
-
Method:
GET
-
Endpoint:
/api/expenses
-
Request Header:
Authorization
(Bearer) - The access token.
-
Response:
- Status:
200 OK
- Content-Type:
application/json
- Status:
-
Example Response:
{ "total_amount": "40.00", "expenses": { "current_page": 1, "data": [ { "id": 2, "user_id": 1, "amount": "10.00", "category": "Groceries", "description": "Buy milk, eggs, and bread", "date_time": "2025-01-30 00:00:00", "created_at": "2025-01-30T16:10:30.000000Z", "updated_at": "2025-01-30T16:10:30.000000Z" }, { "id": 1, "user_id": 1, "amount": "30.00", "category": "Bills", "description": "Pay electricity and water bills", "date_time": "2025-01-29 00:00:00", "created_at": "2025-01-30T16:10:02.000000Z", "updated_at": "2025-01-30T16:10:02.000000Z" } ], "first_page_url": "http://localhost:8000/api/expenses?page=1", "from": 1, "last_page": 1, "last_page_url": "http://localhost:8000/api/expenses?page=1", "links": [ { "url": null, "label": "« Previous", "active": false }, { "url": "http://localhost:8000/api/expenses?page=1", "label": "1", "active": true }, { "url": null, "label": "Next »", "active": false } ], "next_page_url": null, "path": "http://localhost:8000/api/expenses", "per_page": 10, "prev_page_url": null, "to": 2, "total": 2 } }
-
Params:
category
- Used for filtering expenses based on their category.start_date
- Used for filtering expenses based on start date (Options:past_week
,last_month
,last_3_months
, custom date).end_date
- Used for filtering expenses based on end date (Options: custom date).
-
-
This API uses Bearer Token for authentication. You can generate an access token by registering a new user or login.
You must include an access token in each request to the API with the Authorization request header.
If an API key is missing, malformed, or invalid, you will receive an HTTP 401 Unauthorized response code.
The following status codes are returned by the API depending on the success or failure of the request.
Status Code | Description |
---|---|
200 OK | The request was processed successfully. |
201 Created | The new resource was created successfully. |
401 Unauthorized | Authentication is required or the access token is invalid. |
403 Forbidden | Access to the requested resource is forbidden. |
404 Not Found | The requested resource was not found. |
422 Unprocessable Content | The server understands the request, but cannot process it due to a validation error |
500 Internal Server Error | An unexpected server error occurred. |
Project is: complete.
This project was inspired by roadmap.sh.
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
- Simple, fast routing engine.
- Powerful dependency injection container.
- Multiple back-ends for session and cache storage.
- Expressive, intuitive database ORM.
- Database agnostic schema migrations.
- Robust background job processing.
- Real-time event broadcasting.
Laravel is accessible, powerful, and provides tools required for large, robust applications.
Laravel has the most extensive and thorough documentation and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.
You may also try the Laravel Bootcamp, where you will be guided through building a modern Laravel application from scratch.
If you don't feel like reading, Laracasts can help. Laracasts contains thousands of video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.
We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the Laravel Partners program.
- Vehikl
- Tighten Co.
- WebReinvent
- Kirschbaum Development Group
- 64 Robots
- Curotec
- Cyber-Duck
- DevSquad
- Jump24
- Redberry
- Active Logic
- byte5
- OP.GG
Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the Laravel documentation.
In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.
If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via taylor@laravel.com. All security vulnerabilities will be promptly addressed.
The Laravel framework is open-sourced software licensed under the MIT license.