[PR #5720] [CLOSED] feat(api-key): add create, update and delete hooks #31778

Closed
opened 2026-04-17 22:39:35 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5720
Author: @Blackmamoth
Created: 11/1/2025
Status: Closed

Base: canaryHead: feat/api-key-hooks


📝 Commits (7)

  • ea802ed feat: add create, update and delete hooks for api key plugin
  • fa3069d docs: add documentation for api key hooks
  • 7b7dd5d refactor: use BAD_REQUEST for delete hook failure instead of NOT_FOUND
  • 0ac5a73 add null check for apiKeyData to prevent runtime errors
  • 2d48838 feat: add data serialization/deserialization to before/after hooks,
  • e4fa15f fix prepareApiKeyForHook and prepareApiKeyForDB function implementation
  • 4c02248 Merge branch 'canary' into feat/api-key-hooks

📊 Changes

8 files changed (+536 additions, -4 deletions)

View changed files

📝 docs/content/docs/plugins/api-key.mdx (+103 -0)
📝 packages/better-auth/src/plugins/api-key/api-key.test.ts (+283 -0)
📝 packages/better-auth/src/plugins/api-key/index.ts (+1 -0)
📝 packages/better-auth/src/plugins/api-key/routes/create-api-key.ts (+26 -2)
📝 packages/better-auth/src/plugins/api-key/routes/delete-api-key.ts (+17 -1)
📝 packages/better-auth/src/plugins/api-key/routes/index.ts (+29 -0)
📝 packages/better-auth/src/plugins/api-key/routes/update-api-key.ts (+25 -1)
📝 packages/better-auth/src/plugins/api-key/types.ts (+52 -0)

📄 Description

Description

Adds lifecycle hooks (create, update, delete) for the API Key plugin.

Closes #5694

Changes

  • Added before/after hooks for create, update, and delete operations
  • Added tests for all hook scenarios
  • Updated documentation with hook examples

Summary by cubic

Adds before/after lifecycle hooks to the API Key plugin for create, update, and delete so you can validate, modify, or block these actions. Also updates docs, tests, and adds a new error code for hook failures.

  • New Features
    • Hooks: before can return false to cancel, or { data } to modify; hooks receive parsed metadata/permissions and values are serialized automatically; after runs with the created/updated/deleted record.
    • Errors: adds BEFORE_HOOK_FAILED; returns BAD_REQUEST when a before hook cancels create, update, or delete.
    • Docs and tests: extensive examples and coverage for all hook paths.

Written for commit 4c02248a63. 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/5720 **Author:** [@Blackmamoth](https://github.com/Blackmamoth) **Created:** 11/1/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `feat/api-key-hooks` --- ### 📝 Commits (7) - [`ea802ed`](https://github.com/better-auth/better-auth/commit/ea802ed42bb83be7030b1c88d326b646a7873bd8) feat: add create, update and delete hooks for api key plugin - [`fa3069d`](https://github.com/better-auth/better-auth/commit/fa3069dc6880368e28f476cedca212cbd58e1b84) docs: add documentation for api key hooks - [`7b7dd5d`](https://github.com/better-auth/better-auth/commit/7b7dd5d09f203b7eac7ae51591933ee87b6be904) refactor: use BAD_REQUEST for delete hook failure instead of NOT_FOUND - [`0ac5a73`](https://github.com/better-auth/better-auth/commit/0ac5a73f54680a539d806b9c5b2766eb60a13ddb) add null check for apiKeyData to prevent runtime errors - [`2d48838`](https://github.com/better-auth/better-auth/commit/2d48838f554cbb8242078d36ecbcfd916117f2b8) feat: add data serialization/deserialization to before/after hooks, - [`e4fa15f`](https://github.com/better-auth/better-auth/commit/e4fa15f28e4b590112c4f7555b2502606156eb43) fix prepareApiKeyForHook and prepareApiKeyForDB function implementation - [`4c02248`](https://github.com/better-auth/better-auth/commit/4c02248a63ccf9ebca76adc25bf5f14a8fcd8da8) Merge branch 'canary' into feat/api-key-hooks ### 📊 Changes **8 files changed** (+536 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/api-key.mdx` (+103 -0) 📝 `packages/better-auth/src/plugins/api-key/api-key.test.ts` (+283 -0) 📝 `packages/better-auth/src/plugins/api-key/index.ts` (+1 -0) 📝 `packages/better-auth/src/plugins/api-key/routes/create-api-key.ts` (+26 -2) 📝 `packages/better-auth/src/plugins/api-key/routes/delete-api-key.ts` (+17 -1) 📝 `packages/better-auth/src/plugins/api-key/routes/index.ts` (+29 -0) 📝 `packages/better-auth/src/plugins/api-key/routes/update-api-key.ts` (+25 -1) 📝 `packages/better-auth/src/plugins/api-key/types.ts` (+52 -0) </details> ### 📄 Description ## Description Adds lifecycle hooks (create, update, delete) for the API Key plugin. Closes #5694 ## Changes - Added before/after hooks for create, update, and delete operations - Added tests for all hook scenarios - Updated documentation with hook examples <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds before/after lifecycle hooks to the API Key plugin for create, update, and delete so you can validate, modify, or block these actions. Also updates docs, tests, and adds a new error code for hook failures. - **New Features** - Hooks: before can return false to cancel, or { data } to modify; hooks receive parsed metadata/permissions and values are serialized automatically; after runs with the created/updated/deleted record. - Errors: adds BEFORE_HOOK_FAILED; returns BAD_REQUEST when a before hook cancels create, update, or delete. - Docs and tests: extensive examples and coverage for all hook paths. <sup>Written for commit 4c02248a63ccf9ebca76adc25bf5f14a8fcd8da8. 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-04-17 22:39:35 -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#31778