# firecrawl/firegeo architecture

> Architecture diagram of the GitHub repository firecrawl/firegeo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/firecrawl/firegeo
- Repository: https://github.com/firecrawl/firegeo

## Overview

##### Project type and purpose
- This is a full-stack SaaS starter built on Next.js 15 + TypeScript.
- Key features: authentication (Better Auth), subscription billing (Autumn + Stripe), AI-driven chat, brand-monitoring with web scraping, PostgreSQL database, email notifications, and serverless API routes.

##### File structure
- app/
– Next.js App Router for both UI pages (app/login, app/chat, app/brand-monitor, etc.) and serverless API endpoints under app/api.
– API subdivisions:
· auth (Better Auth endpoints)
· autumn (billing endpoints)
· brand-monitor (analysis, scrape, web-search)
· chat (conversations, feedback)
· user, credits
- components/
– React UI components organized by domain (ui, autumn, brand-monitor, general).
- lib/
– Shared server-side logic: DB client (Drizzle), authentication client, API wrappers, AI-provider utilities, scraping utils, SSE parser, email.
- config/, scripts/, migrations/, drizzle.config.ts
– Configuration files, setup and migration scripts for DB, billing, auth.
- public/
– Static assets.
- next.config.ts, middleware.ts, etc.
– Framework configuration and global middleware for auth/session.

##### README for additional insights
- Tech Stack: Next.js, React, Tailwind CSS, Drizzle ORM, Better Auth, Autumn, Firecrawl, multiple AI providers, Resend email.
- Provides a high-level component breakdown under “Project Structure.”
- Describes environment variables required for each external integration.

a. Main Components
1. Client (Browser) – React pages in app/.
2. Next.js Server/API Layer – app/api routes & middleware.
3. Business Logic Layer – lib/ modules handling auth, DB, AI, billing, scraping, email.
4. Database – PostgreSQL via Drizzle ORM.
5. Authentication Service – Better Auth tables & endpoints.
6. Billing Service – Autumn + Stripe webhooks & API.
7. External AI Providers – OpenAI, Anthropic, Google Gemini, Perplexity.
8. Web Scraper – Firecrawl.
9. Email Service – Resend.
10. Drizzle Studio / Migration Scripts – migrations folder & drizzle.config.ts.

