# paddlehq/paddle-nextjs-starter-kit architecture

> Architecture diagram of the GitHub repository paddlehq/paddle-nextjs-starter-kit, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/paddlehq/paddle-nextjs-starter-kit
- Repository: https://github.com/paddlehq/paddle-nextjs-starter-kit

## Overview

##### Project’s Purpose and Main Components
- The project is a full-stack SaaS starter kit for digital product sales and subscription management using Paddle Billing.
- It is built using Next.js and leverages Supabase for authentication and user management, Tailwind CSS with shadcn/ui for components, and integrates with Paddle for billing and checkout.
- The key functionalities include a pricing page, checkout flow, user authentication, a dashboard for managing subscriptions and payments, and webhook endpoints for syncing Paddle events.

A. Frontend (Next.js Application)
- Pages and Views:
- Home, Pricing, Signup, Login, Dashboard (with sections for subscriptions and payments), and error handling.
- Components:
- UI components (buttons, dialogs, forms, etc.) using shadcn/ui.
- Authentication forms, checkout components, home page header, and dashboard layouts.
- Technology/Framework:
- Next.js uses both server-side rendering (SSR) and client-side interactivity.

B. API Routes / Serverless Endpoints (Inside Next.js App)
- API Routes:
- /api endpoints for webhooks (processing Paddle notifications) and possibly other backend logic.
- Authentication & Callback:
- Endpoints for handling auth callbacks and session management.
- Checkout and Payment Processing:
- API logic that interacts with Paddle for initiating checkouts – for example, the checkout route using the provided price IDs.

C. External Services:
- Paddle Billing:
- Payment processing, subscription management, and pricing (via Paddle Checkout, Paddle.js).
- Interactions include checkout initiation from the frontend and webhook callbacks to the API to reflect changes in subscriptions.
- Supabase:
- Authentication service and user data store.
- Manages user session, secure data access, and additional business logic for user management (e.g., subscription and payment details).

D. Database and Data Synchronization:
- Supabase Database:
- Contains user profiles, subscription records, and payment history.
- Synchronization:
- Webhook endpoints process incoming events from Paddle and update the Supabase database accordingly.

E. Deployment and Configuration Artifacts:
- Vercel deployment (as the hosting platform).
- Environment variables and configuration files (.env.local.example, next.config.mjs, etc.) that manage keys and setup.
- Supabase folder for database migrations and seeds, reflecting the intended persistence architecture.

##### How the Components Interact (Data Flow & Dependencies)
- The Client (browser) interacts with the Next.js frontend to access pages like Home, Signup/Login, Pricing, and Dashboard.
- On the Pricing page or during signup, the client triggers checkout actions that call the Next.js API routes which wrap Paddle’s endpoints.
- Paddle processes checkout and billing: after a successful transaction, Paddle sends a webhook callback to /api/webhook (an endpoint in the Next.js app).
- The webhook endpoint validates and processes the event (using the PADDLE_NOTIFICATION_WEBHOOK_SECRET) before updating the Supabase database with subscription and payment details.
- The Dashboard then reads from Supabase (via server-side functions or client-side fetching) to display current subscriptions and payment history.
- Authentication flows (login/signup) leverage Supabase’s auth APIs via Next.js routes/components.

