Backend API for StackSave mobile application built with Express.js and PostgreSQL.
- User authentication and management
- Savings goals tracking
- Deposit management with automatic streak updates
- Transaction history
- Daily growth/earnings tracking
- Payment methods management
- Streak system (Duolingo-style)
- Node.js - Runtime environment
- Express.js - Web framework
- PostgreSQL - Database
- pg - PostgreSQL client for Node.js
- CORS - Cross-origin resource sharing
- dotenv - Environment variable management
npm installCreate a PostgreSQL database:
createdb stacksaveInitialize the database schema:
psql -d stacksave -f db/schema.sqlOr use the npm script:
npm run db:initCopy .env.example to .env and update with your configuration:
cp .env.example .envUpdate the .env file:
DATABASE_URL=postgresql://username:password@localhost:5432/stacksave
PORT=3000
NODE_ENV=development
CORS_ORIGIN=http://localhost:8081
Development mode (with auto-reload):
npm run devProduction mode:
npm startThe server will start on http://localhost:3000
GET /health- Check API and database status
POST /api/auth/login- Login or create user with wallet addressPOST /api/auth/verify- Verify wallet signature (placeholder)
GET /api/users/:userId- Get user profilePUT /api/users/:userId/mode- Update user mode (lite/pro)PUT /api/users/:userId/balance- Update user balanceGET /api/users/:userId/growth- Get daily growth dataPOST /api/users/:userId/growth- Add daily growth entry
GET /api/goals/:userId- Get all goals for userGET /api/goals/:userId/main- Get main goalPOST /api/goals/:userId- Create new goalPUT /api/goals/:goalId- Update goalDELETE /api/goals/:goalId- Delete goal
GET /api/deposits/:userId- Get all depositsPOST /api/deposits/:userId- Create new deposit (auto-updates goals, balance, streaks)PUT /api/deposits/:depositId/status- Update deposit status
GET /api/transactions/:userId- Get all transactionsGET /api/transactions/:userId/recent- Get recent transactions (last 10)POST /api/transactions/:userId/withdrawal- Create withdrawalPOST /api/transactions/:userId/earnings- Record earningsPUT /api/transactions/:transactionId/status- Update transaction status
GET /api/streaks/:userId- Get streak informationPOST /api/streaks/:userId/check- Check and update streakPOST /api/streaks/:userId/reset- Reset current streak
GET /api/payment-methods/:userId- Get all payment methodsGET /api/payment-methods/:userId/default- Get default payment methodPOST /api/payment-methods/:userId- Add payment methodPUT /api/payment-methods/:paymentMethodId- Update payment methodDELETE /api/payment-methods/:paymentMethodId- Delete payment method
- users - User accounts and wallet addresses
- savings_goals - User savings goals
- deposits - Deposit records
- transactions - All transaction types (deposit, withdrawal, earnings)
- streaks - User streak tracking
- daily_growth - Daily earnings and growth tracking
- payment_methods - Linked payment methods
See db/schema.sql for complete schema definition.
backend/
├── db/
│ └── schema.sql # Database schema
├── src/
│ ├── config/
│ │ └── db.js # Database connection
│ ├── routes/
│ │ ├── auth.js # Authentication routes
│ │ ├── users.js # User routes
│ │ ├── goals.js # Goals routes
│ │ ├── deposits.js # Deposits routes
│ │ ├── transactions.js # Transactions routes
│ │ ├── streaks.js # Streaks routes
│ │ └── paymentMethods.js # Payment methods routes
│ └── index.js # Main server file
├── .env.example # Environment variables template
├── package.json
└── README.md
When a deposit is made, the system automatically:
- Updates the user's streak (increments if consecutive day)
- Resets streak if more than 1 day gap
- Updates longest streak if current exceeds it
- Tracks total number of deposits
All deposit and withdrawal operations use database transactions to ensure data consistency:
- Updates user balance
- Updates goal progress
- Creates transaction records
- Updates streak data
- Records daily growth
Daily growth data tracks:
- Earnings from yield/interest
- Growth percentage
- Whether user made a deposit that day
All endpoints include proper error handling:
- 400 - Bad Request (validation errors)
- 404 - Not Found (resource doesn't exist)
- 500 - Internal Server Error (database/server errors)
Error responses include:
{
"error": "Error message",
"message": "Detailed error description"
}The API uses:
- Connection pooling for efficient database queries
- Query logging in development mode
- Automatic timestamp updates via database triggers
- Graceful shutdown handling
- JWT authentication for secure API access
- Rate limiting
- Input validation middleware
- API documentation with Swagger
- Unit and integration tests
- Blockchain integration for deposits/withdrawals
- Webhook support for payment providers