A robust RESTful API for managing user subscriptions, built with Express.js, TypeScript, and Mongoose. This project enables seamless subscription creation, renewal, cancellation, and user management, making it ideal for SaaS platforms or any service requiring subscription handling.
- Express.js: Fast, unopinionated, minimalist web framework for Node.js.
- TypeScript: Strongly typed programming language that builds on JavaScript.
- Mongoose: Elegant MongoDB object modeling for Node.js.
- JWT (jsonwebtoken): Secure, stateless authentication using JSON Web Tokens.
- bcryptjs: Password hashing for secure user authentication.
- dotenv: Loads environment variables from a .env file.
- cookie-parser: Parse Cookie header and populate req.cookies.
- ESLint & TypeScript-ESLint: Linting and code quality tools.
- tsx: TypeScript execution environment for node.
- User registration and authentication (JWT-based)
- Subscription CRUD (Create, Read, Update, Delete)
- Subscription renewal and cancellation
- User management (list, get, create, delete users)
- Middleware for authentication and error handling
- Modular code structure (controllers, models, routes)
- TypeScript for type safety
npm install
Set up your environment variables in config/env.ts
(see example in the file).
npm run start
For development with hot reload:
npm run dev
Method | Endpoint | Description |
---|---|---|
POST | /api/v1/auth/sign-up | Register a new user |
POST | /api/v1/auth/sign-in | Login a user |
Note: Sign-out is handled entirely on the client side by deleting the JWT token. There is no backend sign-out route.
Method | Endpoint | Description |
---|---|---|
GET | /api/v1/users/ | Get all users |
GET | /api/v1/users/:id | Get a user by ID (auth) |
POST | /api/v1/users/ | Create a new user |
DELETE | /api/v1/users/:id | Delete a user by ID |
All endpoints require authentication.
Method | Endpoint | Description |
---|---|---|
GET | /api/v1/subscription/ | Get all subscriptions |
GET | /api/v1/subscription/:id | Get subscription details |
POST | /api/v1/subscription/ | Create a subscription |
PUT | /api/v1/subscription/:id | Update a subscription |
DELETE | /api/v1/subscription/:id | Delete a subscription |
GET | /api/v1/subscription/user/:id | Get all subscriptions for a user |
PUT | /api/v1/subscription/:id/cancel | Cancel a subscription |
GET | /api/v1/subscription/upcoming-renewals | Get upcoming renewals |
{
name: string,
price: number,
currency: 'USD' | 'EUR' | 'EGP',
frequency: 'daily' | 'weekly' | 'monthly' | 'yearly',
category: 'health' | 'fitness' | 'nutrition' | 'wellness' | 'entertainment' | 'other',
paymentMethod: 'credit-card' | 'paypal' | 'apple-pay' | 'google-pay' | 'other',
status: 'active' | 'cancelled' | 'expired',
startDate: Date,
renewalDate: Date,
user: ObjectId (User reference)
}
A script is provided to seed dummy users and subscriptions for testing.
To run the seed script:
npm run tsx seed.ts
This will create a test user and several subscriptions in your database.
This project was created using Express.js, TypeScript, and Mongoose. Contributions are welcome!