[GH-ISSUE #5694] Add hooks for API keys #18957

Closed
opened 2026-04-15 17:42:03 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @florianmartens on GitHub (Oct 31, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/5694

Is this suited for github?

  • Yes, this is suited for github

API Keys support metadata. But there are no hooks in the config options. As such, metadata becomes more-or-less useless unless we define our own custom endpoints for API key management.

Typical cases for metadata would include things like the organizationId but since we have no hooks available, we can never verify that metadata used upon creation of an API key. Upon creation the user may use any organizationId to create their API key.

On my system, I need to have access to this object before the key is created to potentially reject the request. This is true for all meta-information one could possibly store on an API key.

Describe the solution you'd like

I'm proposing adding hooks for API keys.

Describe alternatives you've considered

None

Additional context

No response

Originally created by @florianmartens on GitHub (Oct 31, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/5694 ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. API Keys support metadata. But there are no hooks in the config options. As such, metadata becomes more-or-less useless unless we define our own custom endpoints for API key management. Typical cases for metadata would include things like the `organizationId` but since we have no hooks available, we can never verify that metadata used upon creation of an API key. Upon creation the user may use any organizationId to create their API key. On my system, I need to have access to this object before the key is created to potentially reject the request. This is true for all meta-information one could possibly store on an API key. ### Describe the solution you'd like I'm proposing adding hooks for API keys. ### Describe alternatives you've considered None ### Additional context _No response_
GiteaMirror added the lockedenhancement labels 2026-04-15 17:42:04 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Oct 31, 2025):

It looks like the API key plugin currently doesn’t offer any pre‐ or post‐creation hooks, so there’s no built‐in way to intercept or validate the metadata (such as an organizationId) before the key is created. The recommended next step is to submit a feature request for adding hooks to the API key plugin so that metadata can be validated in a centralized way during creation. In the meantime, you might consider these workarounds:

  1. Create a custom endpoint that wraps the API key creation logic—this lets you perform any metadata checks before calling the native API key creation function.
  2. If your database adapter supports it, use database hooks to validate the metadata before inserting the key, but note that this may require extra configuration and isn’t as clean as having dedicated API key hooks.

This approach follows similar improvements seen in other parts of the system (for example, hooks in organization management) [1][2]. If you decide to formally request this enhancement, providing clear use cases and potential security impacts will help the maintainers prioritize it.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3471568569 --> @better-auth-agent[bot] commented on GitHub (Oct 31, 2025): It looks like the API key plugin currently doesn’t offer any pre‐ or post‐creation hooks, so there’s no built‐in way to intercept or validate the metadata (such as an organizationId) before the key is created. The recommended next step is to submit a feature request for adding hooks to the API key plugin so that metadata can be validated in a centralized way during creation. In the meantime, you might consider these workarounds: 1. Create a custom endpoint that wraps the API key creation logic—this lets you perform any metadata checks before calling the native API key creation function. 2. If your database adapter supports it, use database hooks to validate the metadata before inserting the key, but note that this may require extra configuration and isn’t as clean as having dedicated API key hooks. This approach follows similar improvements seen in other parts of the system (for example, hooks in organization management) [[1]](https://github.com/better-auth/better-auth/issues/2559)[[2]](https://github.com/better-auth/better-auth/issues/2446). If you decide to formally request this enhancement, providing clear use cases and potential security impacts will help the maintainers prioritize it. <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@ping-maxwell commented on GitHub (Nov 3, 2025):

Use normal Better-Auth hooks! You can read the body from hooks, modify the context, most things are pretty much accessible from there.
There isn't a need for custom hook options when normal better-auth hooks can do this.

<!-- gh-comment-id:3481669975 --> @ping-maxwell commented on GitHub (Nov 3, 2025): Use normal Better-Auth hooks! You can read the body from hooks, modify the context, most things are pretty much accessible from there. There isn't a need for custom hook options when normal better-auth hooks can do this.
Author
Owner

@better-auth-agent[bot] commented on GitHub (Nov 3, 2025):

Hello there! You can achieve metadata validation for API keys using the existing Better Auth hooks. Simply add a hook that intercepts the API key creation process, read and verify the metadata (like organizationId) from the request body, and either modify the context or reject the request as needed. This way, you avoid needing separate hook options for API keys because the standard hooks are flexible enough to handle your requirements [1].

Is there anything specific you'd like help with when implementing this hook? tag @better-auth-agent

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3481671542 --> @better-auth-agent[bot] commented on GitHub (Nov 3, 2025): Hello there! You can achieve metadata validation for API keys using the existing Better Auth hooks. Simply add a hook that intercepts the API key creation process, read and verify the metadata (like organizationId) from the request body, and either modify the context or reject the request as needed. This way, you avoid needing separate hook options for API keys because the standard hooks are flexible enough to handle your requirements [[1]](https://github.com/better-auth/better-auth/issues/2000). Is there anything specific you'd like help with when implementing this hook? tag @better-auth-agent <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#18957