Skip to main content

Overview

The Better Auth Adapter for Dodo Payments provides:
  • Automatic customer creation on sign-up
  • Checkout Sessions (preferred) with product slug mapping
  • Self-service customer portal
  • Metered usage ingestion and reporting endpoints for usage-based billing
  • Real-time webhook event processing with signature verification
  • Full TypeScript support
You need a Dodo Payments account and API keys to use this integration.

Prerequisites

  • Node.js 20+
  • Access to your Dodo Payments Dashboard
  • Existing project using better-auth

Installation

1

Install dependencies

Run the following command in your project root:
All required packages are now installed.

Setup

1

Configure environment variables

Add these to your .env file:
Never commit API keys or secrets to version control.
2

Set up server-side integration

Create or update src/lib/auth.ts:
Set environment to live_mode for production.
3

Set up client-side integration

Create or update src/lib/auth-client.ts:

Usage Examples

Prefer authClient.dodopayments.checkoutSession for new integrations. The legacy checkout method is deprecated and kept only for backward compatibility.

Creating a Checkout Session (Preferred)

Unlike the legacy checkout method, checkoutSession does not require billing information upfront as it will be taken from the user at the checkout page. You can still override it by passing the billing key in the argument.
Similar to the legacy checkout method, checkoutSession takes customer’s email and name from their better-auth session, however, you can override it by passing the customer object with email and name.
You can pass the same options in the argument as the request body for Create Checkout Session endpoint.
The return URL is taken from the successUrl configured in the server plugin. You do not need to include return_url in the client payload.

Legacy Checkout (Deprecated)

The authClient.dodopayments.checkout method is deprecated. Use checkoutSession instead for new implementations.

Accessing the Customer Portal

Listing Customer Data

Tracking Metered Usage

Enable the usage() plugin on the server to capture metered events and let customers inspect their usage-based billing.
  • authClient.dodopayments.usage.ingest ingests events for the signed-in, email-verified user.
  • authClient.dodopayments.usage.meters.list lists recent usage for the meters tied to that customer’s subscriptions.
Timestamps older than one hour or more than five minutes into the future are rejected when ingesting usage.
If you omit meter_id when listing usage meters, all meters tied to the customer’s subscriptions are returned.

Webhooks

The webhooks plugin processes real-time payment events from Dodo Payments with secure signature verification. The default endpoint is /api/auth/dodopayments/webhooks.
1

Generate and set webhook secret

Generate a webhook secret for your endpoint URL (e.g., https://<your-domain>/api/auth/dodopayments/webhooks) in the Dodo Payments Dashboard and set it in your .env file:
2

Handle webhook events

Example handler:

Supported Webhook Event Handlers

Configuration Reference

  • client (required): DodoPayments client instance
  • createCustomerOnSignUp (optional): Auto-create customers on user signup
  • use (required): Array of plugins to enable (checkout, portal, usage, webhooks)
  • getCustomerParams (optional): Function that receives the BetterAuth User and returns extra fields to attach to the DodoPayments customer on creation and update (e.g. metadata, phone_number)
  • products: Array of products or async function returning products - successUrl: URL to redirect after successful payment - authenticatedUsersOnly: Require user authentication (default: false)

Troubleshooting & Tips

  • Invalid API key: Double-check your DODO_PAYMENTS_API_KEY in .env. - Webhook signature mismatch: Ensure the webhook secret matches the one set in the Dodo Payments Dashboard. - Customer not created: Confirm createCustomerOnSignUp is set to true.
  • Use environment variables for all secrets and keys. - Test in test_mode before switching to live_mode. - Log webhook events for debugging and auditing.

Prompt for LLMs

Last modified on May 18, 2026