Overview
The Budgets API allows users to set spending limits for specific categories over a defined time period. The API automatically calculates spending progress, alerts for exceeded budgets, and tracks remaining days.Budgets are tracked against expense transactions (
type='chi') within the specified date range and category filters.Authentication
All endpoints require session-based authentication via the@api_login_required decorator.
Get All Budgets
Retrieve all budgets for the authenticated user with real-time spending calculations. Endpoint:GET /api/budgets
Response
Returns an array of budget objects with calculated spending progress.Response Fields
Unique 8-character budget ID
Budget name (e.g., “Monthly Entertainment Budget”)
Budget limit in VND
Total amount spent so far (calculated from transactions)
Spending progress percentage (0-100, capped at 100)
True if spending exceeds the budget limit
Budget start date (YYYY-MM-DD format)
Budget end date (YYYY-MM-DD format)
Number of days remaining (minimum 0)
List of categories tracked by this budget
Example Request
Create Budget
Create a new budget with spending limits and category tracking. Endpoint:POST /api/budgets
Request Body
Budget name (e.g., “Q1 Marketing Budget”)
Budget limit amount in VND
Start date in YYYY-MM-DD format
End date in YYYY-MM-DD format
Array of category IDs to track (e.g.,
["cat001", "cat002"])Example Request
Response
Error Response
Delete Budget
Delete an existing budget. Endpoint:DELETE /api/budgets/{budget_id}
Path Parameters
The unique budget ID to delete
Example Request
Response
Implementation Details
Spending Calculation
The API calculates spending by summing all expense transactions that match:- User ID: Belongs to the authenticated user
- Transaction Type:
type='chi'(expenses only) - Category Match:
category_idis in the budget’s tracked categories - Date Range: Transaction date falls between
start_dateandend_date
budget.py
Progress Calculation
Progress percentage is calculated as:Days Remaining
Days left is calculated from today’s date:Database Schema
Budgets use a many-to-many relationship with categories through thengansach_danhmuc association table:
models.py
Related Endpoints
- Transactions API - Transactions counted toward budget spending
- Categories API - Manage categories tracked by budgets
- Reports API - View spending reports filtered by budget categories
