# orion-script/wix-hubspot-sync architecture

> Architecture diagram of the GitHub repository orion-script/wix-hubspot-sync, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/orion-script/wix-hubspot-sync
- Repository: https://github.com/orion-script/wix-hubspot-sync

## Overview

#### Purpose and runtime
A Next.js full-stack integration service synchronizes Wix and HubSpot contacts in both directions, creates HubSpot leads from Wix forms, and provides a browser dashboard for field mappings and HubSpot connection state. It runs on Node.js 18+ with a local SQLite database.

Primary UI entry points:
- `src/app/page.tsx`
- `src/app/layout.tsx`

#### HTTP integration boundary
Next.js route handlers are the public boundary for OAuth, dashboard data, and inbound provider events:
- HubSpot OAuth initiation/callback/status: `src/app/api/auth/hubspot/route.ts`, `src/app/api/auth/hubspot/callback/route.ts`, `src/app/api/auth/hubspot/status/route.ts`
- Mapping management: `src/app/api/mappings/route.ts`
- HubSpot property discovery for the mapping UI: `src/app/api/hubspot/properties/route.ts`

The browser configures mappings against HubSpot’s available contact properties; webhook paths receive provider-originated changes.

#### OAuth and credential protection
HubSpot authentication uses OAuth authorization and callback routes, then persists access/refresh tokens for later API calls. Token storage is encrypted with AES-256-GCM using `ENCRYPTION_KEY`:
- `src/app/api/auth/hubspot/callback/route.ts`
- `src/lib/crypto.ts`
- `src/lib/db.ts`

This separates provider credentials and persistent integration state from request handlers and UI code.

#### Persistence and mapping model
SQLite is the durable coordination layer, automatically created locally. It holds integration state such as encrypted HubSpot credentials, configurable Wix-to-HubSpot field mappings, and likely sync/idempotency state:
- `src/lib/db.ts`
- `src/app/api/mappings/route.ts`
- `src/lib/sync.ts`

Mappings support source-to-target property selection plus transformations such as trimming and lowercasing. Mapping retrieval/update is exposed to the dashboard rather than embedded in webhook logic.

#### Bi-directional contact synchronization
Provider-specific API adapters encapsulate Wix and HubSpot calls, while the sync layer coordinates mapping, writes, conflict handling, and loop prevention:
- `src/lib/sync.ts`
- `src/lib/hubspot.ts`
- `src/lib/wix.ts`

Inbound Wix contact events arrive at `src/app/api/webhooks/wix/contact/route.ts`; HubSpot contact events arrive at `src/app/api/webhooks/hubspot/contact/route.ts`. The sync engine applies deterministic “last updated wins” resolution before propagating a change to the opposite provider.

#### Webhook security and idempotency
Webhook handlers are a trust boundary: Wix events are authenticated with an HMAC secret, and core cryptographic utilities provide signature/hash behavior:
- `src/app/api/webhooks/wix/contact/route.ts`
- `src/app/api/webhooks/hubspot/contact/route.ts`
- `src/lib/crypto.ts`

SHA-256 state hashing and a 15-second deduplication window prevent repeated delivery and cross-system ping-pong after a propagated update. The synchronization state is persisted through the database boundary.

#### Form-to-lead ingestion
Wix form webhooks use the same integration pipeline to create or update HubSpot contact/lead data, including UTM attribution and referral context:
- `src/app/api/webhooks/wix/form/route.ts`
- `src/lib/sync.ts`
- `src/lib/hubspot.ts`

External dependencies are Wix APIs/webhooks and HubSpot OAuth, CRM APIs, properties APIs, and webhooks. Local development requires a publicly reachable tunnel for provider webhook delivery.

Main entry points: [`src/lib/sync.ts`](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/lib/sync.ts) (Sync engine), [`src/app/page.tsx`](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/page.tsx) (Mapping dashboard), [`src/app/api/webhooks/wix/contact/route.ts`](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/webhooks/wix/contact/route.ts) (Wix contact webhook), [`src/lib/hubspot.ts`](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/lib/hubspot.ts) (HubSpot adapter)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Dashboard"]
  node_dashboard["Mapping dashboard<br/>Next.js UI<br/>[page.tsx]"]
  node_layout["App layout<br/>Next.js UI<br/>[layout.tsx]"]
