Agency OS
Multi-tenant SaaS for agencies: organisations, billing, Kanban and permissions.
- Role
- Full-stack engineer (personal project)
- Timeline
- Feb to Jul 2026
- Team
- Solo
- Links
- Code walkthrough on request
Stack:
- Next.js 16
- TypeScript
- Express 5
- Prisma
- PostgreSQL
- Stripe
- Socket.io
Overview
A SaaS platform for agencies, shipped under the product name Stratis. Organisations with members and roles, clients and projects, a Kanban board with comments, attachments and time logs, subscription billing through Stripe, and a portal for the agency's own clients. Built end to end in TypeScript.
The problem
Agencies juggle clients, projects and billing across disconnected tools. Agency OS puts organisations, subscriptions and project delivery in one product, and keeps every tenant's data apart from every other's.
Constraints
- 01
Tenant isolation is non-negotiable: one organisation must never read another's data, whichever endpoint is called.
- 02
Permissions live in the database, not in code, so roles can change without a deploy.
- 03
Billing state has to follow Stripe. That means trusting webhooks, which means verifying them.
- 04
One engineer. The architecture has to stay small enough to hold in one head.
Architecture
- Client
- Next.js 16Marketing site, auth, dashboard, client portal
- API
- Express 5 REST APIJWT auth, helmet, Zod validation, Swagger docs
- Rate limits100 requests / 15 min per IP; 5 logins / hour
- Domain
- RBAC middlewareTenant resolved from org, client, project or task
- Socket.ioRealtime board updates
- Audit log + notificationsWho changed what, and who needs to know
- AI serviceGroq-backed assistant endpoints
- Data
- PostgreSQL via Prisma18 models
- StripeCheckout sessions, signed webhooks
- Uploads + emailMulter, Nodemailer
Decisions and trade-offs
Permissions as data
- Chose
- Role → RolePermission → Permission tables, checked on every request
- Over
- Hard-coded role checks in route handlers
- Why
- Agencies want custom roles. With permissions in the database, a new role is a row, not a release.
- Trade-off
- Every permission check is a query. Fine at this scale; the next step is caching membership per request.
A separate Express API
- Chose
- One long-lived Node process for REST, websockets and webhooks
- Over
- Next.js route handlers on serverless functions
- Why
- Socket.io connections and Stripe webhooks both want a persistent process. Serverless handlers would have needed a separate realtime service anyway.
- Trade-off
- Two deploys and CORS to manage instead of one.
Verify every Stripe webhook
- Chose
- stripe.webhooks.constructEvent with the signing secret
- Over
- Trusting the webhook payload
- Why
- Plans change on webhook events. An unverified endpoint would let anyone upgrade their own subscription with a forged request.
The hardest problem
Resolving the tenant from any URL
Routes arrive in different shapes: /organizations/:orgId/…, but also /projects/:projectId and /tasks/:taskId. A permission check means nothing until you know which organisation the resource belongs to, so the middleware walks the relationship up (task → project → client → organisation), then checks the caller's membership and the role's permission keys.
export const requirePermission = (permissionKey: string) =>
async (req: AuthRequest, res: Response, next: NextFunction) => {
let orgId = req.params.orgId;
if (!orgId && req.params.taskId) {
const task = await prisma.task.findUnique({
where: { id: req.params.taskId },
select: { project: { select: { client: { select: { organizationId: true } } } } },
});
if (task) orgId = task.project.client.organizationId;
}
// …the same walk for clientId and projectId
const membership = await prisma.membership.findUnique({
where: { userId_organizationId: { userId: req.user.id, organizationId: orgId } },
include: { role: { include: { permissions: { include: { permission: true } } } } },
});
if (!membership) return next(new AppError("You are not a member of this organization", 403));
const keys = membership.role.permissions.map((rp) => rp.permission.key);
if (!keys.includes(permissionKey)) return next(new AppError("You do not have permission", 403));
next();
};Super-admin bypass and error handling omitted.
Results
- Data model
- 18 Prisma models
- Users to time logs, with RBAC and billing in the schema
- Abuse limits
- 100 / 15 min
- API requests per IP; logins capped at 5 an hour
What I'd do next
- 01
Denormalise organizationId onto Project and Task, so finding the tenant is one indexed lookup instead of a three-table walk.
- 02
Cache membership and permissions per request, so a screen that makes ten API calls doesn't make ten permission queries.
- 03
Refuse to boot when JWT_SECRET is missing, instead of falling back to a default.
- 04
Grow the tests past auth: a tenant-isolation test for every route that takes an ID.
Live AI Assistant
Chat that knows when to check the web, and cites what it found.
Read the case study