Skip to main content

Web API Routes (Next.js)

All routes live under apps/web/src/app/api/. These handle server-side operations that require access to Firebase Admin SDK or Stripe secrets — operations that cannot be performed from the browser.


Authentication & Sessions

RouteMethodAuthDescription
/api/auth/sessionPOSTBearer ID tokenCreates HTTP-only __session cookie via Firebase Admin createSessionCookie()
/api/auth/sessionDELETEClears the __session cookie

The __session cookie is the primary auth mechanism for the web app. It's set as HTTP-only to prevent XSS access and is verified server-side on protected routes.


Billing & Subscriptions

RouteMethodAuthDescription
/api/create-checkout-sessionPOSTBearer ID tokenCreates a Stripe Checkout session. Body: { planId: "starter" | "pro" }. Returns { sessionId }
/api/verify-sessionPOSTFinalizes subscription after checkout. Body: { sessionId }. Returns { subscription, customToken, success }
/api/billing/portalPOST__session cookieCreates a Stripe Billing Portal session. Returns { url }
/api/billing/change-planPOSTBearer ID tokenInstant plan upgrade. Body: { planId }. Returns { success, subscription, requiresAction?, paymentIntentClientSecret? }

Checkout flow notes

  • create-checkout-session validates the stored Stripe customer, reuses by email if the stored ID is missing, or creates a new customer
  • verify-session uses the session_id to look up the Stripe session, find/create the user, link stripeCustomerId, and upsert the subscription document
  • When change-plan returns requiresAction: true, confirm the paymentIntentClientSecret via Stripe.js before refreshing claims

Credits

RouteMethodAuthDescription
/api/credits/balanceGET__session cookieReturns { balance, periodStart, periodEnd, planId, priceId }

API Keys & Request Logs

RouteMethodAuthDescription
/api/api-keysGET__session cookieList all API keys for the authenticated user
/api/api-keysPOST__session cookieCreate a new API key. Mirrors hashed key to root apiKeys/{hash} for Functions validation
/api/api-keys/[id]DELETE__session cookieRevoke a key by ID. Mirrors revocation to root apiKeys/{hash}
/api/api-requestsGET__session cookiePaginated list of recent Public API requests from users/{uid}/apiRequests

Library

RouteMethodAuthDescription
/api/library/upload-urlPOST__session cookieGenerates a signed upload URL for library assets
/api/library/folders/[folderId]GET/POST__session cookieList or manage library folder contents

Webhooks

RouteMethodAuthDescription
/api/webhooks/firstpromoterPOSTSignatureFirstPromoter referral tracking webhook

Stripe webhooks are handled by the Firebase Function stripeWebhook, not by a Next.js API route. Configure the Stripe Dashboard to point to your deployed Cloud Functions URL.


Utilities

RouteMethodAuthDescription
/api/debug-envGETShows which environment variables are present (not their values)
/api/tiktok/callbackGETLocal desktop OAuth proxy for TikTok development

Middleware

The Next.js middleware (src/middleware.js) enforces the presence of __session cookie on protected paths:

  • /dashboard/*
  • /create/*
  • /posts/*
  • /settings/*
  • /admin/*
  • /automations/*
  • /billing/*
  • /library/*
  • /templates/*

Requests without a valid session cookie are redirected to the sign-in page.


Frontend Usage

  • AuthContext triggers POST /api/auth/session whenever the Firebase ID token changes (sign-in, refresh)
  • Checkout success page calls POST /api/verify-session to finalize the subscription
  • Settings page manages API keys via the /api/api-keys routes
  • Billing page opens the Stripe portal via POST /api/billing/portal