end

subgraph group_api["Next.js API boundary"]
  node_oauth["HubSpot OAuth routes<br/>route handlers<br/>[route.ts]"]
  node_oauth_callback["OAuth callback<br/>route handler<br/>[route.ts]"]
  node_mappings_api["Mappings API<br/>route handler<br/>[route.ts]"]
  node_properties_api["HubSpot properties API<br/>route handler<br/>[route.ts]"]
  node_wix_contact_hook["Wix contact webhook<br/>route handler<br/>[route.ts]"]
  node_hubspot_contact_hook["HubSpot contact webhook<br/>route handler<br/>[route.ts]"]
  node_wix_form_hook["Wix form webhook<br/>route handler<br/>[route.ts]"]
end

subgraph group_core["Integration core"]
  node_sync{{"Sync engine<br/>coordination layer<br/>[sync.ts]"}}
  node_hubspot_adapter["HubSpot adapter<br/>provider adapter<br/>[hubspot.ts]"]
  node_wix_adapter["Wix adapter<br/>provider adapter<br/>[wix.ts]"]
  node_crypto["Crypto utilities<br/>security utility<br/>[crypto.ts]"]
  node_sqlite[("SQLite integration state<br/>durable store<br/>[db.ts]")]
end

subgraph group_external["External systems"]
  node_wix(("Wix<br/>provider APIs and webhooks"))
  node_hubspot(("HubSpot<br/>OAuth, CRM, properties, webhooks"))
end

node_layout --> node_dashboard
node_dashboard -->|"connect/status"| node_oauth
node_dashboard -->|"manage mappings"| node_mappings_api
node_dashboard -->|"discover properties"| node_properties_api
node_oauth -->|"authorize"| node_hubspot
node_hubspot -->|"OAuth redirect"| node_oauth_callback
node_oauth_callback -->|"encrypt tokens"| node_crypto
node_crypto -->|"protected credentials"| node_sqlite
node_mappings_api -->|"read/write mappings"| node_sqlite
node_properties_api -->|"property lookup"| node_hubspot_adapter
node_hubspot_adapter -->|"CRM and properties API"| node_hubspot
node_wix -->|"contact event"| node_wix_contact_hook
node_hubspot -->|"contact event"| node_hubspot_contact_hook
node_wix -->|"form event"| node_wix_form_hook
node_wix_contact_hook -->|"verify HMAC"| node_crypto
node_wix_contact_hook -->|"Wix change"| node_sync
node_hubspot_contact_hook -->|"HubSpot change"| node_sync
node_wix_form_hook -->|"form lead"| node_sync
node_sync -->|"mappings and sync state"| node_sqlite
node_sync -->|"propagate to Wix"| node_wix_adapter
node_sync -->|"propagate or create lead"| node_hubspot_adapter
node_wix_adapter -->|"Wix API"| node_wix

