Better Auth StreamPay Plugin

1.0.0 · active · verified Wed Apr 22

better-auth-streampay is a community-developed plugin designed to seamlessly integrate StreamPay payment and subscription functionalities into applications utilizing the `better-auth` authentication framework. Currently at version 1.0.0, this package provides robust features such as checkout integration with lazy consumer creation, a comprehensive customer portal for managing subscriptions and invoices, and secure handling of StreamPay webhooks with HMAC-SHA256 signature verification. It also allows for optional eager consumer provisioning upon user signup and ensures consumer data synchronization on user updates or deletions. The plugin differentiates itself by offering a tightly coupled solution specifically for StreamPay users within the Better Auth ecosystem, providing distinct sub-plugins for various functionalities like `checkout`, `portal`, `subscriptions`, and `webhooks` for modular integration. While there's no fixed release cadence, being a v1.0.0 community plugin, updates are typically driven by feature development and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the server-side setup of the `better-auth-streampay` plugin, configuring the core `streampay` plugin with its sub-plugins: `checkout`, `portal`, `subscriptions`, and `webhooks`. It initializes the StreamPay SDK client and integrates webhook handlers for payment success and subscription cancellation events.

import { betterAuth } from "better-auth";
import StreamSDK from "@streamsdk/typescript";
import {
  streampay,
  checkout,
  portal,
  subscriptions,
  webhooks,
} from "better-auth-streampay";

// Ensure these environment variables are set in your .env file
// STREAMPAY_API_KEY=your_base64_encoded_api_key:api_secret
// STREAMPAY_WEBHOOK_SECRET=your_webhook_secret (if using webhooks)
// BETTER_AUTH_SECRET=your_session_signing_secret (min 32 chars)
// BETTER_AUTH_URL=your_app_public_url (required in prod)

const streamPayClient = StreamSDK.init(process.env.STREAMPAY_API_KEY ?? '');

const auth = betterAuth({
  plugins: [
    streampay({
      client: streamPayClient,
      // createConsumerOnSignUp defaults to false; set to `true` to provision
      // a StreamPay consumer at signup instead of lazily on first payment.
      use: [
        checkout({
          products: [
            { productId: "aaaa-bbbb-cccc-dddd", slug: "pro" }
          ],
          successUrl: "/dashboard?checkout=success",
          failureUrl: "/dashboard?checkout=failure",
          authenticatedUsersOnly: true,
        }),
        portal(),
        subscriptions(),
        webhooks({
          secret: process.env.STREAMPAY_WEBHOOK_SECRET ?? '',
          onPaymentSucceeded: async (payload) => {
            console.log('Payment Succeeded:', payload);
            // Implement logic to provision user access or update status
          },
          onSubscriptionCanceled: async (payload) => {
            console.log('Subscription Canceled:', payload);
            // Implement logic to revoke user access or update status
          },
        }),
      ],
    }),
  ],
});

console.log('Better Auth with StreamPay plugin initialized successfully.');

view raw JSON →