multi-tenant-api-route

Enforce multi-tenant data isolation in Next.js API routes with businessId filtering.

Updated Oct 10, 2025
One-click install
npx skills add https://github.com/doorscomputers/nextjspos --skill multi-tenant-api-route
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: multi-tenant-api-route
Source: https://github.com/doorscomputers/nextjspos/tree/main/.claude/skills/multi-tenant-api-route
Command: npx skills add https://github.com/doorscomputers/nextjspos --skill multi-tenant-api-route

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Securely implement multi-tenant API routes that enforce authentication, authorization, and business data isolation across all /src/app/api endpoints.

Core Features & Use Cases

  • Enforced multi-tenant isolation via mandatory businessId filtering on all queries.
  • Authentication using NextAuth session with structured user object including permissions and roles.
  • Role-based access control with configurable PERMISSIONS checks.
  • Ownership verification for GET/PUT/DELETE operations to prevent cross-tenant access.
  • Soft delete support and consistent error handling for API routes.

Quick Start

Review existing /src/app/api routes and apply the standard multi-tenant pattern to new endpoints. Implement authentication with getServerSession(authOptions), extract businessId from session.user, and enforce permission checks using PERMISSIONS. Ensure every Prisma query includes businessId: parseInt(user.businessId) and implement soft deletes where applicable.

Frequently Asked Questions about multi-tenant-api-route

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I enforce multi-tenant data isolation in Next.js API routes?

To enforce multi-tenant data isolation in Next.js API routes, apply mandatory businessId filtering to all Prisma queries. This ensures every database operation is scoped strictly to the authenticated user's business, preventing cross-tenant data access.

How does NextAuth getServerSession work with role-based access control in Prisma?

NextAuth getServerSession works with role-based access control by retrieving a structured user object containing roles and permissions. You apply configurable PERMISSIONS checks within your API routes to restrict access before executing Prisma database operations.

What is the best way to prevent cross-tenant access when updating or deleting records?

The best way to prevent cross-tenant access during updates or deletions is to implement ownership verification. You must verify the requesting user's businessId against the target record's businessId before proceeding with any GET, PUT, or DELETE operations.

Can I implement soft deletes consistently across multi-tenant API endpoints?

Yes, you can implement soft deletes consistently across multi-tenant API endpoints. The pattern supports soft deletion alongside mandatory businessId filtering and consistent error handling to maintain data isolation without permanently removing business records.

Why should I include businessId on every query in a multi-tenant Prisma application?

You must include businessId on every Prisma query because it acts as the primary tenant isolation boundary. Omitting this filter exposes business data from other tenants, completely breaking the required multi-tenant security model.

Do I need NextAuth to use this multi-tenant API routing pattern?

Yes, this multi-tenant API routing pattern requires NextAuth. It relies specifically on the getServerSession function to authenticate users and extract the businessId necessary for enforcing mandatory tenant data isolation on your endpoints.