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
What you'll learn
What you'll learn
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
- Navigate to the Creem dashboard
- Click on the “Developers” menu
- Copy your API key
- 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
- Go to your Creem dashboard
- Click on the “Developers” tab
- Navigate to the “Webhooks” section
- Click “Add Webhook”
- 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
- Copy the webhook signing secret from Creem
- Add it to your environment variables:
- Update your server configuration to include the webhook secret (shown in Configuration section above)
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
persistSubscriptionsfor 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?- Join our Discord community for real-time support
- Chat with us directly using the in-app live chat on the Creem dashboard
- Email us at support@creem.io
- Open an issue on GitHub