name: supabase-api-route
description: Builds and reviews Next.js App Router API routes backed by Supabase in this codebase. Use when editing src/app/api/**/route.ts, adding CRUD endpoints, validating request data, or enforcing auth and subscription checks.
Supabase API Route
Use this skill for route handlers in this repository.
Repository patterns
- Most route handlers are in
src/app/api/**/route.ts. - Auth commonly uses
getServerSession(authOptions)plusresolveSessionUser(session.user). - Supabase access commonly uses
createAdminClient(). - Some routes also wrap handlers with
withRateLimit,withErrorHandling, andwithAuthorization. - Tables use capitalized names such as
User,Company, andInvoice.
Instructions
- Start from an existing route with similar behavior before inventing a new pattern.
- Parse request params and bodies with Zod on the server.
- Authenticate first, then resolve the real user record with
resolveSessionUser. - Scope reads and writes by
sessionUser.id. - Reuse repository middleware when the route is public-facing or high traffic.
- For quota-bound resources such as companies or invoices, call
checkSubscriptionLimitsbefore insertions. - Return precise HTTP status codes and concise JSON error payloads.
- Normalize numeric fields and date handling explicitly when serializing Supabase data back to the client.
- Keep user-visible error strings aligned with the surrounding language in that part of the app.
Verification
- Unauthenticated request returns
401. - Invalid payload returns
400. - Resource conflict or quota violation returns the intended
409or403. - Successful request only reads or mutates rows for the current user.