Unclassified

API Routes Consolidation Mapping Report

Date: 2025-01-20
Source: _reference/app/app/api/ (66 routes from original app)
Target: packages/api/src/ (Hono backend) + apps/workspace/ (Next.js routes)
Strategy: Prioritize merging into Hono backend for consistency. Preserve existing auth.ts, entities, metrics, and onboarding endpoints.


Legend

  • MERGE → Ported/rewritten into packages/api/src/ as Hono routes using D1
  • KEEP → Retained as Next.js API route in apps/workspace/ (complex AI streaming, Supabase-specific, or edge-specific)
  • DISCARD → Redundant with existing backend or no longer needed
  • DEFER → Complex dependency chain; keep in reference for future migration

1. Billing & Payments (10 routes)

RouteDecisionAction TakenNotes
/api/billing/plansMERGEAdded to packages/api/src/billing.tsReturns subscription tiers from DB or config
/api/billing/subscriptionMERGEAdded to packages/api/src/billing.tsFetch tenant subscription status
/api/billing/entitlementsMERGEAdded to packages/api/src/billing.tsCheck feature flags per tenant
/api/billing/invoicesMERGEAdded to packages/api/src/billing.tsList tenant invoices from DB
/api/billing/checkoutMERGEAdded to packages/api/src/billing.tsStripe checkout session creation
/api/billing/subscribeMERGEAdded to packages/api/src/billing.tsCreate subscription
/api/billing/cancelMERGEAdded to packages/api/src/billing.tsCancel subscription
/api/billing/change-planMERGEAdded to packages/api/src/billing.tsUpgrade/downgrade plan
/api/billing/webhook/[provider]MERGEConsolidated into packages/api/src/webhooks.tsStripe/Razorpay/Cashfree/PhonePe webhooks
/api/stripe/webhookDISCARD—Redundant with /webhooks/stripe already in Hono backend

New file: packages/api/src/billing.ts — Sub-router mounted in index.ts
New file: packages/api/src/webhooks.ts — Consolidated webhook handlers (expanded from inline routes in index.ts)


2. Health & Diagnostics (5 routes)

RouteDecisionAction TakenNotes
/api/healthDISCARD—Already served by /api and / in Hono
/api/livenessDISCARD—Redundant; D1 connectivity check added to /api/health
/api/pingDISCARD—Redundant
/api/readinessDISCARD—Redundant; D1 readiness check added to /api/health
/api/env/healthDISCARD—Supabase-specific env checks; not needed in D1/Hono stack

Enhanced: GET /api/health in index.ts now returns D1 status, AI binding status, and env check.


3. Auth & Session (2 routes)

RouteDecisionAction TakenNotes
/api/sessionDISCARD—Supabase auth session; replaced by GET /api/auth/me in Hono auth.ts
/api/workspace/bootstrapMERGEAdded to packages/api/src/index.ts as POST /api/workspace/bootstrapIdempotent org/workspace creation; adapted to D1 schema

4. Search (1 route)

RouteDecisionAction TakenNotes
/api/searchMERGEAdded to packages/api/src/index.ts as GET /api/searchFull-text search across entities using entities_fts

5. Metrics & Insights (4 routes)

RouteDecisionAction TakenNotes
/api/metrics/kpisDISCARD—Already exists as GET /api/metrics/kpis in Hono
/api/insights/patternsMERGEAdded to packages/api/src/insights.tsPattern detection over entity history
/api/spend/summaryMERGEAdded to packages/api/src/spend.tsAggregated spend summary
/api/spend/trendMERGEAdded to packages/api/src/spend.tsSpend trend over time

New file: packages/api/src/insights.ts
New file: packages/api/src/spend.ts


6. Customer Success & Goals (2 routes)

RouteDecisionAction TakenNotes
/api/cs/health-scoreMERGEAdded to packages/api/src/cs.tsHealth score calculation per entity
/api/goals/progressMERGEAdded to packages/api/src/goals.tsGoal progress tracking

New file: packages/api/src/cs.ts
New file: packages/api/src/goals.ts


7. Support & Sync (3 routes)

RouteDecisionAction TakenNotes
/api/support/contactMERGEAdded to packages/api/src/index.tsSimple support ticket creation in DB
/api/sync/scheduleMERGEAdded to packages/api/src/sync.tsSync schedule CRUD
/api/data-syncMERGEAdded to packages/api/src/sync.tsManual sync trigger

New file: packages/api/src/sync.ts


8. Webhooks — CRM & Productivity (7 routes)

