Skip to main content

Referral System Architecture

Overview

The referral system allows users to invite friends to the platform. Each user gets a unique referral link based on their clerk_id. Referrals are tracked and marked as “completed” when the referred user connects their first account (via Plaid or SnapTrade).

Key Features

  • Simple Link-Based Referrals: No custom codes needed - uses clerk_id directly
  • Automatic Tracking: Tracks signups and account connections automatically
  • Completion Criteria: Referral is completed when referred user connects an account
  • Statistics Dashboard: Users can see total signups and completed referrals

Database Schema

Table: referrals

Tracks all referral relationships and their completion status. Note: The referrals table is the single source of truth. No additional columns needed in all_users table.

API Routes

1. GET /api/referrals/stats

Get referral statistics for the authenticated user. Authentication: Required (Clerk) Response:

2. POST /api/referrals/track-signup

Track a new referral signup. Should be called during user registration when a referral link was used. Authentication: None (called during signup process) Request Body:
Response:

3. POST /api/referrals/mark-completed

Mark a referral as completed when the referred user connects their first account. Authentication: None (called by system) Request Body:
Response:

4. GET /api/referrals/validate-referrer?ref=abc123xyz456

Validate that a referrer exists and get their clerk_id. Used when someone clicks a referral link. Authentication: None (public endpoint) Response:

How It Works

Each user gets a unique random 12-character string stored in all_users.referral_link. This is used in their referral URL instead of exposing their clerk_id.
Where abc123xyz456 is a unique 12-character random string stored in the user’s referral_link column. Fetches or generates the user’s unique referral link. Authentication: Required (Clerk) Response:

Integration Points

1. User Signup (Clerk Webhook)

When a new user signs up via a referral link:
  1. Store the ref parameter from the URL (in localStorage or session)
  2. After Clerk creates the user, call /api/referrals/track-signup with:
    • referred_clerk_id: New user’s clerk_id
    • referrer_clerk_id: Value from the ref parameter
Location to integrate: app/api/webhooks/clerk/route.ts (user.created event)

2. Account Connection

When a user connects their first account (Plaid or SnapTrade): Call /api/referrals/mark-completed with the user’s clerk_id. Locations to integrate:
  • app/api/connect-bank/route.ts (Plaid connection success)
  • app/api/snaptrade/process-connection/route.ts (SnapTrade connection success)
Example integration:

3. Settings Page (Frontend)

Display the user’s referral link and statistics. Location to create: app/settings/page.tsx or a new referrals section Example component structure:

Implementation Checklist

  • Create database migration (sql-queries/create-referrals-table.sql)
  • Create API routes:
    • /api/referrals/stats - Get user’s referral statistics
    • /api/referrals/track-signup - Track new referral signup
    • /api/referrals/mark-completed - Mark referral as completed
    • /api/referrals/validate-referrer - Validate referrer exists
  • Integrate with Clerk webhook (track signups)
  • Integrate with Plaid connection (mark completed)
  • Integrate with SnapTrade connection (mark completed)
  • Create frontend UI in Settings page
  • Add referral link sharing functionality
  • Add referral statistics display

Future Enhancements (TBD)

  • Reward system for completed referrals
  • Email notifications when referrals complete
  • Referral leaderboard
  • Custom referral campaigns
  • Referral analytics (conversion rates, etc.)