A modern, production-ready SaaS application template featuring user authentication, subscription management with Stripe, and infrastructure as code with Terraform.
- Features
- Architecture
- Prerequisites
- Quick Start
- Detailed Setup
- Environment Variables
- Development
- Deployment
- API Documentation
- Authentication System: JWT-based authentication with user registration/login
- Subscription Management: Stripe integration for subscription billing
- Modern Frontend: Next.js 15 with TypeScript, Tailwind CSS, and Redux
- FastAPI Backend: Python FastAPI with SQLAlchemy ORM
- Infrastructure as Code: Terraform for GCP deployment
- Kubernetes and Helm: Comes with Kubernetes and Helm options
- Containerized: Docker Compose for local development
- Database: PostgreSQL with Alembic migrations
- CI/CD: Utilizes Github Actions for CI and CD
-
Clone the repository
git clone <repository-url> cd saas-webapp-template
-
Install dependencies
# Backend dependencies pdm install # Frontend dependencies cd frontend pnpm install cd ..
-
Set up environment variables
cp .env.example .env # Edit .env with your configuration -
Start the application
make build up
-
Visit the application
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
- API Docs: http://localhost:8000/docs
The backend is built with FastAPI, SQLAlchemy, and PostgreSQL.
- FastAPI: Web framework
- SQLAlchemy: ORM for database operations
- Alembic: Database migrations
- Stripe: Payment processing
- PyJWT: JWT token handling
- Pytest: Testing framework
# Install Python dependencies
pdm install
# Run database migrations
pdm run alembic upgrade head
# Run tests
make pytest# Start backend with hot reload
make build up-devThe frontend is built with Next.js 15, TypeScript, and Tailwind CSS.
- Next.js 15: React framework
- TypeScript: Type safety
- Tailwind CSS: Styling
- Redux Toolkit: State management
- NextAuth.js: Authentication
- Axios: HTTP client
cd frontend
pnpm installmake build up-devmake build upThe application includes comprehensive Stripe integration for subscription management.
-
Create a Stripe Account
- Sign up at stripe.com
- Get your API keys from the dashboard
-
Configure Environment Variables
STRIPE_SECRET_KEY=sk_test_... STRIPE_WEBHOOK_SECRET=whsec_... PRO_PLAN_PRICE_ID=price_...
-
Create Products and Prices
- Create a product in Stripe Dashboard
- Create a recurring price for your subscription
- Copy the price ID to your environment variables
-
Set up Webhooks
- Configure webhook endpoint:
https://your-domain.com/stripe/webhook - Events to listen for:
customer.subscription.createdinvoice.payment_succeededcustomer.subscription.deleted
- Configure webhook endpoint:
-
Edit
./.github/workflows/main.ymlbuild: runs-on: ubuntu-latest needs: [test, version] strategy: matrix: include: - dockerfile: ./.deploy/docker-images/Dockerfile.frontend dockerhub_repo: your-frontend-repo - dockerfile: ./.deploy/docker-images/Dockerfile.backend dockerhub_repo: your-backend-repo
-
Create some environment variables for the Github Repo
DOCKERHUB_USERNAME=your-dockerhub-username GPAT_TOKEN=your-gpat-token HELM_REPO_PATH=your-helm-repo-path # ex: jaypyles/helm.git CHART_NAME=your-chart-name # ex: saas-webapp-template
You can always comment out the push to helm part of the workflow file or delete, if you would not like to use it.
- Subscription Management: Create, cancel, and reactivate subscriptions
- Webhook Handling: Automatic subscription status updates
- Customer Management: Automatic Stripe customer creation
- Payment Processing: Secure checkout sessions
The infrastructure is managed with Terraform and deploys to Google Cloud Platform.
# Install Google Cloud CLI
curl https://sdk.cloud.google.com | bash
gcloud init
# Install Terraform
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs)"
sudo apt-get update && sudo apt-get install terraform-
Configure Variables Edit
.deploy/terraform/terraform.tfvars:project_id = "your-gcp-project-id" region = "us-central1" frontend_image = "gcr.io/your-project/frontend:latest" backend_image = "gcr.io/your-project/backend:latest"
-
Initialize Terraform
make init-terraform
-
Initialize GCP APIs
make init-gcp-apis project_id=your-project-id
-
Deploy Infrastructure
# Deploy all resources make apply # Deploy specific resource make apply-resource target=module.vpc
- VPC: Virtual Private Cloud with private subnets
- Cloud SQL: Managed PostgreSQL database
- Cloud Run: Serverless containers for frontend and backend
- Secret Manager: Secure environment variable storage
- Load Balancer: HTTPS traffic distribution
# Refresh Terraform state
make terraform-refresh
# Destroy specific resource
make destroy-resource resource=module.vpc
# Destroy all infrastructure
make destroy# Database
DATABASE_URL=postgresql://user:password@localhost:5432/saas_db # could technically be any database as long as supported by sqlalchemy
# Authentication
JWT_SECRET=your-super-secret-jwt-key
# Stripe
STRIPE_SECRET_KEY=sk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...
PRO_PLAN_PRICE_ID=price_...
# Application
APP_MODE=dev # dev | prod
FRONTEND_BASE_URL=http://localhost:3000# Authentication
NEXTAUTH_SECRET=your-nextauth-secret
NEXTAUTH_URL=http://localhost:3000
JWT_SECRET=your-super-secret-jwt-key # same as backend
# API
API_URL=http://localhost:8000# Docker Operations
make build # Build Docker images
make up # Start containers
make down # Stop containers
make up-dev # Start with development overrides
# Testing
make pytest # Run backend tests
# Terraform Operations
make init-terraform # Initialize Terraform
make apply # Deploy infrastructure
make destroy # Destroy infrastructure
make terraform-refresh # Refresh Terraform state
# GCP Operations
make init-gcp-apis # Initialize required GCP APIsRun make for all available commands
-
Start Development Environment
make up-dev
-
Run Tests
make pytest
-
Database Migrations
pdm run alembic revision --autogenerate -m "Description" pdm run alembic upgrade head
- Deploy Infrastructure
make apply
Once the backend is running, you can access:
- Interactive API Docs: http://localhost:8000/docs
- ReDoc Documentation: http://localhost:8000/redoc
- OpenAPI Schema: http://localhost:8000/openapi.json
POST /auth/register- User registrationPOST /auth/login- User loginGET /auth/profile- Get user profile
GET /stripe/create-checkout-session- Create payment sessionPOST /stripe/cancel-subscription- Cancel subscriptionPOST /stripe/webhook- Stripe webhook handler
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Create an issue in the repository
- Check the API documentation
- Review the test files for usage examples