Skip to main content

Overview

Welcome to the integration guide for Creem and Better Auth! This integration enables you to combine powerful authentication capabilities with seamless payment processing and subscription management. Better Auth is a modern authentication framework for TypeScript that provides comprehensive user management, session handling, and authentication flows. By integrating Better Auth with Creem, you can:
  • Automatically synchronize customer and subscription data with your users
  • Grant or revoke access based on subscription status
  • Manage subscriptions directly through your authentication layer
  • Handle payments and billing for authenticated users
  • Prevent trial abuse across multiple subscriptions
How to integrate Better Auth with Creem to build a complete authentication and payment solution for your SaaS application.

Requirements

  • A Creem account
  • Your Creem API keys
  • A TypeScript/JavaScript application
  • A database (PostgreSQL, MySQL, or SQLite)

Installation

1

Install the plugin

Install the Better Auth Creem plugin in your project:
If you’re using a separate client and server setup, make sure to install the plugin in both parts of your project.
2

Get your Creem API Key

  1. Navigate to the Creem dashboard
  2. Click on the “Developers” menu
  3. Copy your API key
  4. Add it to your environment variables:
Test Mode and Production have different API keys. Make sure you’re using the correct one for your environment.

Configuration

Server Configuration

Configure Better Auth with the Creem plugin:

Client Configuration

Database Migration

Generate and run the database schema for subscription persistence:

Webhook Setup

1

Create Webhook in Creem Dashboard

  1. Go to your Creem dashboard
  2. Click on the “Developers” tab
  3. Navigate to the “Webhooks” section
  4. Click “Add Webhook”
  5. Enter your webhook URL:
The /api/auth prefix is the default Better Auth server path. Adjust if you’ve customized your Better Auth configuration.
2

Configure Webhook Secret

  1. Copy the webhook signing secret from Creem
  2. Add it to your environment variables:
  1. Update your server configuration to include the webhook secret (shown in Configuration section above)
3

Local Development (Optional)

For local testing, use ngrok to expose your local server:
Then add the ngrok URL to your Creem webhook settings.

Usage Examples

Create Checkout Session

Allow users to subscribe to your products:

Customer Portal

Let users manage their subscriptions:

Check Subscription Access

Verify if a user has an active subscription:

Cancel Subscription

Allow users to cancel their subscription:

Access Control with Webhooks

The plugin provides high-level handlers to manage user access automatically:

Server-Side Usage

Use Creem functions directly in Server Components or API routes:

Key Features

Automatic Trial Abuse Prevention

When using database mode, the plugin automatically prevents users from abusing trial periods. Each user can only receive one trial across all subscription plans.

Database Persistence

Store subscription data in your database for fast access checks without API calls. This enables:
  • Offline access to subscription data
  • SQL queries for subscription management
  • Automatic synchronization via webhooks

Transaction History

Search and filter transaction records for authenticated users:

Best Practices

  • Always test in development - Use test mode and a development environment before going live
  • Implement error handling - Handle payment failures and webhook errors gracefully
  • Monitor webhooks - Set up logging and alerts for webhook processing
  • Use database mode - Enable persistSubscriptions for better performance and features
  • Protect sensitive routes - Use middleware or server-side checks to protect premium content
  • Validate subscriptions - Always verify subscription status before granting access to premium features

Additional Resources

Support

Need help with the integration?