b. Relationships and Interactions
- Client → Next.js Router & Server Components for pages (login, dashboard, chat).
- Client → app/api/* routes over HTTPS for data operations.
- API Layer → lib/auth.ts for session validation.
- API Layer → lib/db.ts → PostgreSQL for CRUD.
- API Layer → lib/api-wrapper or lib/anthropic-web-search → AI providers.
- API Layer → lib/scrape-utils → Firecrawl.
- API Layer → lib/email → Resend for notifications.
- API Layer → better-auth migrations & endpoints for user management.
- API Layer → Autumn endpoints for billing actions → Stripe.
- Drizzle migration scripts → PostgreSQL schema versioning.

c. Architectural Patterns / Design Principles
- Monolithic “modular” architecture leveraging Next.js App Router with clear separation of UI (React components) and API (serverless functions).
- Layered architecture: Presentation (React), API layer (Next.js routes), Service layer (lib/), Data Access (Drizzle ORM), External integrations.
- Incremental adoption of feature domains: auth, billing, chat, brand-monitor.
- Infrastructure as code / migrations for database & auth.

d. Key Technologies & Frameworks
- Frontend: Next.js 15, React 19, Tailwind CSS, shadcn/ui.
- Backend/API: Next.js serverless routes, TypeScript.
- ORM: Drizzle ORM (drizzle.config.ts, lib/db/schema.ts).
- Auth: Better Auth + custom middleware (middleware.ts).
- Billing: Autumn SDK + Stripe.
- AI: OpenAI, Anthropic, Google Gemini, Perplexity via lib/providers.
- Scraping: Firecrawl.
- Email: Resend.
- Database: PostgreSQL.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Browser Client
    subgraph "Browser (Client)"
        UI["Next.js Pages (app/)"]:::frontend
    end

    %% Next.js API Layer
    subgraph "Next.js API Layer"
        MW["middleware.ts"]:::backend

        subgraph "Auth Domain"
            AuthAPI["Auth Routes (app/api/auth)"]:::backend
        end

        subgraph "Billing Domain"
            BillingAPI["Billing Routes (app/api/autumn)"]:::backend
        end

        subgraph "Brand-Monitor Domain"
            BMAPI["Brand-Monitor Routes (app/api/brand-monitor)"]:::backend
        end

        subgraph "Chat Domain"
            ChatAPI["Chat Routes (app/api/chat)"]:::backend
        end

        subgraph "User & Credits Domain"
            CreditsAPI["Credits Routes (app/api/credits) & User Routes (app/api/user)"]:::backend
        end
    end

    %% Business Logic Layer
    subgraph "Business Logic (lib/)"
        AuthLib["lib/auth.ts"]:::backend
        DBLib["lib/db.ts"]:::backend
        APILib["lib/api-wrapper.ts"]:::backend
        ML["lib/anthropic-web-search.ts"]:::backend
        Scrape["lib/scrape-utils.ts"]:::backend
        Email["lib/email.ts"]:::backend
    end

    %% Database & Migrations
    subgraph "Database & Migrations"
        DB["PostgreSQL (Drizzle ORM)"]:::database
        Migrations["migrations/"]:::backend
        BA_Migrations["better-auth_migrations/"]:::backend
    end

    %% External Services
    subgraph "External Services"
        BetterAuth["Better Auth Service"]:::external
        AutumnSvc["Autumn Billing Service"]:::external
        Stripe["Stripe"]:::external
        AIExt["AI Providers (OpenAI, Anthropic, Gemini, Perplexity)"]:::external
        Firecrawl["Firecrawl Scraper"]:::external
        Resend["Resend Email Service"]:::external
    end

    %% Connections
    UI --> AuthAPI
    UI --> BillingAPI
    UI --> BMAPI
    UI --> ChatAPI
    UI --> CreditsAPI

    MW --> AuthAPI
    MW --> BillingAPI
    MW --> BMAPI
    MW --> ChatAPI
    MW --> CreditsAPI

    AuthAPI --> AuthLib
    AuthAPI --> DBLib

    BillingAPI --> APILib
    BillingAPI --> AutumnSvc
    AutumnSvc --> Stripe
    BillingAPI --> DBLib

    BMAPI --> Scrape
    BMAPI --> ML
    Scrape --> Firecrawl
    BMAPI --> DBLib

    ChatAPI --> APILib
    ChatAPI --> DBLib

    CreditsAPI --> DBLib

    AuthLib --> BetterAuth
    AuthLib --> DBLib

    APILib --> AIExt
    Email --> Resend

    DBLib --> DB
    Migrations --> DB
    BA_Migrations --> BetterAuth

    %% Click Events
    click UI "https://github.com/firecrawl/firegeo/tree/main/app/"
    click MW "https://github.com/firecrawl/firegeo/blob/main/middleware.ts"
    click AuthAPI "https://github.com/firecrawl/firegeo/blob/main/app/api/auth/[...better-auth]/route.ts"
    click BillingAPI "https://github.com/firecrawl/firegeo/blob/main/app/api/autumn/[...all]/route.ts"
    click BMAPI "https://github.com/firecrawl/firegeo/blob/main/app/api/brand-monitor/analyses/route.ts"
    click ChatAPI "https://github.com/firecrawl/firegeo/blob/main/app/api/chat/[conversationId]/route.ts"
    click CreditsAPI "https://github.com/firecrawl/firegeo/blob/main/app/api/credits/route.ts"
    click AuthLib "https://github.com/firecrawl/firegeo/blob/main/lib/auth.ts"
    click DBLib "https://github.com/firecrawl/firegeo/blob/main/lib/db.ts"
    click APILib "https://github.com/firecrawl/firegeo/blob/main/lib/api-wrapper.ts"
    click ML "https://github.com/firecrawl/firegeo/blob/main/lib/anthropic-web-search.ts"
    click Scrape "https://github.com/firecrawl/firegeo/blob/main/lib/scrape-utils.ts"
    click Email "https://github.com/firecrawl/firegeo/blob/main/lib/email.ts"
    click Migrations "https://github.com/firecrawl/firegeo/blob/main/migrations/001_create_app_schema.sql"
    click BA_Migrations "https://github.com/firecrawl/firegeo/blob/main/better-auth_migrations/initial-schema.sql"
    click DB "https://github.com/firecrawl/firegeo/blob/main/drizzle.config.ts"
    click BetterAuth "https://github.com/firecrawl/firegeo/blob/main/better-auth.config.ts"
    click AutumnSvc "https://github.com/firecrawl/firegeo/blob/main/config/autumn-products.ts"

    %% Styles
    classDef frontend fill:#CDE8FF,stroke:#0366d6,color:#0366d6,stroke-width:1px;
    classDef backend fill:#DFFFD6,stroke:#28a745,color:#155724,stroke-width:1px;
    classDef database fill:#FFE5B4,stroke:#d2691e,color:#8b4513,stroke-width:1px;
    classDef external fill:#F0F0F0,stroke:#6c757d,color:#343a40,stroke-width:1px;
```
