Skip to content

feat: Add API token support via @better-auth/api-key#973

Closed
Rhonstin wants to merge 2 commits into
nicotsx:mainfrom
Rhonstin:feat/api-tokens
Closed

feat: Add API token support via @better-auth/api-key#973
Rhonstin wants to merge 2 commits into
nicotsx:mainfrom
Rhonstin:feat/api-tokens

Conversation

@Rhonstin

Copy link
Copy Markdown

Summary

  • Add @better-auth/api-key plugin for programmatic API access
  • Create API Tokens settings page with full CRUD UI
  • Enable session mocking for seamless integration with existing auth middleware

Changes

Backend

  • app/server/lib/auth.ts: Added apiKey() plugin configuration
    • User-scoped keys with zb_ prefix
    • enableSessionForAPIKeys: true for automatic session creation

Frontend

  • app/client/lib/auth-client.ts: Added apiKeyClient() plugin
  • app/client/modules/settings/components/api-tokens-section.tsx: New UI component
  • app/client/modules/settings/routes/settings.tsx: Added "API Tokens" tab

Usage

Create Token

curl -X POST /api/auth/api-key/create \
  -H "Content-Type: application/json" \
  -b cookies.txt \
  -d '{"name":"mcp-server"}'

Authenticate

curl -H "x-api-key: zb_..." /api/v1/volumes

Features

  • Create tokens via Settings → API Tokens
  • 90-day default expiration
  • Enable/disable and revoke tokens
  • Compatible with existing requireAuth middleware

- Add @better-auth/api-key dependency
- Configure apiKey plugin with user-scoped keys and zb_ prefix
- Add apiKeyClient to auth-client for frontend integration
- Create API Tokens settings page with CRUD UI
- Enable session mocking for API keys (enableSessionForAPIKeys)

API keys support:
- Create tokens via Settings > API Tokens
- Authenticate via x-api-key header
- 90-day default expiration
- Enable/disable and revoke tokens
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 596691bf-60f2-4553-bdcc-b2e9ab793100

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@socket-security

socket-security Bot commented Jun 12, 2026

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​better-auth/​api-key@​1.6.14771007897100

View full report

- Add apikey table schema for @better-auth/api-key plugin
- Update requireAuth middleware to fallback to user's default org
  when activeOrganizationId is missing (API key mock sessions)
- Pass full schema to drizzleAdapter for apikey model resolution
@nicotsx

nicotsx commented Jun 13, 2026

Copy link
Copy Markdown
Owner

Hello @Rhonstin thanks for putting this together, unfortunately we already had two PRs lined up for this exact feature. I will close this one as the feature has already landed in main. Next time if you could discuss the implementation in the issue before jumping on implementation it will be easier to align

@nicotsx nicotsx closed this Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants