A secure and scalable RESTful banking API built with Node.js, Express, and MongoDB. It provides core banking functionalities including user authentication, account management, and fund transfers with robust transaction handling.
- User Authentication – Register, login, and logout with JWT stored in HTTP‑only cookies.
- Account Management – Create and view bank accounts (supports multiple accounts per user).
- Secure Transactions – Transfer funds between accounts with:
- Idempotency – Prevent duplicate transactions using a unique key.
- Atomic Updates – MongoDB transactions ensure debit/credit operations are consistent.
- Ledger-Based Accounting – Every movement is recorded as a ledger entry for full auditability.
- Email Notifications – Automatic alerts for successful or failed transfers.
- System Administration – Special endpoint to credit funds to any account (for system users).
- Token Blacklisting – Logout invalidates the JWT immediately.
| Layer | Technology |
|---|---|
| Runtime | Node.js |
| Framework | Express |
| Database | MongoDB (Mongoose ODM) |
| Authentication | JWT, bcryptjs, cookie-parser |
| Nodemailer | |
| Environment | dotenv |
| Development | Nodemon |
- Node.js (v14 or higher)
- MongoDB (local or cloud instance like MongoDB Atlas)
- A Gmail account (or other SMTP provider) for email notifications
- Clone the repository
git clone https://github.com/priyan17singh/RESTbank-API.git cd RESTbank-API
-
Install Dependencies
npm install
-
Set Up Environment Variables
Create a
.envfile in the root directory and add the following:PORT=5000 MONGO_URI=your_mongodb_connection_string JWT_SECRET=your_secret_key EMAIL_USER=your_email@gmail.com EMAIL_PASS=your_app_password
# Development
npm run dev
# Production
npm start- All endpoints are prefixed with
/api. - The API uses JSON for request and response bodies.
- Authentication is handled via HTTP-only cookies. The JWT token cookie is automatically set after a successful login.
| Method | Endpoint | Description | Request Body | Response |
|---|---|---|---|---|
POST |
/register |
Register a new user | { name, email, password } |
User object (without password) |
POST |
/login |
Log in and set JWT cookie | { email, password } |
{ message, user } |
POST |
/logout |
Log out (blacklists token) | — | { message } |
| Method | Endpoint | Description | Request Body / Query | Response |
|---|---|---|---|---|
GET |
/myAccounts |
Get all accounts of the authenticated user | — | Array of accounts |
GET |
/balance |
Get balance of a specific account | { accountId } |
{ balance } |
POST |
/create |
Create a new bank account | { currency } (optional, default: INR) |
Created account object |
| Method | Endpoint | Description | Request Body | Response |
|---|---|---|---|---|
POST |
/create |
Transfer funds between accounts | { fromAccount, toAccount, amount, idempotencyKey } |
{ transaction, status } |
POST |
/system/initiate-funds |
System-only: Credit funds to an account | { accountId, amount, description } |
{ message, ledgerEntry } |
Note:
The/system/initiate-fundsendpoint is protected by a specialauthSystemMiddleware. Only users with thesystemUserprivilege can access this endpoint.
POST /api/transactions/create
Cookie: token=eyJhbGciOiJIUzI1NiIs...
Content-Type: application/json
{
"fromAccount": "65f2a1b2c3d4e5f6a7b8c9d0",
"toAccount": "65f2a1b2c3d4e5f6a7b8c9d1",
"amount": 100.50,
"idempotencyKey": "unique-uuid-1234"
}{
"transaction": {
"id": "65f2a1b2c3d4e5f6a7b8c9d2",
"status": "COMPLETED",
"amount": 100.50,
"fromAccount": "...",
"toAccount": "..."
}
}| Variable | Description |
|---|---|
PORT |
Port on which the server runs (default: 5000) |
MONGO_URI |
MongoDB connection string |
JWT_SECRET |
Secret key used to sign JWT tokens |
EMAIL_USER |
Email address used for sending notifications |
CLIENT_ID |
Google cloud client id |
CLIENT_SECRET |
Google cloud client secret |
REFRESH_TOKEN |
Gmail api refresh token |
Currently, no automated test suite has been implemented.
Contributions are welcome! Feel free to add:
- Unit Tests
- Integration Tests
- API Tests
Contributions are always welcome!
- Fork the repository.
- Create a feature branch.
git checkout -b feature/your-feature- Commit your changes.
git commit -m "Add some feature"- Push your branch.
git push origin feature/your-feature- Open a Pull Request.
This project is licensed under the MIT License.
- Built as a learning project by Priyanshu Singh.
- Inspired by real-world banking system requirements.
This API is intended for educational and demonstration purposes only. It is not production-ready out of the box. Before deploying to a live environment, review and improve:
- Security practices
- Error handling
- Logging
- Performance optimizations
- Testing
- Monitoring
- Deployment configuration