A fast, lightweight RESTful API built with Node.js, Express, Prisma ORM, and PostgreSQL. Designed for receipt and sales tracking with automated financial calculations (subtotals, totals, and profits) and a role-based authentication model.
- User Capacity: Strictly designed to accommodate a maximum of 6 users total (including the Superadmin, Admins, and Staff).
- Access Control: Public self-registration is disabled. Only Admins and Superadmins can create accounts for new staff members.
- Automated Credential Distribution: When an admin creates a staff account, a secure temporary password is automatically generated and emailed directly to the staff member's email address.
- Authentication & Authorization: Stateless JWT authentication with long-lived (7-day) sessions. Role-based access control (
SUPER_ADMIN,ADMIN,STAFF). - Receipt Management: Full CRUD capabilities for receipts with automatic currency (
NGN,USD,EURO,POUNDS) and status handling (paid,in-transit,delivered,refunded). - Item Tracking & Automated Math: Dynamic item insertion, updates, and deletion. Automated background recalculation of receipt subtotals, totals, and overall profit.
- Database Resiliency: Foreign key protections using
SetNullon receipt creators to ensure zero data loss when users are deleted, andCascadedeletion on items when a receipt is removed.
- Runtime: Node.js (ES Modules)
- Framework: Express.js
- Database: PostgreSQL
- ORM: Prisma
- Authentication: JSON Web Tokens (
jsonwebtoken) &bcryptjs
- User:
id(Int),email,name,password,role(SUPER_ADMIN, ADMIN, STAFF),active(Boolean). - Receipt:
id(Int),createdById(Int?),createdByName(String),customerName,customerEmail,customerPhone,paidDate,receivedDate,trackingId,status,currency,shippingFee,subtotal,total,totalProfit,notes. - Item:
id(Int),receiptId(Int),itemName,costPrice,sellingPrice,totalAmount,profit.
- Node.js (v18+ recommended)
- PostgreSQL database running locally or hosted online.
git clone https://github.com/thinkingtek/receipt-generator-api.git
cd receipt-generator-api
npm installCreate a .env file in the root directory and configure the following variables:
PORT=5000
DATABASE_URL="postgresql://username:password@localhost:5432/receipt_db?schema=public"
JWT_SECRET="your_super_secret_jwt_key_here"
NODE_ENV="development"Sync your Prisma schema with your PostgreSQL database:
# Push schema directly to database (for fast development)
npx prisma db push
# Generate Prisma Client
npx prisma generateTo reset the database during development at any point:
npx prisma migrate reset# Development mode
npm run dev
# Production mode
npm startPOST /api/auth/login- Authenticate user & receive 7-day JWT.
GET /api/v1/receipts- Retrieve all receipts (includes attached items).POST /api/v1/create-receipts- Create a new receipt header.PUT /api/v1/receipts/:id/update-receipt- Update receipt details or shipping fee.DELETE /api/v1/receipts/:id/details- Delete a receipt (cascades to items).
POST /api/v1/receipts/:receiptId/items- Add item to receipt & trigger auto-recalculation.PUT /api/v1/receipts/items/:itemId- Update item pricing/quantity & recalculate receipt totals.DELETE /api/v1/receipts/items/:itemId- Remove item from receipt & recalculate receipt totals.
Protected routes require a Bearer token in the request header:
Authorization: Bearer <your_jwt_token>