RouteDecisionAction TakenNotes
/api/webhooks/hubspotMERGEEnhanced packages/api/src/webhooks.tsMore detailed contact/deal/company sync
/api/webhooks/asanaMERGEAdded to packages/api/src/webhooks.tsAsana task → entity sync
/api/webhooks/slackKEEPPorted to apps/workspace/api/webhooks/slack/route.tsComplex interactive signatures + triage AI; needs Node crypto for Ed25519
/api/webhooks/discordKEEPPorted to apps/workspace/api/webhooks/discord/route.tsEd25519 verification; Cloudflare Workers can do this but the library stack is different
/api/webhooks/[provider]KEEPPorted to apps/workspace/api/webhooks/[provider]/route.tsGeneric dispatcher for 10+ providers; too large to inline
/api/webhooks/ai-relayKEEPPorted to apps/workspace/api/webhooks/ai-relay/route.tsHMAC-SHA256 custom signature; specific to relay architecture
/api/webhooks/brainstormKEEPPorted to apps/workspace/api/webhooks/brainstorm/route.tsBrainstorm-specific webhook
/api/webhooks/healthMERGEAdded to packages/api/src/webhooks.tsSimple DB connectivity check for webhooks
/api/webhookKEEPPorted to apps/workspace/api/webhook/route.tsGeneric inbound webhook with API secret
/api/webhook-scheduler/triggerKEEPPorted to apps/workspace/api/webhook-scheduler/trigger/route.tsComplex scheduler trigger with Slack/Discord notifications
/api/hubspot/syncDISCARD—Redundant with /api/webhooks/hubspot + manual sync endpoint

Expanded: packages/api/src/webhooks.ts — Extracted from inline routes in index.ts into dedicated module. Added Asana, enhanced HubSpot.


9. AI & Chat (3 routes)

RouteDecisionAction TakenNotes
/api/ai/chatKEEPPorted to apps/workspace/api/ai/chat/route.tsUses Vercel AI SDK (ai package) with streamText + convertToModelMessages; incompatible with Cloudflare Workers Hono without bundling
/api/chat/streamKEEPPorted to apps/workspace/api/chat/stream/route.tsUses @ai-sdk/openai + Vercel streaming; needs Next.js edge runtime
/api/brainstorm/analyzeKEEPPorted to apps/workspace/api/brainstorm/analyze/route.tsComplex AI workflow with Zod schema + Vercel AI SDK
/api/brainstorm/daily-insightsKEEPPorted to apps/workspace/api/brainstorm/daily-insights/route.tsVercel AI SDK + cron trigger
/api/brainstorm/executeKEEPPorted to apps/workspace/api/brainstorm/execute/route.tsComplex multi-step AI execution

Note: These use the Vercel AI SDK (ai, @ai-sdk/openai) which has Node.js-specific streaming. Keeping in Next.js allows continued use. Future: port to Cloudflare Workers AI binding (c.env.AI) when Vercel AI SDK supports it fully.


10. Cron Jobs (6 routes)

RouteDecisionAction TakenNotes
/api/cron/daily-insightsKEEPPorted to apps/workspace/api/cron/daily-insights/route.tsVercel Cron; triggers /api/brainstorm/daily-insights
/api/cron/hourly-insightsKEEPPorted to apps/workspace/api/cron/hourly-insights/route.tsComplex multi-provider AI insights
/api/cron/integrity-checkKEEPPorted to apps/workspace/api/cron/integrity-check/route.tsNeon DB integrity checks; D1 doesn't need this
/api/cron/outboxKEEPPorted to apps/workspace/api/cron/outbox/route.tsOutbox pattern processing
/api/cron/spend-insightsKEEPPorted to apps/workspace/api/cron/spend-insights/route.tsSpend insights cron
/api/cron/sync-schedulerKEEPPorted to apps/workspace/api/cron/sync-scheduler/route.tsSync scheduler cron

Note: These are Vercel Cron format. In Cloudflare stack, these should become Cloudflare Cron Triggers on the Worker. For now, kept as Next.js routes. Future: migrate to wrangler.toml cron triggers.


11. Loader System (9 routes)

RouteDecisionAction TakenNotes
/api/loader/[source]KEEPPorted to apps/workspace/api/loader/[source]/route.tsComplex loader dispatcher (HubSpot, Notion, Sheets, Gmail, Slack)
/api/loader/analyze-templateKEEPPorted to apps/workspace/api/loader/analyze-template/route.tsNotion client SDK + template analysis
/api/loader/creamy-previewKEEPPorted to apps/workspace/api/loader/creamy-preview/route.tsLoader preview system
/api/loader/generate-schemaKEEPPorted to apps/workspace/api/loader/generate-schema/route.tsAI-powered schema generation
/api/loader/input-sourceKEEPPorted to apps/workspace/api/loader/input-source/route.tsInput source management
/api/loader/renderKEEPPorted to apps/workspace/api/loader/render/route.tsUniversal renderer with Notion SDK
/api/loader/stage1KEEPPorted to apps/workspace/api/loader/stage1/route.tsCreamy layer extraction
/api/loader/stage2KEEPPorted to apps/workspace/api/loader/stage2/route.tsFull extraction stage
/api/loader/syncKEEPPorted to apps/workspace/api/loader/sync/route.tsLoader sync scheduler

