An intelligent, real-time mortgage rate monitoring and analysis system built with Motia, OpenAI, Couchbase Vector Search, and Resend. This system automatically scrapes mortgage rates, stores them in a vector database, and provides AI-powered insights via email.
- 🤖 AI-Powered Analysis: Uses OpenAI GPT-4 to analyze mortgage rate trends and provide insights
- 🔍 Vector Search: Leverages Couchbase vector search with 1536-dimensional embeddings for semantic similarity
- 📧 Email Alerts: Beautiful HTML emails with AI analysis delivered via Resend
- ⏰ Automated Monitoring: Cron-based scheduled rate scraping from multiple sources
- 🌐 Web Scraping: Fetches live mortgage rates from major lenders
- 📊 CSV Logging: Tracks all analyses with timestamps and context
- 🔄 Real-time Processing: Immediate analysis of newly submitted rates
-
Manual Webhook Flow
POST /mortgage_rate_alert → Process Data → Generate Embeddings → Store in Couchbase → Vector Search → AI Analysis → Email + Log -
Automated Monitoring Flow
Cron Job (Daily) → Scrape Websites → Detect Rate Changes → Send Alert Emails
- Framework: Motia - Backend Framework
- AI: OpenAI (text-embedding-ada-002, GPT-4)
- Database: Couchbase Capella (Vector Search + N1QL)
- Email: Resend API
- Language: TypeScript (100% type-safe)
- Node.js 18+
- Couchbase Capella account
- OpenAI API key
- Resend API key
# Clone the repository
git clone https://github.com/motiadev/motia-examples
cd mortgage-rate-alert
# Install dependencies
npm install
# Generate types
npm run generate-types
# Start development server
npm run devServer will be available at http://localhost:3000
Create a .env file in the root directory:
# OpenAI Configuration
OPENAI_API_KEY=sk-...
# Couchbase Capella Configuration
COUCHBASE_CONNECTION_STRING=couchbases://cb.xxxxx.cloud.couchbase.com
COUCHBASE_DATA_API_URL=https://xxxxx.data.cloud.couchbase.com
COUCHBASE_USERNAME=your-username
COUCHBASE_PASSWORD=your-password
COUCHBASE_BUCKET=morgage-data
COUCHBASE_SCOPE=_default
COUCHBASE_COLLECTION=_default
COUCHBASE_INDEX=mortgage-vector-index
# Resend Email Configuration
RESEND_API_KEY=re_...
FROM_EMAIL=onboarding@resend.dev
ALERT_RECIPIENTS=email1@example.com,email2@example.com-
Create a Vector Search Index in Couchbase Cloud:
- Index Name:
mortgage-vector-index - Bucket:
morgage-data - Scope:
_default - Collections:
_default - Add Type Mapping:
- Field:
embedding - Type: vector
- Dimensions: 1536
- Similarity: dot_product
- Field:
- Index Name:
-
Grant User Permissions:
- Data Writer
- Data Reader
- Search Admin
The easiest way to interact with the system is through the Motia Plugin UI:
- Start the development server:
npm run dev - Open
http://localhost:3000in your browser - Click on "Mortgage Alerts 🏠" in the plugins sidebar
The plugin provides:
- Manual Rate Submission: Test the AI analysis with custom mortgage data
- Automated Rate Fetching: Trigger cron jobs manually to scrape live rates
- Recent Analyses: View the last 10 AI-powered analyses
- Real-time Status: Watch processing pipeline in action
- Quick Testing: No need for curl commands or Postman
You can also use the API directly:
curl -X POST http://localhost:3000/api/mortgage_rate_alert \
-H "Content-Type: application/json" \
-d '{
"content": "Wells Fargo: 30-year fixed at 6.125% APR, 15-year at 5.375% APR"
}'The system will:
- ✅ Chunk the text
- ✅ Generate embeddings (OpenAI)
- ✅ Store in Couchbase with vectors
- ✅ Perform vector search for similar rates
- ✅ AI analysis with GPT-4
- ✅ Send email with insights
- ✅ Log to CSV
The cron job (fetchRates.step.ts) automatically:
- Scrapes mortgage websites daily
- Compares with previous rates
- Detects significant changes (>0.1%)
- Sends alert emails
- Logs: Check
logs/mortgage-rate-alert-log.csv - Workbench: Open
http://localhost:3000for visual flow monitoring
mortgage-rate-alert/
├── plugins/
│ └── plugin-mortgage-alert/
│ ├── index.tsx # React UI for testing & monitoring
│ └── package.json # Plugin configuration
│
├── steps/mortgageRateAlert/
│ ├── webhook.step.ts # API endpoint for rate submission
│ ├── triggerFetch.step.ts # API to manually trigger rate fetch
│ ├── getRecent.step.ts # API to fetch recent analyses
│ ├── processData.step.ts # Text chunking
│ ├── storeEmbeddings.step.ts # OpenAI embeddings + Couchbase storage
│ ├── analyzeData.step.ts # Vector search + AI analysis
│ ├── sendAnalysis.step.ts # Email AI analysis results
│ ├── logResults.step.ts # CSV logging
│ ├── fetchRates.step.ts # Cron job for rate scraping
│ ├── manualFetch.step.ts # Event handler for manual fetch
│ └── sendAlerts.step.ts # Email rate change alerts
│
├── src/
│ ├── services/
│ │ ├── openai/ # OpenAI API integration
│ │ ├── couchbase/ # Couchbase vector DB operations
│ │ ├── resend/ # Email service
│ │ └── scraper/ # Web scraping for mortgage rates
│ └── utils/
│ ├── text-chunker.ts # Text splitting utility
│ └── email-templates.ts # HTML email generation
│
├── logs/ # CSV analysis logs
├── package.json
└── README.md
npm run test:webhookThe cron job runs automatically based on the schedule defined in fetchRates.step.ts. To test manually, modify the cron expression to * * * * * (every minute).
Vector search may take seconds to minutes to index new documents. The system uses state-backed immediate access to bypass this lag for newly submitted rates.
The system disables SSL verification for Couchbase in development:
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'Access the Motia Workbench at http://localhost:3000 to:
- View workflow execution traces
- Monitor step durations
- Debug failures
- Visualize data flow
MIT
- Motia - Workflow orchestration framework
- OpenAI - AI embeddings and chat completions
- Couchbase - Vector database
- Resend - Email delivery
Built with ❤️ using Motia, TypeScript, and AI

