Org API Route
Build public API endpoints scoped to the organization that owns the API key.
Keep route files tiny, keep auth and org resolution centralized, and document
each endpoint as its own page.
Read First
Before editing, read:
convex/_generated/ai/guidelines.md.agents/rules/api-routes.md.agents/rules/convex-authorization-dto.md.agents/rules/mdx.mdwhen touching docs- Existing references:
src/lib/api/public-api.tsconvex/apiKeys/functions.tsconvex/apiKeys/actions.tsconvex/apiKeys/queries.tssrc/routes/api/v1/me.tssrc/routes/api/v1/members.tssrc/routes/api/v1/members.$memberId.tssrc/routes/orgs/$orgSlug/(navigation)/settings/api-keys/index.tsxcontent/docs/api-me.mdxcontent/docs/api-members.mdxcontent/docs/api-member.mdx
Architecture
- HTTP route files call only
createPublicApiHandler(...). - Public Convex actions use
orgApiAction(...). - Internal Convex reads use
orgApiQuery(...). verifyApiKeyForOrg(...)verifies the key and exposesorganizationId.- Do not duplicate API-key extraction, verification, or org lookup in route files.
- Do not call
authClientfrom route files or the API-key settings UI.
Response Shape
- Keep endpoint payloads focused on the resource requested.
- Do not attach
organizationto every response by default. - Use
GET /api/v1/mefor the organization attached to the API key. - Use resource endpoints for resource payloads only:
GET /api/v1/membersreturns{ members }.GET /api/v1/members/:memberIdreturns{ member }.GET /api/v1/mereturns{ organization }.createPublicApiHandlerstripsokandstatusfrom successful action
results before sending JSON.
- Missing or invalid keys return
401. - Missing org or resource returns
404.
Documentation Rules
- Always create one doc page per public endpoint.
- Use
.agents/skills/add-documentation/scripts/create-doc.shfor new docs,
then replace the generated template with endpoint-specific content.
- Title pages by operation, not broad groups:
List Organization Members APIGet Organization Member APICurrent Organization API- Keep examples scoped to that endpoint only. Do not document list and detail
endpoints on the same page.
- Add every new or changed endpoint doc to
CHANGELOG.md.
API-Key Settings UI
- Creation happens in a dialog.
- The newly generated key is revealed in a dialog only; never render a large
inline one-time-key card under the page header.
- Existing keys render in a table.
- Row actions live in a dropdown menu, not inline icon buttons.
- The docs entry near the
API Keystitle should stay minimal: a simple text
link labeled Docs.
API Key CRUD Learning
@better-auth/api-key@1.6.10 can throw dynamic module import unsupported
when its create/delete endpoint permission path runs inside a Convex mutation.
For organization API-key settings:
- Keep the outer function as
orgMutation(...)with the repo's permission gate. - Create and delete keys through
components.betterAuth.adapter. - Generate keys with the configured
nsk_prefix. - Store the SHA-256 base64url hash in the Better Auth
apikeymodel. - Return the raw key only once from the create mutation.
- Delete by both
_idandreferenceIdso keys stay scoped to the active org.
Workflow
- Add or update the internal
orgApiQuery. - Add or update the public
orgApiAction. - Add or update the
src/routes/api/v1/*route withcreatePublicApiHandler. - Add or update exactly one docs page per endpoint.
- Update API-key settings UI only when the discoverability surface changes.
- Update
CHANGELOG.md. - Run
pnpm ts,pnpm lint:ci,pnpm test:ci, andpnpm buildwhen route
behavior or docs are touched.