Overview
The backend is an Express server built with TypeScript that handles OAuth authentication with HubSpot and proxies requests to the HubSpot Forms API. It runs on port 3001 by default.Tech Stack
- Node.js - JavaScript runtime
- Express 4.19 - Web framework
- TypeScript 5.5 - Type-safe JavaScript
- dotenv 16.4 - Environment variable management
- cors 2.8 - Cross-Origin Resource Sharing
- tsx - TypeScript execution for development
Server Setup
File: server/src/index.tsOAuth 2.0 Implementation
File: server/src/oauth.ts Handles the OAuth 2.0 authorization code flow with HubSpot.Token Storage
OAuth Routes
1. GET /oauth/hubspot/install
Initiates the OAuth flow by redirecting to HubSpot.- Generate random state token (CSRF protection)
- Store state in memory
- Build HubSpot authorization URL
- Redirect user to HubSpot login
2. GET /oauth/hubspot/callback
Handles the callback from HubSpot after user authorization.- Receive authorization code and state from HubSpot
- Validate state (CSRF protection)
- Exchange code for access token and refresh token
- Store tokens in memory (keyed by portal ID)
- Redirect user back to frontend
3. GET /oauth/hubspot/status
Checks if the user has an active OAuth session.4. POST /oauth/hubspot/logout
Clears OAuth session.HubSpot Forms API Proxy
File: server/src/forms.ts Proxies requests to HubSpot’s Forms API with authentication.Type Definitions
Helper Functions
Get Access Token
Normalize Field Options
Normalize Field Schema
Normalize HubSpot Form
API Routes
1. GET /api/forms
Fetches list of forms from HubSpot.2. GET /api/forms/:formId
Fetches detailed schema for a specific form.Environment Variables
File:server/.env
Required Scopes
forms- Read and write access to forms
Environment Helper
CORS Configuration
The server allows requests from:- Localhost -
http://localhost:5173(default Vite port) - Cloudflare Tunnels - Any domain ending in
.trycloudflare.com - No origin - Allows same-origin requests
Error Handling
All endpoints return consistent error responses:200- Success400- Bad Request (missing parameters)401- Unauthorized (not connected or token expired)500- Server Error (HubSpot API failure or internal error)