Reason: The loader system is a large subsystem with 9 routes, many external SDKs (Notion, HubSpot, Google Sheets, Gmail), and complex AI integration. Moving it to Hono would require rewriting all SDK calls for Cloudflare Workers. This is a Phase 2 migration.


12. BYOM / BYOT / Templates (3 routes)

RouteDecisionAction TakenNotes
/api/byomKEEPPorted to apps/workspace/api/byom/route.tsBYOM uses Supabase SSR + @supabase/ssr + cookies; deeply tied to Next.js server patterns
/api/byotKEEPPorted to apps/workspace/api/byot/route.tsBYOT uses Supabase SSR + cookie-based auth
/api/templates/downloadMERGEAdded to packages/api/src/index.tsStatic template definitions moved to DB-driven endpoint

13. Neutron (Browser Tab Intelligence) (3 routes)

RouteDecisionAction TakenNotes
/api/neutron/ingestKEEPPorted to apps/workspace/api/neutron/ingest/route.tsUses Supabase SSR + Neon DB
/api/neutron/clearKEEPPorted to apps/workspace/api/neutron/clear/route.tsUses Supabase SSR + Neon DB
/api/neutron/promoteKEEPPorted to apps/workspace/api/neutron/promote/route.tsUses Supabase SSR + Neon DB

Reason: Neutron depends on a separate Neon Postgres database (@/lib/neon) and Supabase SSR auth. These are external to the D1 data model. Future: migrate to D1 or keep as external service.


14. Analytics & Tracking (2 routes)

RouteDecisionAction TakenNotes
/api/captureKEEPPorted to apps/workspace/api/capture/route.tsCapture API with API secret validation
/api/website/trackMERGEAdded to packages/api/src/analytics.tsVisitor tracking endpoint; writes to D1

New file: packages/api/src/analytics.ts


Summary

CategoryCountMERGEKEEPDISCARD
Billing & Payments10901
Health & Diagnostics5005
Auth & Session2101
Search1100
Metrics & Insights4301
CS & Goals2200
Support & Sync3300
Webhooks11371
AI & Chat5050
Cron Jobs6060
Loader System9090
BYOM/BYOT/Templates3120
Neutron3030
Analytics & Tracking2110
TOTAL6624339

Files Created in packages/api/src/

  1. packages/api/src/billing.ts — Billing sub-router (checkout, plans, subscriptions, invoices, entitlements, cancel, change-plan, subscribe)
  2. packages/api/src/webhooks.ts — Consolidated webhook handlers (HubSpot, Asana, and existing providers; extracted from index.ts inline routes)
  3. packages/api/src/insights.ts — Pattern detection insights
  4. packages/api/src/spend.ts — Spend tracking
  5. packages/api/src/cs.ts — Customer success health scores
  6. packages/api/src/goals.ts — Goal progress tracking
  7. packages/api/src/sync.ts — Sync scheduling & data sync
  8. packages/api/src/analytics.ts — Website visitor tracking

Files Modified in packages/api/src/

  1. packages/api/src/index.ts — Added routes for: search, workspace/bootstrap, support/contact, templates, enhanced health check
  2. packages/api/src/db.ts — Added helper functions for new domains

Files Created in apps/workspace/

The 33 KEEP routes are ported into apps/workspace/api/... preserving their Next.js App Router structure. Import paths updated from @/lib/supabase/... to ~/lib/... where appropriate.

Next Steps (Phase 2)

  1. Migrate AI routes to Cloudflare Workers AI binding (c.env.AI) when Vercel AI SDK adds Worker-compatible streaming
  2. Migrate Loader system to Hono backend by rewriting SDK calls (Notion, HubSpot, Gmail, Sheets) to use raw HTTP + CF Worker-compatible libraries
  3. Migrate Neutron from Neon to D1, or keep as external service with proxy
  4. Migrate Cron jobs to Cloudflare Cron Triggers in wrangler.toml
  5. Migrate Webhooks (Slack, Discord, AI-Relay) to Hono when crypto libraries are verified compatible