click node_dashboard "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/page.tsx"
click node_layout "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/layout.tsx"
click node_oauth "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/auth/hubspot/route.ts"
click node_oauth_callback "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/auth/hubspot/callback/route.ts"
click node_mappings_api "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/mappings/route.ts"
click node_properties_api "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/hubspot/properties/route.ts"
click node_wix_contact_hook "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/webhooks/wix/contact/route.ts"
click node_hubspot_contact_hook "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/webhooks/hubspot/contact/route.ts"
click node_wix_form_hook "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/webhooks/wix/form/route.ts"
click node_sync "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/lib/sync.ts"
click node_hubspot_adapter "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/lib/hubspot.ts"
click node_wix_adapter "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/lib/wix.ts"
click node_crypto "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/lib/crypto.ts"
click node_sqlite "https://github.com/orion-script/wix-hubspot-sync/blob/main/src/lib/db.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_dashboard,node_layout toneBlue
class node_oauth,node_oauth_callback,node_mappings_api,node_properties_api,node_wix_contact_hook,node_hubspot_contact_hook,node_wix_form_hook toneAmber
class node_sync,node_hubspot_adapter,node_wix_adapter,node_crypto,node_sqlite toneMint
class node_wix,node_hubspot toneRose
```

## Components

### Dashboard

- [Mapping dashboard](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/page.tsx): Next.js UI · Browser UI for mappings and connection state. · `src/app/page.tsx`
- [App layout](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/layout.tsx): Next.js UI · `src/app/layout.tsx`

### Next.js API boundary

- [HubSpot OAuth routes](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/auth/hubspot/route.ts): route handlers · Starts OAuth and reports connection status. · `src/app/api/auth/hubspot/route.ts`
- [OAuth callback](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/auth/hubspot/callback/route.ts): route handler · Exchanges and persists provider tokens. · `src/app/api/auth/hubspot/callback/route.ts`
- [Mappings API](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/mappings/route.ts): route handler · `src/app/api/mappings/route.ts`
- [HubSpot properties API](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/hubspot/properties/route.ts): route handler · Supplies target contact properties to the UI. · `src/app/api/hubspot/properties/route.ts`
- [Wix contact webhook](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/webhooks/wix/contact/route.ts): route handler · Authenticated inbound contact changes. · `src/app/api/webhooks/wix/contact/route.ts`
- [HubSpot contact webhook](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/webhooks/hubspot/contact/route.ts): route handler · Inbound HubSpot contact changes. · `src/app/api/webhooks/hubspot/contact/route.ts`
- [Wix form webhook](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/app/api/webhooks/wix/form/route.ts): route handler · Ingests form leads with attribution context. · `src/app/api/webhooks/wix/form/route.ts`

### Integration core

- [Sync engine](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/lib/sync.ts): coordination layer · Applies mappings, last-updated-wins, deduplication, and loop prevention. · `src/lib/sync.ts`
- [HubSpot adapter](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/lib/hubspot.ts): provider adapter · `src/lib/hubspot.ts`
- [Wix adapter](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/lib/wix.ts): provider adapter · `src/lib/wix.ts`
- [Crypto utilities](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/lib/crypto.ts): security utility · AES-256-GCM token encryption plus signature and state hashing. · `src/lib/crypto.ts`
- [SQLite integration state](https://github.com/orion-script/wix-hubspot-sync/blob/main/src/lib/db.ts): durable store · Stores encrypted credentials, mappings, and sync/idempotency state. · `src/lib/db.ts`

### External systems

- **Wix**: provider APIs and webhooks
- **HubSpot**: OAuth, CRM, properties, webhooks

## Connections

- App layout → Mapping dashboard
- Mapping dashboard → HubSpot OAuth routes: connect/status
- Mapping dashboard → Mappings API: manage mappings
- Mapping dashboard → HubSpot properties API: discover properties
- HubSpot OAuth routes → HubSpot: authorize
- HubSpot → OAuth callback: OAuth redirect
- OAuth callback → Crypto utilities: encrypt tokens
- Crypto utilities → SQLite integration state: protected credentials
- Mappings API → SQLite integration state: read/write mappings
- HubSpot properties API → HubSpot adapter: property lookup
- HubSpot adapter → HubSpot: CRM and properties API
- Wix → Wix contact webhook: contact event
- HubSpot → HubSpot contact webhook: contact event
- Wix → Wix form webhook: form event
- Wix contact webhook → Crypto utilities: verify HMAC
- Wix contact webhook → Sync engine: Wix change
- HubSpot contact webhook → Sync engine: HubSpot change
- Wix form webhook → Sync engine: form lead
- Sync engine → SQLite integration state: mappings and sync state
- Sync engine → Wix adapter: propagate to Wix
- Sync engine → HubSpot adapter: propagate or create lead
- Wix adapter → Wix: Wix API
