[PR #4871] [MERGED] fix(api-key): correct refill interval time calculation #22530

Closed
opened 2026-04-15 21:06:43 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4871
Author: @Pankaj3112
Created: 9/24/2025
Status: Merged
Merged: 10/2/2025
Merged by: @himself65

Base: canaryHead: fix/apikey-refill-interval-milliseconds


📝 Commits (2)

  • 1b257dd fix(api-key): correct refill interval time calculation
  • cf81d3f test: add

📊 Changes

6 files changed (+186 additions, -6 deletions)

View changed files

📝 packages/better-auth/src/plugins/api-key/api-key.test.ts (+180 -0)
📝 packages/better-auth/src/plugins/api-key/routes/get-api-key.ts (+1 -1)
📝 packages/better-auth/src/plugins/api-key/routes/list-api-keys.ts (+1 -1)
📝 packages/better-auth/src/plugins/api-key/routes/update-api-key.ts (+1 -1)
📝 packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts (+1 -1)
📝 packages/better-auth/src/plugins/api-key/types.ts (+2 -2)

📄 Description

What this PR does

This PR fixes a critical bug in the API key refill mechanism where API key credits weren't being replenished after the specified interval. The bug was caused by a time unit mismatch: the code was calculating elapsed time in days while comparing against refillInterval which is specified in milliseconds.

The Issue

const timeSinceLastRequest = (now - lastTime) / (1000 * 60 * 60 * 24); // in days
if (timeSinceLastRequest > refillInterval) {
    remaining = refillAmount;
    lastRefillAt = new Date();
}

This meant if a user set refillInterval: 5000 (5 seconds), the refill would only occur after 5000 days (~13.7 years).

The Fix

const timeSinceLastRequest = (now - lastTime); // in milliseconds
if (timeSinceLastRequest > refillInterval) {
    remaining = refillAmount;
    lastRefillAt = new Date();
}

Benefits

  • API keys now correctly replenish their request allowance after the specified interval
  • Consistent behavior with documentation which already stated refillInterval was in milliseconds
  • Rate limiting and throttling strategies now work as expected

Documentation Updates

Also updated inconsistent type definitions and comments to ensure all references to refillInterval consistently specify the unit as milliseconds.

Testing

Tested scenarios:

  • API key with refillInterval set to 5 seconds (5000ms)
  • API key with refillInterval set to 1 hour (3600000ms)
  • Multiple API calls depleting and then waiting for refill
  • Concurrent requests during refill periods

Breaking changes

None for users who followed documentation. The fix aligns the implementation with the documented behavior that refillInterval is measured in milliseconds.

Closes #4336


Summary by cubic

Fixes the API key refill logic to use milliseconds, so credits refill at the configured interval. Also clarifies docs and types to consistently state milliseconds.

  • Bug Fixes

    • Compare elapsed time in milliseconds (now - lastTime) to refillInterval.
    • Reset remaining to refillAmount and update lastRefillAt when interval passes.
  • Documentation

    • Update ApiKey types and route descriptions to specify refillInterval in milliseconds with examples.

🔄 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/4871 **Author:** [@Pankaj3112](https://github.com/Pankaj3112) **Created:** 9/24/2025 **Status:** ✅ Merged **Merged:** 10/2/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/apikey-refill-interval-milliseconds` --- ### 📝 Commits (2) - [`1b257dd`](https://github.com/better-auth/better-auth/commit/1b257ddf30b55f8a0c1301e1b9680ff0d9fa5d52) fix(api-key): correct refill interval time calculation - [`cf81d3f`](https://github.com/better-auth/better-auth/commit/cf81d3f16cfe7de6021911fa4eb9486b452e6f91) test: add ### 📊 Changes **6 files changed** (+186 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/api-key/api-key.test.ts` (+180 -0) 📝 `packages/better-auth/src/plugins/api-key/routes/get-api-key.ts` (+1 -1) 📝 `packages/better-auth/src/plugins/api-key/routes/list-api-keys.ts` (+1 -1) 📝 `packages/better-auth/src/plugins/api-key/routes/update-api-key.ts` (+1 -1) 📝 `packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts` (+1 -1) 📝 `packages/better-auth/src/plugins/api-key/types.ts` (+2 -2) </details> ### 📄 Description # What this PR does This PR fixes a critical bug in the API key refill mechanism where API key credits weren't being replenished after the specified interval. The bug was caused by a time unit mismatch: the code was calculating elapsed time in days while comparing against `refillInterval` which is specified in milliseconds. ## The Issue ```typescript const timeSinceLastRequest = (now - lastTime) / (1000 * 60 * 60 * 24); // in days if (timeSinceLastRequest > refillInterval) { remaining = refillAmount; lastRefillAt = new Date(); } ``` This meant if a user set `refillInterval: 5000` (5 seconds), the refill would only occur after 5000 days (~13.7 years). ## The Fix ```typescript const timeSinceLastRequest = (now - lastTime); // in milliseconds if (timeSinceLastRequest > refillInterval) { remaining = refillAmount; lastRefillAt = new Date(); } ``` ## Benefits - API keys now correctly replenish their request allowance after the specified interval - Consistent behavior with documentation which already stated refillInterval was in milliseconds - Rate limiting and throttling strategies now work as expected ## Documentation Updates Also updated inconsistent type definitions and comments to ensure all references to refillInterval consistently specify the unit as milliseconds. ## Testing Tested scenarios: - API key with refillInterval set to 5 seconds (5000ms) - API key with refillInterval set to 1 hour (3600000ms) - Multiple API calls depleting and then waiting for refill - Concurrent requests during refill periods ## Breaking changes None for users who followed documentation. The fix aligns the implementation with the documented behavior that refillInterval is measured in milliseconds. Closes #4336 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes the API key refill logic to use milliseconds, so credits refill at the configured interval. Also clarifies docs and types to consistently state milliseconds. - **Bug Fixes** - Compare elapsed time in milliseconds (now - lastTime) to refillInterval. - Reset remaining to refillAmount and update lastRefillAt when interval passes. - **Documentation** - Update ApiKey types and route descriptions to specify refillInterval in milliseconds with examples. <!-- 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-15 21:06:43 -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#22530