Payments and Checkouts made dead simple with BetterAuth
Configure Polar Access Token
Configure BetterAuth Server
client
(required): Polar SDK client instancecreateCustomerOnSignUp
(optional): Automatically create a Polar customer when a user signs upgetCustomerCreateParams
(optional): Custom function to provide additional customer creation metadatause
(optional): Array of Polar plugins to enable specific functionality (checkout, portal, usage, and webhooks)Configure BetterAuth Client
createCustomerOnSignUp
Polar plugin configuration option to automatically create a new Polar Customer when a new User is added in the BetterAuth database.
All new customers are created with an associated externalId
, i.e. the ID of your User in the Database. This skips any Polar to User mapping in your database.
checkout
plugin in the use
property.
The checkout plugin accepts the following configuration options:
products
(optional): An array of product mappings or a function that returns them asynchronously. Each mapping contains a productId
and a slug
that allows you to reference products by a friendly slug instead of their full ID.
successUrl
(optional): The relative URL where customers will be redirected after a successful checkout completion. You can use the {CHECKOUT_ID}
placeholder in the URL to include the checkout session ID in the redirect.
authenticatedUsersOnly
(optional): A boolean flag that controls whether checkout sessions require user authentication. When set to true
, only authenticated users can initiate checkouts and the customer information will be automatically associated with the authenticated user. When false
, anonymous checkouts are allowed.
Use Checkout Plugin
use
property of the Polar plugin for BetterAuth client to have the checkout
plugin.Create checkouts using BetterAuth client
checkout
plugin is passed, you are then able to initialize Checkout Sessions using the checkout
method on the BetterAuth client. This will redirect the user to the product’s checkout link.The checkout
method accepts the following properties:products
(optional): An array of Polar Product IDs.slug
(optional): A string that can be used as a reference to the products
defined in the Checkout configreferenceId
(optional): An identifier that will be saved in the metadata of the checkout, order & subscription objectusage
plugin in the use
property.
ingestion
method of the usage
plugin accepts the following parameters:
event
(string): The name of the event to ingest. For example, ai_usage
, video_streamed
or file_uploaded
.
metadata
(object): A record containing key-value pairs that provide additional context about the event. Values can be strings, numbers, or booleans. This is useful for storing information that can be used to filter the events or compute the actual usage. For example, you can store the duration of the video streamed or the size of the file uploaded.
meters
method of the usage
plugin accepts the following parameters:
page
(number): The page number for pagination (starts from 1).
limit
(number): The maximum number of meters to return per page.
meters
method returns the following fields in the response object:
webhooks
plugin can be used to capture incoming events from your Polar organization.
To set up the Polar webhooks
plugin with the BetterAuth client, follow the steps below:
Configure Webhook Endpoints in Polar
Add the Webhook Secret
process.env.POLAR_WEBHOOK_SECRET
):Use Webhooks Plugin in BetterAuth client
webhooks
plugin in the use
property.webhooks
plugin allows you to invoke handlers for all Polar webhook events:
onPayload
- Catch-all handler for any incoming Webhook eventonCheckoutCreated
- Triggered when a checkout is createdonCheckoutUpdated
- Triggered when a checkout is updatedonOrderCreated
- Triggered when an order is createdonOrderPaid
- Triggered when an order is paidonOrderRefunded
- Triggered when an order is refundedonRefundCreated
- Triggered when a refund is createdonRefundUpdated
- Triggered when a refund is updatedonSubscriptionCreated
- Triggered when a subscription is createdonSubscriptionUpdated
- Triggered when a subscription is updatedonSubscriptionActive
- Triggered when a subscription becomes activeonSubscriptionCanceled
- Triggered when a subscription is canceledonSubscriptionRevoked
- Triggered when a subscription is revokedonSubscriptionUncanceled
- Triggered when a subscription cancellation is reversedonProductCreated
- Triggered when a product is createdonProductUpdated
- Triggered when a product is updatedonOrganizationUpdated
- Triggered when an organization is updatedonBenefitCreated
- Triggered when a benefit is createdonBenefitUpdated
- Triggered when a benefit is updatedonBenefitGrantCreated
- Triggered when a benefit grant is createdonBenefitGrantUpdated
- Triggered when a benefit grant is updatedonBenefitGrantRevoked
- Triggered when a benefit grant is revokedonCustomerCreated
- Triggered when a customer is createdonCustomerUpdated
- Triggered when a customer is updatedonCustomerDeleted
- Triggered when a customer is deletedonCustomerStateChanged
- Triggered when a customer is createdportal
plugin gives the BetterAuth Client a set of customer management methods, scoped under authClient.customer
object.
referenceId
you can retrieve all the subscriptions associated with that organization (instead of the user).
To figure out if a user should have access, pass the user’s organization ID to see if there is an active subscription for that organization.