[PR #5999] [CLOSED] Add support for additionalFields to API key #6374

Closed
opened 2026-03-13 12:56:28 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5999
Author: @gricha
Created: 11/15/2025
Status: Closed

Base: canaryHead: api-key-additional-fields


📝 Commits (3)

  • 7baf386 Add support for additionalFields to API key plugin
  • 9dedc1b pnpm format
  • a32d4b6 Fix typecheck, generic options

📊 Changes

13 files changed (+453 additions, -260 deletions)

View changed files

📝 packages/better-auth/src/db/schema.ts (+22 -7)
📝 packages/better-auth/src/plugins/api-key/api-key.test.ts (+40 -2)
📝 packages/better-auth/src/plugins/api-key/index.ts (+11 -4)
packages/better-auth/src/plugins/api-key/routes/additional-fields.ts (+50 -0)
📝 packages/better-auth/src/plugins/api-key/routes/create-api-key.ts (+118 -91)
📝 packages/better-auth/src/plugins/api-key/routes/delete-api-key.ts (+3 -3)
📝 packages/better-auth/src/plugins/api-key/routes/get-api-key.ts (+5 -5)
📝 packages/better-auth/src/plugins/api-key/routes/index.ts (+47 -32)
📝 packages/better-auth/src/plugins/api-key/routes/list-api-keys.ts (+5 -5)
📝 packages/better-auth/src/plugins/api-key/routes/update-api-key.ts (+114 -85)
📝 packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts (+11 -11)
📝 packages/better-auth/src/plugins/api-key/types.ts (+12 -0)
📝 packages/better-auth/src/types/plugins.ts (+15 -15)

📄 Description

Hey folks! The problem I'm trying to solve with this PR is an ability to scope API keys to organizations. I happen to use both organization and API key plugin and I wanted to make sure that there's a way to associate the two easily. This allows me to add an organizationId field directly to API key in a similar manner that I would extend organization/user/session. I considered using metadata field, but I figured that this may end up being a field I want to index on, and there's prior art to how these extensions are being made.


Summary by cubic

Adds support for custom additional fields on API keys, enabling first-class fields like organizationId with validation, typing, and persistence. This avoids overloading metadata and allows indexing on these fields.

  • New Features
    • apiKey plugin now accepts schema.apikey.additionalFields to define extra DB fields.
    • create/update API routes validate, persist, and return these fields.
    • Added helpers to build/parse additional field schemas for request payloads.
    • mergeSchema now merges additionalFields into table fields.
    • Types updated to infer additional fields on ApiKey and plugin option schema.
    • Tests cover creating and updating an API key with organizationId.

Written for commit a32d4b6696. Summary will update automatically on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/5999 **Author:** [@gricha](https://github.com/gricha) **Created:** 11/15/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `api-key-additional-fields` --- ### 📝 Commits (3) - [`7baf386`](https://github.com/better-auth/better-auth/commit/7baf386a3c44625e21459fdf4ce31de8c71ae008) Add support for additionalFields to API key plugin - [`9dedc1b`](https://github.com/better-auth/better-auth/commit/9dedc1b9858bf621351e2d967f87c19369ac6e12) pnpm format - [`a32d4b6`](https://github.com/better-auth/better-auth/commit/a32d4b66960e361fb412679a7aad5d884746e619) Fix typecheck, generic options ### 📊 Changes **13 files changed** (+453 additions, -260 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/schema.ts` (+22 -7) 📝 `packages/better-auth/src/plugins/api-key/api-key.test.ts` (+40 -2) 📝 `packages/better-auth/src/plugins/api-key/index.ts` (+11 -4) ➕ `packages/better-auth/src/plugins/api-key/routes/additional-fields.ts` (+50 -0) 📝 `packages/better-auth/src/plugins/api-key/routes/create-api-key.ts` (+118 -91) 📝 `packages/better-auth/src/plugins/api-key/routes/delete-api-key.ts` (+3 -3) 📝 `packages/better-auth/src/plugins/api-key/routes/get-api-key.ts` (+5 -5) 📝 `packages/better-auth/src/plugins/api-key/routes/index.ts` (+47 -32) 📝 `packages/better-auth/src/plugins/api-key/routes/list-api-keys.ts` (+5 -5) 📝 `packages/better-auth/src/plugins/api-key/routes/update-api-key.ts` (+114 -85) 📝 `packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts` (+11 -11) 📝 `packages/better-auth/src/plugins/api-key/types.ts` (+12 -0) 📝 `packages/better-auth/src/types/plugins.ts` (+15 -15) </details> ### 📄 Description Hey folks! The problem I'm trying to solve with this PR is an ability to scope API keys to organizations. I happen to use both organization and API key plugin and I wanted to make sure that there's a way to associate the two easily. This allows me to add an `organizationId` field directly to API key in a similar manner that I would extend organization/user/session. I considered using metadata field, but I figured that this may end up being a field I want to index on, and there's prior art to how these extensions are being made. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds support for custom additional fields on API keys, enabling first-class fields like organizationId with validation, typing, and persistence. This avoids overloading metadata and allows indexing on these fields. - **New Features** - apiKey plugin now accepts schema.apikey.additionalFields to define extra DB fields. - create/update API routes validate, persist, and return these fields. - Added helpers to build/parse additional field schemas for request payloads. - mergeSchema now merges additionalFields into table fields. - Types updated to infer additional fields on ApiKey and plugin option schema. - Tests cover creating and updating an API key with organizationId. <sup>Written for commit a32d4b66960e361fb412679a7aad5d884746e619. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:56:28 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#6374