##### Architectural Patterns and Design Principles
- Separation of Concerns:
- The frontend is clearly separated from the backend API logic.
- API routes are responsible for handling external integrations and secure data processing.
- External Integrations:
- The project leverages external services (Paddle Billing and Supabase) for specialized functionalities.
- Modular Components:
- The file structure reveals modular React components (organized under /src/components) and isolated API logic under /src/app/api.
- Serverless Approach:
- API endpoints (including webhooks) suggest a serverless design, aligning with the hurdles of cloud deployment via Vercel.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Nodes
    Browser["Browser (Client)"]:::frontend

    %% Next.js Frontend Pages
    subgraph "Next.js Pages"
        Home["Home Page"]:::frontend
        Pricing["Pricing Page"]:::frontend
        Signup["Signup Page"]:::frontend
        Login["Login Page"]:::frontend
        Dashboard["Dashboard Page"]:::frontend
        Error["Error Page"]:::frontend
    end

    %% UI & Authentication Components
    subgraph "UI Components & Auth Forms"
        UI["Reusable UI Components (shadcn/ui)"]:::frontend
        LoginForm["Login Form"]:::frontend
        SignUpForm["Sign-Up Form"]:::frontend
    end

    %% Checkout Flow Components
    subgraph "Checkout Flow"
        CheckoutDynamic["Checkout Page ([priceId])"]:::frontend
        CheckoutSuccess["Checkout Success Page"]:::frontend
        CheckoutContents["Checkout Contents"]:::frontend
        CheckoutHeader["Checkout Header"]:::frontend
    end

    %% Dashboard Components
    subgraph "Dashboard Components"
        DashboardLayout["Dashboard Layout"]:::frontend
        PaymentsContent["Payments Content"]:::frontend
        Subscriptions["Subscriptions View"]:::frontend
    end

    %% API Routes in Next.js
    subgraph "Next.js API Routes"
        AuthCallback["Auth Callback Endpoint"]:::backend
        WebhookEndpoint["Webhook Endpoint (/api/webhook)"]:::backend
    end

    %% External Services
    subgraph "External Services"
        Paddle["Paddle Billing"]:::external
        Supabase["Supabase (Auth & Database)"]:::external
    end

    %% Deployment & Configuration
    subgraph "Deployment & Config"
        Vercel["Vercel Deployment"]:::config
        EnvConfig["Env & Config Files"]:::config
        DBMigrations["DB Migrations & Seeds"]:::config
    end

    %% Connections
    Browser -->|"accesses"| Home
    Browser -->|"accesses"| Pricing
    Browser -->|"accesses"| Signup
    Browser -->|"accesses"| Login
    Browser -->|"accesses"| Dashboard
    Browser -->|"accesses"| Error

    %% UI Components integration
    Home -->|"renders"| UI
    Signup -->|"displays"| LoginForm
    Signup -->|"displays"| SignUpForm
    Login -->|"displays"| LoginForm

    %% Checkout Flow
    Pricing -->|"initiates checkout"| CheckoutDynamic
    CheckoutDynamic -->|"uses"| CheckoutContents
    CheckoutDynamic -->|"uses"| CheckoutHeader
    CheckoutDynamic -->|"calls"| Paddle
    CheckoutSuccess -->|"confirms"| CheckoutDynamic

    %% Authentication via API
    Signup -->|"triggers auth"| AuthCallback
    Login -->|"triggers auth"| AuthCallback

    %% Webhook and Data Sync
    Paddle -->|"sends webhook"| WebhookEndpoint
    WebhookEndpoint -->|"updates"| Supabase

    %% Dashboard Data
    Dashboard -->|"displays data from"| DashboardLayout
    DashboardLayout -->|"fetches"| Supabase
    DashboardLayout -->|"includes"| PaymentsContent
    DashboardLayout -->|"includes"| Subscriptions

    %% Deployment Connections
    Vercel -->|"hosts"| Home
    Vercel -->|"hosts"| AuthCallback
    Vercel -->|"hosts"| WebhookEndpoint
    Vercel -->|"hosts"| CheckoutDynamic
    Vercel -->|"hosts"| DashboardLayout
    Vercel -->|"hosts"| UI
    Vercel -->|"hosts"| LoginForm
    Vercel -->|"hosts"| SignUpForm
    Vercel -->|"hosts"| CheckoutContents
    Vercel -->|"hosts"| CheckoutHeader
    Vercel -->|"hosts"| PaymentsContent
    Vercel -->|"hosts"| Subscriptions
    Vercel -->|"uses"| EnvConfig
    Vercel -->|"uses"| DBMigrations

    %% Styles
    classDef frontend fill:#b3cde0,stroke:#03396c,stroke-width:2px;
    classDef backend fill:#ccebc5,stroke:#2a9d8f,stroke-width:2px;
    classDef external fill:#fdd49e,stroke:#d95f02,stroke-width:2px;
    classDef config fill:#f2e6d9,stroke:#b58836,stroke-width:2px;

    %% Click Events for Frontend Pages & Views
    click Home "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/app/page.tsx"
    click Signup "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/app/signup/page.tsx"
    click Login "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/app/login/page.tsx"
    click Dashboard "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/app/dashboard/page.tsx"
    click Error "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/app/error/page.tsx"

    %% Click Events for UI Components
    click UI "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/components/ui/button.tsx"
    click UI "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/components/ui/card.tsx"
    click LoginForm "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/components/authentication/login-form.tsx"
    click SignUpForm "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/components/authentication/sign-up-form.tsx"

    %% Click Events for Checkout Flow
    click CheckoutDynamic "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/app/checkout/[priceId]/page.tsx"
    click CheckoutSuccess "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/app/checkout/success/page.tsx"
    click CheckoutContents "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/components/checkout/checkout-contents.tsx"
    click CheckoutHeader "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/components/checkout/checkout-header.tsx"

    %% Click Events for Dashboard Components
    click DashboardLayout "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/app/dashboard/layout.tsx"
    click DashboardLayout "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/components/dashboard/layout/dashboard-layout.tsx"
    click PaymentsContent "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/components/dashboard/payments/payments-content.tsx"
    click Subscriptions "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/components/dashboard/subscriptions/subscriptions.tsx"

    %% Click Events for API Routes
    click AuthCallback "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/app/auth/callback/route.ts"
    click WebhookEndpoint "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/app/api/webhook/route.ts"
    click WebhookEndpoint "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/utils/paddle/process-webhook.ts"

    %% Click Events for Paddle Integration
    click Paddle "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/utils/paddle/get-paddle-instance.ts"

    %% Click Events for Supabase Integration
    click Supabase "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/utils/supabase/client.ts"
    click Supabase "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/src/utils/supabase/server.ts"

    %% Click Events for Deployment & Config Artifacts
    click EnvConfig "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/.env.local.example"
    click EnvConfig "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/next.config.mjs"
    click DBMigrations "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/supabase/migrations/20240907140223_initialize.sql"
    click DBMigrations "https://github.com/paddlehq/paddle-nextjs-starter-kit/blob/main/supabase/seed.sql"
```
