Skill v1.0.0
currentAutomated scan100/100name: payload-cms description: > Payload CMS (v3, Next.js-native) expert guidance — collections, fields, access control, hooks, the Local API, migrations, and admin-panel customization. Use when working in a Payload project: defining or editing collections/globals, writing field configs, access-control functions, collection/field hooks, Local API queries, custom admin views/components, or database migrations. Encodes hard-won project rules: hand-author migrations (never trust migrate:create's destructive diffs from stale snapshots), and verify Entra/SSO-gated admin views via offline renders. Triggers on: payload, payloadcms, "collection config", "field config", access control, beforeChange/ afterChange hooks, Local API, getPayload, payload.find/create/update, admin components, payload migrate, Postgres/Mongo adapter, Payload + Next.js. metadata: version: "1.0.0"
Payload CMS
Payload v3 is a Next.js-native headless CMS: it runs inside your App Router app (app/(payload)/), ships a React admin panel, and exposes REST, GraphQL, and a first-class Local API (direct DB access, no HTTP). Config is TypeScript.
Project hard rules (learned the hard way)
- Hand-author migrations. Do NOT trust `payload migrate:create`.
It diffs against a snapshot that goes stale, so it emits destructive diffs (drops/re-creates columns, loses data). Instead: write the migration file by hand — up() and down() with explicit, additive SQL/adapter calls. Review every generated diff before committing; if it drops anything you didn't intend, throw it away and write it manually.
- Admin views are often SSO/Entra-gated → you can't just open `/admin`.
Verify custom admin components (analytics views, charts) with offline renders (render the component to static HTML/PNG, or headless-Chrome the built output) rather than relying on a live authenticated session.
- Keep the admin on-brand: custom chart components use the project palette,
not Payload defaults.
Core concepts
- Collections = repeatable docs (Users, Posts, Media). Globals = single
docs (site settings, nav). Both live in payload.config.ts under collections / globals and each is a typed config object.
- Fields are the unit of schema. Common:
text,textarea,richText,
number, select, relationship, array, blocks, group, upload, json, date, checkbox. Every field can carry access, hooks, admin, validate, defaultValue, required, unique, index, localized.
- Auth is a collection flag:
{ auth: true }on Users turns on login,
JWT/cookies, me, refresh, reset-password, etc.
Access control (the security surface)
Access functions run on every request and return boolean or a query constraint (a Where) that filters what the user can see:
// collections/Posts.tsexport const Posts: CollectionConfig = {slug: 'posts',access: {read: ({ req: { user } }) => {if (user?.role === 'admin') return true // full accessreturn { status: { equals: 'published' } } // row-level filter},create: ({ req: { user } }) => Boolean(user),update: ({ req: { user } }) => user?.role === 'admin',delete: ({ req: { user } }) => user?.role === 'admin',},}
Returning a Where (not just true/false) is the idiom for row-level security. Field-level access ({ read, update }) hides/locks individual fields.
Hooks
- Collection hooks:
beforeChange,afterChange,beforeRead,afterRead,
beforeDelete, afterDelete, beforeValidate.
- Field hooks:
beforeChange,afterRead, etc. — scoped to one field. - Keep side effects (emails, cache invalidation, external syncs) in
afterChange/afterDelete, not beforeChange. Guard against infinite loops: a hook that calls payload.update on its own collection re-triggers itself — use context flags or req to break the cycle.
hooks: {afterChange: [async ({ doc, operation, req }) => {if (operation === 'create') await notify(req.payload, doc)return doc}],}
Local API (prefer it server-side)
Inside Server Components, Route Handlers, hooks, and scripts, use the Local API — it skips HTTP and access control (runs as admin) unless you pass overrideAccess: false + user.
import { getPayload } from 'payload'import config from '@payload-config'const payload = await getPayload({ config })const { docs } = await payload.find({collection: 'posts',where: { status: { equals: 'published' } },depth: 1, // how many relationship levels to populatelimit: 10,sort: '-createdAt',})
depthcontrols relationship population — keep it low to avoid over-fetching.select/populatetrim the payload; use them on hot paths.- To enforce a user's permissions in the Local API, pass
{ overrideAccess: false, user }.
Admin customization
- Swap components via
admin.components(customviews,fields,Nav,
beforeDashboard, Logo). Custom views are React Server Components by default; mark 'use client' only where you need interactivity (charts).
- Register a custom view with a path + component; it renders inside the admin
shell (so it inherits auth/nav). For analytics dashboards, fetch via Local API in the server component, pass data to a client chart component.
- Verify without a live login: render the component in a standalone route or
a test harness and screenshot with headless Chrome — the admin's Entra gate otherwise blocks automated checks.
Migrations workflow
payload migrate:status # what's appliedpayload migrate # apply pendingpayload migrate:down # roll back the last
- Author
up()/down()by hand (see rule #1). Makeup()additive and
reversible; make down() actually undo it.
- For Postgres, use the adapter's
db.execute(sql\...\)for explicit DDL when
the generated diff is wrong.
- Run migrations in CI/deploy, not implicitly at boot in production.
Common gotchas
- `depth` runaway: default
depth: 2on nested relationships explodes query
cost. Set it explicitly.
- Type generation: run
payload generate:typesafter schema changes; import
from payload-types.ts for end-to-end types.
- Rich text: v3 defaults to Lexical. Store as JSON; render with the provided
serializer, don't hand-parse.
- Uploads:
upload: truecollections need a storage adapter (local disk,
S3/Azure Blob) — configure it, don't rely on local disk in serverless/Azure.
- `req.payload` is available inside hooks — reuse it, don't call
getPayload
again per hook.