RESTful API for subscription management built with Express, TypeScript, and Prisma.
- JWT Authentication: Secure user signup/login
- Role-Based Access: User and company isolation
- Customer Management: CRUD operations for customers
- Subscription Tracking: Manage subscriptions with status tracking
- Plan Management: Create and manage subscription plans
- Dashboard Analytics: Aggregate statistics and metrics
- PostgreSQL Database: Relational data with Prisma ORM
- Node.js - Runtime environment
- Express - Web framework
- TypeScript - Type safety
- Prisma - ORM for PostgreSQL
- PostgreSQL - Database
- JWT - Authentication tokens
- bcryptjs - Password hashing
src/
├── config/ # Middleware configuration (CORS, etc.)
├── error-handling/ # Global error handlers
├── lib/ # Prisma client setup
├── middleware/ # JWT authentication middleware
├── routes/ # API route handlers
└── types/ # TypeScript type definitions
prisma/
├── schema.prisma # Database schema
└── migrations/ # Database migrations
- Node.js 18+
- PostgreSQL 14+
- npm or yarn
- Clone the repository
git clone <repository-url>
cd subly-backend- Install dependencies
npm install- Configure environment variables
Create a .env file in the root:
PORT=5005
DATABASE_URL=postgresql://user:password@localhost:5432/subly
TOKEN_SECRET=your-super-secret-jwt-key
ORIGIN=http://localhost:5173- Set up database
Run Prisma migrations:
npx prisma migrate devGenerate Prisma client:
npx prisma generate- Start development server
npm run devThe API will be available at http://localhost:5005
- User: Authentication and company association
- Company: Multi-tenant isolation
- Customer: Customer records per company
- Plan: Subscription plans
- Subscription: Active subscriptions linking customers and plans
SubscriptionStatus: ACTIVE, CANCELLED, EXPIREDSubscriptionEventType: CREATED, RENEWED, CANCELLED, EXPIREDRole: USER, ADMIN
POST /auth/signup- Register new userPOST /auth/login- Login and get JWT tokenGET /auth/verify- Verify JWT token
GET /api/customers- List all customersPOST /api/customers- Create customerPUT /api/customers/:id- Update customerDELETE /api/customers/:id- Delete customer
GET /api/plans- List all plansPOST /api/plans- Create planPUT /api/plans/:id- Update planDELETE /api/plans/:id- Delete plan
GET /api/subscriptions- List subscriptions (with filters)POST /api/subscriptions- Create subscriptionPUT /api/subscriptions/:id- Update subscriptionDELETE /api/subscriptions/:id- Delete subscription
GET /api/dashboard/stats- Get aggregate statistics
Protected routes require a JWT token in the Authorization header:
Authorization: Bearer <token>
The JWT middleware automatically:
- Validates tokens
- Extracts user and company info
- Attaches to
req.payload
npm run buildnpm start- Connect your repository to Railway
- Set environment variables:
DATABASE_URL- PostgreSQL connection stringTOKEN_SECRET- JWT secret keyORIGIN- Frontend URL (without trailing slash)
- Railway auto-detects
railway.jsonconfiguration - Deploy
npm run dev- Start development server with nodemonnpm run build- Compile TypeScript to JavaScriptnpm start- Run production buildnpx prisma studio- Open Prisma Studio (database GUI)npx prisma migrate dev- Create and apply migrations
npx prisma migrate dev- Create migration in developmentnpx prisma migrate deploy- Apply migrations in productionnpx prisma generate- Generate Prisma clientnpx prisma studio- Launch database GUI
CORS is configured in src/config/index.ts. The ORIGIN environment variable must match your frontend domain (without trailing slash).
PostgreSQL connection is managed via Prisma. Update DATABASE_URL in .env or use Railway's auto-injected variable.
https://github.com/PauSerranoHerraiz/subly-frontend
Contributions are welcome! Please open an issue or PR.