How orion-script/wix-hubspot-sync works
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: sync.ts Sync engine · page.tsx Mapping dashboard · route.ts Wix contact webhook · hubspot.ts HubSpot adapter
Components 16
Dashboard
- Mapping dashboard · Browser UI for mappings and connection state.
src/app/page.tsx - App layout
src/app/layout.tsx
Next.js API boundary
- HubSpot OAuth routes · Starts OAuth and reports connection status.
src/app/api/auth/hubspot/route.ts - OAuth callback · Exchanges and persists provider tokens.
src/app/api/auth/hubspot/callback/route.ts - Mappings API
src/app/api/mappings/route.ts - HubSpot properties API · Supplies target contact properties to the UI.
src/app/api/hubspot/properties/route.ts - Wix contact webhook · Authenticated inbound contact changes.
src/app/api/webhooks/wix/contact/route.ts - HubSpot contact webhook · Inbound HubSpot contact changes.
src/app/api/webhooks/hubspot/contact/route.ts - Wix form webhook · Ingests form leads with attribution context.
src/app/api/webhooks/wix/form/route.ts
Integration core
- Sync engine · Applies mappings, last-updated-wins, deduplication, and loop prevention.
src/lib/sync.ts - HubSpot adapter
src/lib/hubspot.ts - Wix adapter
src/lib/wix.ts - Crypto utilities · AES-256-GCM token encryption plus signature and state hashing.
src/lib/crypto.ts - SQLite integration state · Stores encrypted credentials, mappings, and sync/idempotency state.
src/lib/db.ts
External systems
- Wix
- HubSpot
Connections 22
- 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