Build Your First Feature
Add a Projects page — a table, a page, and a plan-gated action — the way the kit expects.
This walks through adding Projects: a list every member of an organization can see, and a form that creates one if the organization's plan allows it. It touches each layer once, so the same steps apply to whatever you're actually building.
The sample plans in src/config/billing.ts already include a
create-projects feature, so the plan gating works as soon as you're done.
Add the table
Create src/db/schema/projects.ts. Every row carries the orgId it belongs
to — data is scoped to the organization, not the user (see
Organizations):
import { index, pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core'
export const projects = pgTable(
'projects',
{
id: uuid('id').defaultRandom().primaryKey(),
orgId: text('org_id').notNull(),
name: text('name').notNull(),
createdAt: timestamp('created_at', { mode: 'date', withTimezone: true })
.defaultNow()
.notNull(),
},
(t) => [index('projects_org_id_idx').on(t.orgId)]
)Export it from src/db/schema/index.ts, then generate and apply the
migration:
export * from './billing'
export * from './projects'pnpm db:generate
pnpm db:migrateAdd the Server Action
Mutations are Server Actions, not API routes (see
API & Server Actions). Create
src/app/[locale]/(saas)/_actions/projects.ts:
'use server'
import { auth } from '@clerk/nextjs/server'
import { revalidatePath } from 'next/cache'
import { db } from '@/db'
import { projects } from '@/db/schema'
import { getOrgBilling, hasFeature } from '@/lib/billing/server'
export async function createProject(name: string) {
const { orgId } = await auth()
if (!orgId) throw new Error('Unauthorized')
const billing = await getOrgBilling(orgId)
if (!hasFeature(billing, 'create-projects'))
throw new Error('Your plan does not include projects.')
await db.insert(projects).values({ orgId, name })
revalidatePath('/[locale]/app/projects', 'page')
}The action takes orgId from the session, never from its arguments, and
checks the plan itself — hiding the form in the UI isn't enough, since
anyone can call an action directly. A thrown error shows up as a toast in the
next step.
Add the page
Pages inside (gated) only render for organizations with access — the
upgrade gate and the member-limit lock are applied by (gated)/layout.tsx,
so there's nothing to add for them. Create
src/app/[locale]/(saas)/app/(gated)/projects/page.tsx:
import { auth } from '@clerk/nextjs/server'
import { desc, eq } from 'drizzle-orm'
import { db } from '@/db'
import { projects } from '@/db/schema'
import { getOrgBilling, hasFeature } from '@/lib/billing/server'
import { CreateProjectForm } from './_components/create-project-form'
export default async function ProjectsPage() {
const { orgId } = await auth()
const billing = await getOrgBilling(orgId!)
const rows = await db.query.projects.findMany({
where: eq(projects.orgId, orgId!),
orderBy: desc(projects.createdAt),
})
return (
<div className="flex flex-1 flex-col gap-6 p-6">
{hasFeature(billing, 'create-projects') && <CreateProjectForm />}
<ul className="flex flex-col gap-2">
{rows.map((project) => (
<li key={project.id} className="rounded-lg border p-4">
{project.name}
</li>
))}
</ul>
</div>
)
}getOrgBilling() is cached per request, so calling it here and in the
layout costs one query.
Add the form
Create src/app/[locale]/(saas)/app/(gated)/projects/_components/create-project-form.tsx.
useAction() handles the pending state and turns thrown errors into toasts:
'use client'
import { useState } from 'react'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { useAction } from '@/hooks/use-action'
import { createProject } from '../../../../_actions/projects'
export function CreateProjectForm() {
const [name, setName] = useState('')
const { isPending, execute } = useAction()
return (
<form
className="flex gap-2"
onSubmit={(e) => {
e.preventDefault()
execute(() => createProject(name), { success: 'Project created' })
setName('')
}}
>
<Input
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Project name"
required
/>
<Button type="submit" disabled={isPending}>
Create
</Button>
</form>
)
}The strings are hardcoded to keep the example short. To translate them, move
them into src/i18n/messages/*.json — see
Internationalization.
Link it in the sidebar
In src/app/[locale]/(saas)/_components/app-sidebar.tsx, import FolderIcon
from lucide-react and add an entry to appItems:
{ title: t('projects'), url: '/app/projects', icon: <FolderIcon /> },In _components/site-header.tsx, add the page's title to the header:
type RouteKey = 'dashboard' | 'projects' | 'account' | 'organization'
const ROUTE_ENTRIES: [prefix: string, key: RouteKey][] = [
['/app/dashboard', 'dashboard'],
['/app/projects', 'projects'],
// ...
]Then add both labels to src/i18n/messages/en.json (and fr.json):
"nav": {
"projects": "Projects",
"routes": {
"projects": "Projects"
}
}Open /app/projects and create a project. To see the gate work, remove
create-projects from the free plan's features: the form disappears for
free organizations, and calling the action anyway throws.
Limit projects per plan
To cap how many projects each plan allows, add a maxProjects limit to
every plan in src/config/billing.ts (-1 means unlimited):
limits: { maxMembers: 1, maxProjects: 3 },Then check it in the action, before the insert:
// Add to the existing imports:
import { count, eq } from 'drizzle-orm'
import { hasReachedLimit } from '@/lib/billing/server'
const [{ total }] = await db
.select({ total: count() })
.from(projects)
.where(eq(projects.orgId, orgId))
if (hasReachedLimit(billing, 'maxProjects', total))
throw new Error('Upgrade your plan to create more projects.')A plan without the key counts as a limit of 0, so forgetting it on one plan
blocks that plan entirely.
What you used
- Database — a table scoped by
orgId, plus a migration. - API & Server Actions — a Server Action for the write.
- Billing —
hasFeature()andhasReachedLimit()against the organization's plan. - Organizations —
orgIdfromauth()as the tenancy boundary.