[PR #2483] [CLOSED] feat(stripe): add support for metered billing #4338

Closed
opened 2026-03-13 11:42:19 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/2483
Author: @kimonneuhoff
Created: 4/30/2025
Status: Closed

Base: v1.3Head: stripe-metered-billing


📝 Commits (3)

📊 Changes

4 files changed (+448 additions, -7 deletions)

View changed files

📝 packages/stripe/src/hooks.ts (+78 -2)
📝 packages/stripe/src/index.ts (+298 -5)
📝 packages/stripe/src/schema.ts (+32 -0)
📝 packages/stripe/src/types.ts (+40 -0)

📄 Description

Hi,

I've developed a feature enabling Stripe's metered billing (usage-based billing) within the Better Auth platform. This feature allows for the creation of usage alerts, such as sending an email when a user reaches their predefined limit (one alert per customer per meter), and supports adding usage data via Stripe. It supports all aggregation formulas.

For reference, please see the relevant Stripe documentation:

How to Use:

  1. Recording New Usage:

    const { data } = await authClient.subscription.usage({
      value: 20,
      referenceId: $activeOrganization?.data?.id,
    });
    
  2. Creating an Alert:
    (Note: Limited to one alert per customer per meter)

    const { data } = await authClient.subscription.alert({
      title: "Test Alert",
      threshold: 20,
      referenceId: $activeOrganization?.data?.id,
    });
    
  3. Configuration:

    stripe({
      subscription: {
        plans: [
          {
            name: "basic",
            priceId: "price_1RJJ82IxHNmbAzfl7fVDuY7a",
            metered: { // Add this
              meterId: "mtr_test_61SSh4j4EGb7KaQzb41IxHNmbAzflTMW",
              eventName: "api_calls",
            },
          },
        ],
        onAlertTriggered: async ({ event, subscription, user, alertData }) => {
          // Send an email or notification
          // Interesting values: currentUsage: subscription.meteredUsage, alertThreshold: subscription.meteredAlertThreshold
        },
      },
    });
    

This is my first contribution to Better Auth, so if anything is wrong or missing, feel free to tell me and i'll change it :) Thanks


🔄 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/2483 **Author:** [@kimonneuhoff](https://github.com/kimonneuhoff) **Created:** 4/30/2025 **Status:** ❌ Closed **Base:** `v1.3` ← **Head:** `stripe-metered-billing` --- ### 📝 Commits (3) - [`d2b3d56`](https://github.com/better-auth/better-auth/commit/d2b3d56055ab6ff86dc5665c58fe5f5edcea7e98) feat: added support for metered billing - [`5d57430`](https://github.com/better-auth/better-auth/commit/5d57430a9c9daddde02a963305358f6bd6f13f7e) fix: fixing merge errors - [`20041ab`](https://github.com/better-auth/better-auth/commit/20041ab778713478da1e3df433013b06ab0f3437) fix: lint ### 📊 Changes **4 files changed** (+448 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `packages/stripe/src/hooks.ts` (+78 -2) 📝 `packages/stripe/src/index.ts` (+298 -5) 📝 `packages/stripe/src/schema.ts` (+32 -0) 📝 `packages/stripe/src/types.ts` (+40 -0) </details> ### 📄 Description Hi, I've developed a feature enabling Stripe's metered billing (usage-based billing) within the Better Auth platform. This feature allows for the creation of usage alerts, such as sending an email when a user reaches their predefined limit (one alert per customer per meter), and supports adding usage data via Stripe. It supports all aggregation formulas. For reference, please see the relevant Stripe documentation: - [Stripe Metered Billing API](https://docs.stripe.com/api/billing/meter) - [Stripe Usage-Based Subscriptions](https://docs.stripe.com/billing/subscriptions/usage-based) **How to Use:** 1. **Recording New Usage:** ```javascript const { data } = await authClient.subscription.usage({ value: 20, referenceId: $activeOrganization?.data?.id, }); ``` 2. **Creating an Alert:** (Note: Limited to one alert per customer per meter) ```javascript const { data } = await authClient.subscription.alert({ title: "Test Alert", threshold: 20, referenceId: $activeOrganization?.data?.id, }); ``` 3. **Configuration:** ```javascript stripe({ subscription: { plans: [ { name: "basic", priceId: "price_1RJJ82IxHNmbAzfl7fVDuY7a", metered: { // Add this meterId: "mtr_test_61SSh4j4EGb7KaQzb41IxHNmbAzflTMW", eventName: "api_calls", }, }, ], onAlertTriggered: async ({ event, subscription, user, alertData }) => { // Send an email or notification // Interesting values: currentUsage: subscription.meteredUsage, alertThreshold: subscription.meteredAlertThreshold }, }, }); ``` This is my first contribution to Better Auth, so if anything is wrong or missing, feel free to tell me and i'll change it :) Thanks --- <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-03-13 11:42:19 -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#4338