A robust REST API for managing personal wallet transactions with rate limiting, database persistence, and automated health monitoring.
- Transaction Management: Create, read, and delete wallet transactions
- Financial Analytics: Get balance, income, and expense summaries
- Rate Limiting: Built-in protection against API abuse
- Database Persistence: PostgreSQL with Neon serverless
- Health Monitoring: Automated health checks every 14 minutes
- CORS Support: Cross-origin resource sharing enabled
- Prerequisites
- Installation
- Environment Variables
- Database Schema
- API Endpoints
- Rate Limiting
- Health Monitoring
- Development
- Deployment
- Node.js (v16 or higher)
- PostgreSQL database (Neon recommended)
- Upstash Redis account (for rate limiting)
-
Clone the repository
git clone https://github.com/regis-mugisha/expo-wallet-api.git cd expo-wallet-api
-
Install dependencies
npm install
-
Set up environment variables
cp .env.example .env # Edit .env with your configuration
-
Start the server
# Development mode npm run dev # Production mode npm start
Create a .env
file in the root directory with the following variables:
# Database Configuration
DATABASE_URL=postgresql://username:password@host:port/database(from neon console)
# Server Configuration
PORT=5001
NODE_ENV=development
# API Configuration
API_URL=https://expo-wallet-api.onrender.com/api/health
# Upstash Redis (for rate limiting)
UPSTASH_REDIS_REST_URL=your-redis-url
UPSTASH_REDIS_REST_TOKEN=your-redis-token
The API uses a PostgreSQL database with the following schema:
Column | Type | Constraints | Description |
---|---|---|---|
id | SERIAL | PRIMARY KEY | Unique transaction identifier |
user_id | VARCHAR(255) | NOT NULL | User identifier |
title | VARCHAR(255) | NOT NULL | Transaction description |
amount | DECIMAL(10,2) | NOT NULL | Transaction amount |
category | VARCHAR(50) | NOT NULL | Transaction category |
created_at | DATE | NOT NULL, DEFAULT | Transaction creation date |
http://localhost:5001
GET /api/health
Response:
{
"status": "ok"
}
GET /api/transactions/:userId
Parameters:
userId
(path): User identifier
Response:
[
{
"id": 1,
"user_id": "user123",
"title": "Grocery Shopping",
"amount": -50.0,
"category": "Food",
"created_at": "2024-01-15"
}
]
POST /api/transactions
Request Body:
{
"user_id": "user123",
"title": "Salary",
"amount": 2000.0,
"category": "Income"
}
Response:
{
"id": 2,
"user_id": "user123",
"title": "Salary",
"amount": 2000.0,
"category": "Income",
"created_at": "2024-01-15"
}
DELETE /api/transactions/:id
Parameters:
id
(path): Transaction identifier
Response:
{
"message": "Transaction deleted successfully."
}
GET /api/transactions/summary/:userId
Parameters:
userId
(path): User identifier
Response:
{
"balance": 1500.0,
"income": 3000.0,
"expenses": -1500.0
}
The API implements rate limiting using Upstash Redis:
- Limit: 100 requests per minute per IP address
- Window: Sliding window algorithm
- Response: 429 Too Many Requests when limit exceeded
The API includes automated health monitoring:
- Schedule: Every 14 minutes
- Endpoint:
/api/health
- Purpose: Keep the server active and monitor availability
- Production Only: Runs only in production environment
src/
βββ config/
β βββ cron.js # Health monitoring scheduler
β βββ db.js # Database connection
β βββ upstash.js # Rate limiting configuration
βββ controllers/
β βββ transactionsController.js # Business logic
βββ middleware/
β βββ rateLimiter.js # Rate limiting middleware
βββ routes/
β βββ transactionsRoute.js # API routes
βββ server.js # Main application file
# Start development server with hot reload
npm run dev
# Start production server
npm start
The API includes comprehensive error handling:
- 400 Bad Request: Missing required fields
- 404 Not Found: Transaction not found
- 429 Too Many Requests: Rate limit exceeded
- 500 Internal Server Error: Server-side errors
- Environment Variables: Ensure all required environment variables are set
- Database: Use a production-ready PostgreSQL instance
- Rate Limiting: Configure Upstash Redis for production
- Health Monitoring: The cron job will automatically start in production
The API can be deployed on various platforms:
- Vercel: Serverless deployment
- Railway: Easy PostgreSQL integration
- Heroku: Traditional hosting
- DigitalOcean: VPS deployment
For production deployment, ensure:
NODE_ENV=production
DATABASE_URL=your-production-database-url
API_URL=https://your-production-domain.com/api/health
# Get user transactions
curl -X GET "http://localhost:5001/api/transactions/user123"
# Create a transaction
curl -X POST "http://localhost:5001/api/transactions" \
-H "Content-Type: application/json" \
-d '{
"user_id": "user123",
"title": "Coffee",
"amount": -5.50,
"category": "Food"
}'
# Get transaction summary
curl -X GET "http://localhost:5001/api/transactions/summary/user123"
// Get transactions
const response = await fetch("/api/transactions/user123");
const transactions = await response.json();
// Create transaction
const newTransaction = await fetch("/api/transactions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
user_id: "user123",
title: "Salary",
amount: 2000.0,
category: "Income",
}),
});
-
Database Connection Error
- Verify
DATABASE_URL
is correct - Ensure database is accessible
- Verify
-
Rate Limiting Issues
- Check Upstash Redis configuration
- Verify environment variables
-
Health Check Failures
- Ensure
API_URL
is set correctly - Check server logs for errors
- Ensure
The API provides detailed logging:
- Transaction operations
- Error messages
- Rate limiting events
- Health check status
This project is licensed under the ISC License.
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
Version: 1.0.0
Last Updated: July 2025