Skip to content

iyousefosama/Express-Subscription

Repository files navigation

Subscription API

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.

Key Technologies & Libraries

  • 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.

Features

  • 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

Getting Started

Prerequisites

  • Node.js v18 or later
  • npm v9 or later
  • MongoDB instance (local or cloud)

Installation

npm install

Environment Variables

Set up your environment variables in config/env.ts (see example in the file).

Running the App

npm run start

For development with hot reload:

npm run dev

API Endpoints

Auth Endpoints

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.

User Endpoints

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

Subscription Endpoints

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

Subscription Model

{
  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)
}

Seeding Dummy Data

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!

About

A robust RESTful API for managing user subscriptions, built with Express.js, TypeScript, and Mongoose.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published