Skip to content

dodopayments/fastapi-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dodo Payments FastAPI Boilerplate

Join Discord

A minimal FastAPI boilerplate for integrating Dodo Payments into your Python application.

Features

  • Quick Setup - Get started in under 5 minutes
  • Payment Integration - Pre-configured checkout flow using dodopayments Python SDK
  • Modern UI - Clean, dark-themed pricing page with Tailwind CSS
  • Webhook Handler - Ready-to-use webhook endpoint for payment events
  • Customer Portal - One-click subscription management
  • Type Safety - Fully typed with Pydantic models
  • Pre-filled Checkout - Demonstrates passing customer data to improve UX

Prerequisites

Before you begin, make sure you have:

  • Python 3.8+ (required for FastAPI and the Dodo Payments SDK)
  • Dodo Payments account (to access API and Webhook Keys from dashboard)

Quick Start

1. Clone the Repository

git clone https://github.com/dodopayments/fastapi-boilerplate.git
cd fastapi-boilerplate

2. Create Virtual Environment

python3 -m venv venv
source venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

4. Get API Credentials

Sign up at Dodo Payments and get your credentials from the dashboard:

Make sure you're in Test Mode while developing!

5. Configure Environment Variables

Create a .env file in the root directory:

cp .env.example .env

Update the values with your Dodo Payments credentials:

DODO_PAYMENTS_API_KEY=your_api_key_here
DODO_PAYMENTS_WEBHOOK_KEY=your_webhook_signing_key_here
DODO_PAYMENTS_RETURN_URL=http://localhost:8000
DODO_PAYMENTS_ENVIRONMENT=test_mode

6. Add Your Products

Update app/lib/products.py with your actual product IDs from Dodo Payments:

products: List[Product] = [
    Product(
        product_id="pdt_001",  # Replace with your product ID
        name="Basic Plan",
        description="Get access to basic features and support",
        price=9999,  # in cents
        features=[
            "Access to basic features",
            "Email support",
            "1 Team member",
            "Basic analytics",
        ],
    ),
    # ... add more products
]

7. Run the Development Server

uvicorn app.main:app --reload --port 8000

Open http://localhost:8000 to see your pricing page!

Project Structure

app/
├── api/
│   ├── checkout.py      # Checkout session handler
│   ├── portal.py        # Customer portal redirect
│   └── webhook.py       # Webhook event handler
├── core/
│   └── config.py        # Configuration settings
├── lib/
│   ├── customers.py     # Customer utilities (normalization, payload helpers)
│   └── products.py      # Product definitions
├── templates/
│   ├── base.html        # Base template
│   └── index.html       # Pricing page
├── static/
│   └── css/             # Custom styles (if needed)
└── main.py              # App entry point

Customization

Update Product Information

Edit app/lib/products.py to modify:

  • Product IDs (from your Dodo dashboard)
  • Pricing
  • Features
  • Descriptions

Pre-fill Customer Data

In app/templates/index.html, replace the hardcoded values with your actual user data:

const customerData = {
    name: "John Doe",  // Replace with actual logged-in user's name
    email: "john@example.com"  // Replace with actual logged-in user's email
};

In a production app, you would get this data from your authentication system (e.g., session, JWT token, etc.).

Update Customer Portal Link

In app/templates/index.html, replace the hardcoded customer ID:

const customerId = "cus_001";  // Replace with actual customer ID

Webhook Events

The boilerplate demonstrates handling two webhook events in app/api/webhook.py:

  • subscription.active - Triggered when a subscription becomes active
  • payment.succeeded - Triggered when a payment is successful

Add your business logic inside these handlers:

if event_type == "subscription.active":
    # Grant access to your product
    # Update user database
    # Send welcome email
    pass

Add more webhook events as needed.

For local development, you can use tools like ngrok to create a secure tunnel to your local server and use it as your webhook URL. Remember to update your .env file with the correct webhook verification key.

Deployment

Build for Production

For production, you can use Gunicorn with Uvicorn workers:

pip install gunicorn
gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker

Deployment targets

You can deploy this FastAPI application to any platform that supports Python/FastAPI: Note: FastAPI on Vercel is possible via Python Serverless Functions but is outside the scope of this boilerplate. Prefer Railway/Render for FastAPI.

Don't forget to add your environment variables (API key, Webhook key, Return URL, Environment) in the deployment platform's dashboard.

Deploy with Vercel

Update Webhook URL

After deploying, update your webhook URL in the Dodo Payments Dashboard:

https://yourdomain.com/api/webhook

Learn More

Support

Need help? Reach out:

About

Minimal FastAPI boilerplate for integrating Dodo Payments into your Python application.

Topics

Resources

Stars

Watchers

Forks

Contributors 2

  •  
  •