use the api key plugin in an existing project with different auth system #1498

Closed
opened 2026-03-13 08:43:27 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @ugolaseemore on GitHub (Jul 14, 2025).

We have an app with users management already utilising an existing auth mechanism.

I'm exploring the possibility of using betterAuth API Keys plugin, to manage the api keys created by our users.

I manage to create an api key without an existing betterAuth session, but getting users' keys, updating a key and deleting a key throws UNAUTHORIZED API exception, I assume it expects an existing session betterAuth to succeed?

Is there a way around this to manage the keys from the app (similar to like its possible to create the key initially).

Thank you!

Originally created by @ugolaseemore on GitHub (Jul 14, 2025). We have an app with users management already utilising an existing auth mechanism. I'm exploring the possibility of using betterAuth API Keys plugin, to manage the api keys created by our users. I manage to create an api key without an existing betterAuth session, but getting users' keys, updating a key and deleting a key throws UNAUTHORIZED API exception, I assume it expects an existing session betterAuth to succeed? Is there a way around this to manage the keys from the app (similar to like its possible to create the key initially). Thank you!
GiteaMirror added the plugin label 2026-03-13 08:43:27 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 14, 2025):

Hi @ugolaseemore! I'm Dosu and I’m helping the better-auth team.

You’re correct: listing, updating, or deleting API keys with the betterAuth API Keys plugin requires a valid betterAuth session, which is why you’re seeing UNAUTHORIZED errors. However, the plugin supports a session mocking feature—if you include a valid API key in the request header (default is x-api-key), the plugin will automatically create a mock session for the user associated with that key. This allows API key management endpoints to work without a traditional betterAuth session, as long as the API key is valid and not expired or usage-limited [docs].

To use this, make sure your requests to list, update, or delete keys include the user’s API key in the header. You can customize the header name with the apiKeyHeaders option or provide a custom getter if needed. The session mocking feature is enabled by default (unless you set disableSessionForAPIKeys: true), but double-check your plugin config.

Be aware: this gives the API key nearly all the power of a session, so restrict key permissions, scope, and lifespan as needed for security [plugin code].

There’s no built-in way to bridge your external auth system directly to betterAuth sessions for these endpoints. If you want to manage keys purely with your own auth, you’d need to fork and patch the plugin to bypass session checks, which isn’t recommended for most setups.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

@dosubot[bot] commented on GitHub (Jul 14, 2025): <!-- Greeting --> Hi @ugolaseemore! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> You’re correct: listing, updating, or deleting API keys with the betterAuth API Keys plugin requires a valid betterAuth session, which is why you’re seeing UNAUTHORIZED errors. However, the plugin supports a session mocking feature—if you include a valid API key in the request header (default is `x-api-key`), the plugin will automatically create a mock session for the user associated with that key. This allows API key management endpoints to work without a traditional betterAuth session, as long as the API key is valid and not expired or usage-limited [[docs](https://github.com/better-auth/better-auth/blob/f569ff140230d58355b1f10a2133202902ab4e74/docs/content/docs/plugins/api-key.mdx)]. To use this, make sure your requests to list, update, or delete keys include the user’s API key in the header. You can customize the header name with the `apiKeyHeaders` option or provide a custom getter if needed. The session mocking feature is enabled by default (unless you set `disableSessionForAPIKeys: true`), but double-check your plugin config. Be aware: this gives the API key nearly all the power of a session, so restrict key permissions, scope, and lifespan as needed for security [[plugin code](https://github.com/better-auth/better-auth/blob/f569ff140230d58355b1f10a2133202902ab4e74/packages/better-auth/src/plugins/api-key/index.ts)]. There’s no built-in way to bridge your external auth system directly to betterAuth sessions for these endpoints. If you want to manage keys purely with your own auth, you’d need to fork and patch the plugin to bypass session checks, which isn’t recommended for most setups. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/ce10b841-f3cf-42f1-866e-cba770235de0?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/ce10b841-f3cf-42f1-866e-cba770235de0?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/ce10b841-f3cf-42f1-866e-cba770235de0?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/ce10b841-f3cf-42f1-866e-cba770235de0?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/ce10b841-f3cf-42f1-866e-cba770235de0?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/ce10b841-f3cf-42f1-866e-cba770235de0?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/ce10b841-f3cf-42f1-866e-cba770235de0?feedback_type=other)</sup>&nbsp;&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3380)
Author
Owner

@ugolaseemore commented on GitHub (Jul 14, 2025):

thank you @dosu, so basically the owner of the key can revoke/delete/update is only using the key itself?

I think my use-case connects with this issue, there might be a case where the key is compromised, or the owner of the key leaves the org, and his key need to revoked/deleted.

there is no way to support this currently, correct?

@ugolaseemore commented on GitHub (Jul 14, 2025): thank you @dosu, so basically the owner of the key can revoke/delete/update is only using the key itself? I think my use-case connects with [this issue](https://github.com/better-auth/better-auth/issues/2134), there might be a case where the key is compromised, or the owner of the key leaves the org, and his key need to revoked/deleted. there is no way to support this currently, correct?
Author
Owner

@dosubot[bot] commented on GitHub (Jul 14, 2025):

That's correct—right now, only the owner of an API key can revoke, delete, or update it, either by having a valid session or by presenting the key itself. There’s no built-in way for an admin or another user to manage (list, update, delete) someone else’s API keys through the better-auth API Keys plugin. This limitation is tracked in issue #2134, and as of now, there’s no supported workaround besides direct database changes or forking/extending the plugin, which is generally discouraged for most setups. If admin-level management is important for your use case, keep an eye on that issue for future updates.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

@dosubot[bot] commented on GitHub (Jul 14, 2025): <!-- Answer --> That's correct—right now, only the owner of an API key can revoke, delete, or update it, either by having a valid session or by presenting the key itself. There’s no built-in way for an admin or another user to manage (list, update, delete) someone else’s API keys through the better-auth API Keys plugin. This limitation is tracked in [issue #2134](https://github.com/better-auth/better-auth/issues/2134), and as of now, there’s no supported workaround besides direct database changes or forking/extending the plugin, which is generally discouraged for most setups. If admin-level management is important for your use case, keep an eye on that issue for future updates. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/62eeb58a-6dac-4242-8bf4-451e217a90d0?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/62eeb58a-6dac-4242-8bf4-451e217a90d0?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/62eeb58a-6dac-4242-8bf4-451e217a90d0?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/62eeb58a-6dac-4242-8bf4-451e217a90d0?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/62eeb58a-6dac-4242-8bf4-451e217a90d0?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/62eeb58a-6dac-4242-8bf4-451e217a90d0?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/62eeb58a-6dac-4242-8bf4-451e217a90d0?feedback_type=other)</sup>&nbsp;&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3380)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1498