{"slug":"business/ROUTE_SLUG_MANIFEST","title":"API Routes Consolidation Mapping Report","section":"business","sectionLabel":"Business","status":"unclassified","type":"document","path":"business/ROUTE_SLUG_MANIFEST.mdx","bytes":21459,"description":null,"updated":null,"source":"integratewise/Integratewise-docs-wiki","content":"\n**Date:** 2025-01-20  \n**Source:** `_reference/app/app/api/` (66 routes from original app)  \n**Target:** `packages/api/src/` (Hono backend) + `apps/workspace/` (Next.js routes)  \n**Strategy:** Prioritize merging into Hono backend for consistency. Preserve existing auth.ts, entities, metrics, and onboarding endpoints.\n\n---\n\n## Legend\n\n- **MERGE** → Ported/rewritten into `packages/api/src/` as Hono routes using D1\n- **KEEP** → Retained as Next.js API route in `apps/workspace/` (complex AI streaming, Supabase-specific, or edge-specific)\n- **DISCARD** → Redundant with existing backend or no longer needed\n- **DEFER** → Complex dependency chain; keep in reference for future migration\n\n---\n\n## 1. Billing & Payments (10 routes)\n\n| Route                             | Decision    | Action Taken                                     | Notes                                                     |\n| --------------------------------- | ----------- | ------------------------------------------------ | --------------------------------------------------------- |\n| `/api/billing/plans`              | **MERGE**   | Added to `packages/api/src/billing.ts`           | Returns subscription tiers from DB or config              |\n| `/api/billing/subscription`       | **MERGE**   | Added to `packages/api/src/billing.ts`           | Fetch tenant subscription status                          |\n| `/api/billing/entitlements`       | **MERGE**   | Added to `packages/api/src/billing.ts`           | Check feature flags per tenant                            |\n| `/api/billing/invoices`           | **MERGE**   | Added to `packages/api/src/billing.ts`           | List tenant invoices from DB                              |\n| `/api/billing/checkout`           | **MERGE**   | Added to `packages/api/src/billing.ts`           | Stripe checkout session creation                          |\n| `/api/billing/subscribe`          | **MERGE**   | Added to `packages/api/src/billing.ts`           | Create subscription                                       |\n| `/api/billing/cancel`             | **MERGE**   | Added to `packages/api/src/billing.ts`           | Cancel subscription                                       |\n| `/api/billing/change-plan`        | **MERGE**   | Added to `packages/api/src/billing.ts`           | Upgrade/downgrade plan                                    |\n| `/api/billing/webhook/[provider]` | **MERGE**   | Consolidated into `packages/api/src/webhooks.ts` | Stripe/Razorpay/Cashfree/PhonePe webhooks                 |\n| `/api/stripe/webhook`             | **DISCARD** | —                                                | Redundant with `/webhooks/stripe` already in Hono backend |\n\n**New file:** `packages/api/src/billing.ts` — Sub-router mounted in `index.ts`  \n**New file:** `packages/api/src/webhooks.ts` — Consolidated webhook handlers (expanded from inline routes in `index.ts`)\n\n---\n\n## 2. Health & Diagnostics (5 routes)\n\n| Route             | Decision    | Action Taken | Notes                                                     |\n| ----------------- | ----------- | ------------ | --------------------------------------------------------- |\n| `/api/health`     | **DISCARD** | —            | Already served by `/api` and `/` in Hono                  |\n| `/api/liveness`   | **DISCARD** | —            | Redundant; D1 connectivity check added to `/api/health`   |\n| `/api/ping`       | **DISCARD** | —            | Redundant                                                 |\n| `/api/readiness`  | **DISCARD** | —            | Redundant; D1 readiness check added to `/api/health`      |\n| `/api/env/health` | **DISCARD** | —            | Supabase-specific env checks; not needed in D1/Hono stack |\n\n**Enhanced:** `GET /api/health` in `index.ts` now returns D1 status, AI binding status, and env check.\n\n---\n\n## 3. Auth & Session (2 routes)\n\n| Route                      | Decision    | Action Taken                                                            | Notes                                                                 |\n| -------------------------- | ----------- | ----------------------------------------------------------------------- | --------------------------------------------------------------------- |\n| `/api/session`             | **DISCARD** | —                                                                       | Supabase auth session; replaced by `GET /api/auth/me` in Hono auth.ts |\n| `/api/workspace/bootstrap` | **MERGE**   | Added to `packages/api/src/index.ts` as `POST /api/workspace/bootstrap` | Idempotent org/workspace creation; adapted to D1 schema               |\n\n---\n\n## 4. Search (1 route)\n\n| Route         | Decision  | Action Taken                                              | Notes                                                 |\n| ------------- | --------- | --------------------------------------------------------- | ----------------------------------------------------- |\n| `/api/search` | **MERGE** | Added to `packages/api/src/index.ts` as `GET /api/search` | Full-text search across entities using `entities_fts` |\n\n---\n\n## 5. Metrics & Insights (4 routes)\n\n| Route                    | Decision    | Action Taken                            | Notes                                             |\n| ------------------------ | ----------- | --------------------------------------- | ------------------------------------------------- |\n| `/api/metrics/kpis`      | **DISCARD** | —                                       | Already exists as `GET /api/metrics/kpis` in Hono |\n| `/api/insights/patterns` | **MERGE**   | Added to `packages/api/src/insights.ts` | Pattern detection over entity history             |\n| `/api/spend/summary`     | **MERGE**   | Added to `packages/api/src/spend.ts`    | Aggregated spend summary                          |\n| `/api/spend/trend`       | **MERGE**   | Added to `packages/api/src/spend.ts`    | Spend trend over time                             |\n\n**New file:** `packages/api/src/insights.ts`  \n**New file:** `packages/api/src/spend.ts`\n\n---\n\n## 6. Customer Success & Goals (2 routes)\n\n| Route                  | Decision  | Action Taken                         | Notes                               |\n| ---------------------- | --------- | ------------------------------------ | ----------------------------------- |\n| `/api/cs/health-score` | **MERGE** | Added to `packages/api/src/cs.ts`    | Health score calculation per entity |\n| `/api/goals/progress`  | **MERGE** | Added to `packages/api/src/goals.ts` | Goal progress tracking              |\n\n**New file:** `packages/api/src/cs.ts`  \n**New file:** `packages/api/src/goals.ts`\n\n---\n\n## 7. Support & Sync (3 routes)\n\n| Route                  | Decision  | Action Taken                         | Notes                                |\n| ---------------------- | --------- | ------------------------------------ | ------------------------------------ |\n| `/api/support/contact` | **MERGE** | Added to `packages/api/src/index.ts` | Simple support ticket creation in DB |\n| `/api/sync/schedule`   | **MERGE** | Added to `packages/api/src/sync.ts`  | Sync schedule CRUD                   |\n| `/api/data-sync`       | **MERGE** | Added to `packages/api/src/sync.ts`  | Manual sync trigger                  |\n\n**New file:** `packages/api/src/sync.ts`\n\n---\n\n## 8. Webhooks — CRM & Productivity (7 routes)\n\n| Route                            | Decision    | Action Taken                                                      | Notes                                                                                   |\n| -------------------------------- | ----------- | ----------------------------------------------------------------- | --------------------------------------------------------------------------------------- |\n| `/api/webhooks/hubspot`          | **MERGE**   | Enhanced `packages/api/src/webhooks.ts`                           | More detailed contact/deal/company sync                                                 |\n| `/api/webhooks/asana`            | **MERGE**   | Added to `packages/api/src/webhooks.ts`                           | Asana task → entity sync                                                                |\n| `/api/webhooks/slack`            | **KEEP**    | Ported to `apps/workspace/api/webhooks/slack/route.ts`            | Complex interactive signatures + triage AI; needs Node crypto for Ed25519               |\n| `/api/webhooks/discord`          | **KEEP**    | Ported to `apps/workspace/api/webhooks/discord/route.ts`          | Ed25519 verification; Cloudflare Workers can do this but the library stack is different |\n| `/api/webhooks/[provider]`       | **KEEP**    | Ported to `apps/workspace/api/webhooks/[provider]/route.ts`       | Generic dispatcher for 10+ providers; too large to inline                               |\n| `/api/webhooks/ai-relay`         | **KEEP**    | Ported to `apps/workspace/api/webhooks/ai-relay/route.ts`         | HMAC-SHA256 custom signature; specific to relay architecture                            |\n| `/api/webhooks/brainstorm`       | **KEEP**    | Ported to `apps/workspace/api/webhooks/brainstorm/route.ts`       | Brainstorm-specific webhook                                                             |\n| `/api/webhooks/health`           | **MERGE**   | Added to `packages/api/src/webhooks.ts`                           | Simple DB connectivity check for webhooks                                               |\n| `/api/webhook`                   | **KEEP**    | Ported to `apps/workspace/api/webhook/route.ts`                   | Generic inbound webhook with API secret                                                 |\n| `/api/webhook-scheduler/trigger` | **KEEP**    | Ported to `apps/workspace/api/webhook-scheduler/trigger/route.ts` | Complex scheduler trigger with Slack/Discord notifications                              |\n| `/api/hubspot/sync`              | **DISCARD** | —                                                                 | Redundant with `/api/webhooks/hubspot` + manual sync endpoint                           |\n\n**Expanded:** `packages/api/src/webhooks.ts` — Extracted from inline routes in `index.ts` into dedicated module. Added Asana, enhanced HubSpot.\n\n---\n\n## 9. AI & Chat (3 routes)\n\n| Route                            | Decision | Action Taken                                                      | Notes                                                                                                                                      |\n| -------------------------------- | -------- | ----------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |\n| `/api/ai/chat`                   | **KEEP** | Ported to `apps/workspace/api/ai/chat/route.ts`                   | Uses Vercel AI SDK (`ai` package) with `streamText` + `convertToModelMessages`; incompatible with Cloudflare Workers Hono without bundling |\n| `/api/chat/stream`               | **KEEP** | Ported to `apps/workspace/api/chat/stream/route.ts`               | Uses `@ai-sdk/openai` + Vercel streaming; needs Next.js edge runtime                                                                       |\n| `/api/brainstorm/analyze`        | **KEEP** | Ported to `apps/workspace/api/brainstorm/analyze/route.ts`        | Complex AI workflow with Zod schema + Vercel AI SDK                                                                                        |\n| `/api/brainstorm/daily-insights` | **KEEP** | Ported to `apps/workspace/api/brainstorm/daily-insights/route.ts` | Vercel AI SDK + cron trigger                                                                                                               |\n| `/api/brainstorm/execute`        | **KEEP** | Ported to `apps/workspace/api/brainstorm/execute/route.ts`        | Complex multi-step AI execution                                                                                                            |\n\n**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.\n\n---\n\n## 10. Cron Jobs (6 routes)\n\n| Route                       | Decision | Action Taken                                                 | Notes                                                  |\n| --------------------------- | -------- | ------------------------------------------------------------ | ------------------------------------------------------ |\n| `/api/cron/daily-insights`  | **KEEP** | Ported to `apps/workspace/api/cron/daily-insights/route.ts`  | Vercel Cron; triggers `/api/brainstorm/daily-insights` |\n| `/api/cron/hourly-insights` | **KEEP** | Ported to `apps/workspace/api/cron/hourly-insights/route.ts` | Complex multi-provider AI insights                     |\n| `/api/cron/integrity-check` | **KEEP** | Ported to `apps/workspace/api/cron/integrity-check/route.ts` | Neon DB integrity checks; D1 doesn't need this         |\n| `/api/cron/outbox`          | **KEEP** | Ported to `apps/workspace/api/cron/outbox/route.ts`          | Outbox pattern processing                              |\n| `/api/cron/spend-insights`  | **KEEP** | Ported to `apps/workspace/api/cron/spend-insights/route.ts`  | Spend insights cron                                    |\n| `/api/cron/sync-scheduler`  | **KEEP** | Ported to `apps/workspace/api/cron/sync-scheduler/route.ts`  | Sync scheduler cron                                    |\n\n**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.\n\n---\n\n## 11. Loader System (9 routes)\n\n| Route                          | Decision | Action Taken                                                    | Notes                                                             |\n| ------------------------------ | -------- | --------------------------------------------------------------- | ----------------------------------------------------------------- |\n| `/api/loader/[source]`         | **KEEP** | Ported to `apps/workspace/api/loader/[source]/route.ts`         | Complex loader dispatcher (HubSpot, Notion, Sheets, Gmail, Slack) |\n| `/api/loader/analyze-template` | **KEEP** | Ported to `apps/workspace/api/loader/analyze-template/route.ts` | Notion client SDK + template analysis                             |\n| `/api/loader/creamy-preview`   | **KEEP** | Ported to `apps/workspace/api/loader/creamy-preview/route.ts`   | Loader preview system                                             |\n| `/api/loader/generate-schema`  | **KEEP** | Ported to `apps/workspace/api/loader/generate-schema/route.ts`  | AI-powered schema generation                                      |\n| `/api/loader/input-source`     | **KEEP** | Ported to `apps/workspace/api/loader/input-source/route.ts`     | Input source management                                           |\n| `/api/loader/render`           | **KEEP** | Ported to `apps/workspace/api/loader/render/route.ts`           | Universal renderer with Notion SDK                                |\n| `/api/loader/stage1`           | **KEEP** | Ported to `apps/workspace/api/loader/stage1/route.ts`           | Creamy layer extraction                                           |\n| `/api/loader/stage2`           | **KEEP** | Ported to `apps/workspace/api/loader/stage2/route.ts`           | Full extraction stage                                             |\n| `/api/loader/sync`             | **KEEP** | Ported to `apps/workspace/api/loader/sync/route.ts`             | Loader sync scheduler                                             |\n\n**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.\n\n---\n\n## 12. BYOM / BYOT / Templates (3 routes)\n\n| Route                     | Decision  | Action Taken                                 | Notes                                                                                      |\n| ------------------------- | --------- | -------------------------------------------- | ------------------------------------------------------------------------------------------ |\n| `/api/byom`               | **KEEP**  | Ported to `apps/workspace/api/byom/route.ts` | BYOM uses Supabase SSR + `@supabase/ssr` + cookies; deeply tied to Next.js server patterns |\n| `/api/byot`               | **KEEP**  | Ported to `apps/workspace/api/byot/route.ts` | BYOT uses Supabase SSR + cookie-based auth                                                 |\n| `/api/templates/download` | **MERGE** | Added to `packages/api/src/index.ts`         | Static template definitions moved to DB-driven endpoint                                    |\n\n---\n\n## 13. Neutron (Browser Tab Intelligence) (3 routes)\n\n| Route                  | Decision | Action Taken                                            | Notes                       |\n| ---------------------- | -------- | ------------------------------------------------------- | --------------------------- |\n| `/api/neutron/ingest`  | **KEEP** | Ported to `apps/workspace/api/neutron/ingest/route.ts`  | Uses Supabase SSR + Neon DB |\n| `/api/neutron/clear`   | **KEEP** | Ported to `apps/workspace/api/neutron/clear/route.ts`   | Uses Supabase SSR + Neon DB |\n| `/api/neutron/promote` | **KEEP** | Ported to `apps/workspace/api/neutron/promote/route.ts` | Uses Supabase SSR + Neon DB |\n\n**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.\n\n---\n\n## 14. Analytics & Tracking (2 routes)\n\n| Route                | Decision  | Action Taken                                    | Notes                                   |\n| -------------------- | --------- | ----------------------------------------------- | --------------------------------------- |\n| `/api/capture`       | **KEEP**  | Ported to `apps/workspace/api/capture/route.ts` | Capture API with API secret validation  |\n| `/api/website/track` | **MERGE** | Added to `packages/api/src/analytics.ts`        | Visitor tracking endpoint; writes to D1 |\n\n**New file:** `packages/api/src/analytics.ts`\n\n---\n\n## Summary\n\n| Category             | Count  | MERGE  | KEEP   | DISCARD |\n| -------------------- | ------ | ------ | ------ | ------- |\n| Billing & Payments   | 10     | 9      | 0      | 1       |\n| Health & Diagnostics | 5      | 0      | 0      | 5       |\n| Auth & Session       | 2      | 1      | 0      | 1       |\n| Search               | 1      | 1      | 0      | 0       |\n| Metrics & Insights   | 4      | 3      | 0      | 1       |\n| CS & Goals           | 2      | 2      | 0      | 0       |\n| Support & Sync       | 3      | 3      | 0      | 0       |\n| Webhooks             | 11     | 3      | 7      | 1       |\n| AI & Chat            | 5      | 0      | 5      | 0       |\n| Cron Jobs            | 6      | 0      | 6      | 0       |\n| Loader System        | 9      | 0      | 9      | 0       |\n| BYOM/BYOT/Templates  | 3      | 1      | 2      | 0       |\n| Neutron              | 3      | 0      | 3      | 0       |\n| Analytics & Tracking | 2      | 1      | 1      | 0       |\n| **TOTAL**            | **66** | **24** | **33** | **9**   |\n\n---\n\n## Files Created in `packages/api/src/`\n\n1. `packages/api/src/billing.ts` — Billing sub-router (checkout, plans, subscriptions, invoices, entitlements, cancel, change-plan, subscribe)\n2. `packages/api/src/webhooks.ts` — Consolidated webhook handlers (HubSpot, Asana, and existing providers; extracted from `index.ts` inline routes)\n3. `packages/api/src/insights.ts` — Pattern detection insights\n4. `packages/api/src/spend.ts` — Spend tracking\n5. `packages/api/src/cs.ts` — Customer success health scores\n6. `packages/api/src/goals.ts` — Goal progress tracking\n7. `packages/api/src/sync.ts` — Sync scheduling & data sync\n8. `packages/api/src/analytics.ts` — Website visitor tracking\n\n## Files Modified in `packages/api/src/`\n\n1. `packages/api/src/index.ts` — Added routes for: search, workspace/bootstrap, support/contact, templates, enhanced health check\n2. `packages/api/src/db.ts` — Added helper functions for new domains\n\n## Files Created in `apps/workspace/`\n\nThe 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.\n\n## Next Steps (Phase 2)\n\n1. **Migrate AI routes** to Cloudflare Workers AI binding (`c.env.AI`) when Vercel AI SDK adds Worker-compatible streaming\n2. **Migrate Loader system** to Hono backend by rewriting SDK calls (Notion, HubSpot, Gmail, Sheets) to use raw HTTP + CF Worker-compatible libraries\n3. **Migrate Neutron** from Neon to D1, or keep as external service with proxy\n4. **Migrate Cron jobs** to Cloudflare Cron Triggers in `wrangler.toml`\n5. **Migrate Webhooks** (Slack, Discord, AI-Relay) to Hono when crypto libraries are verified compatible\n"}