Migrating from Clerk to Better Auth
In this guide, we'll walk through the steps to migrate a project from Clerk to Better Auth — including email/password with proper hashing, social/external accounts, phone number, two-factor data, and more.
This migration will invalidate all active sessions. This guide doesn't currently show you how to migrate Organization but it should be possible with additional steps and the Organization Plugin.
Before You Begin
Before starting the migration process, set up Better Auth in your project. Follow the installation guide to get started. And go to
Connect to your database
You'll need to connect to your database to migrate the users and accounts. You can use any database you want, but for this example, we'll use PostgreSQL.
And then you can use the following code to connect to your database.
Enable Email and Password (Optional)
Enable the email and password in your auth config and implement your own logic for sending verification emails, reset password emails, etc.
See Email and Password for more configuration options.
Setup Social Providers (Optional)
Add social providers you have enabled in your Clerk project in your auth config.
Add Plugins (Optional)
You can add the following plugins to your auth config based on your needs.
Admin Plugin will allow you to manage users, user impersonations and app level roles and permissions.
Two Factor Plugin will allow you to add two-factor authentication to your application.
Phone Number Plugin will allow you to add phone number authentication to your application.
Username Plugin will allow you to add username authentication to your application.
Generate Schema
If you're using a custom database adapter, generate the schema:
or if you're using the default adapter, you can use the following command:
Export Clerk Users
Go to the Clerk dashboard and export the users. Check how to do it here. It will download a CSV file with the users data. You need to save it as exported_users.csv
and put it in the root of your project.
Create the migration script
Create a new file called migrate-clerk.ts
in the scripts
folder and add the following code:
Make sure to replace the process.env.CLERK_SECRET_KEY
with your own Clerk secret key. Feel free to customize the script to your needs.
Run the migration
Run the migration:
Make sure to:
- Test the migration in a development environment first
- Monitor the migration process for any errors
- Verify the migrated data in Better Auth before proceeding
- Keep Clerk installed and configured until the migration is complete
Verify the migration
After running the migration, verify that all users have been properly migrated by checking the database.
Update your components
Now that the data is migrated, you can start updating your components to use Better Auth. Here's an example for the sign-in component:
Remove Clerk Dependencies
Once you've verified that everything is working correctly with Better Auth, you can remove Clerk:
Additional Resources
Goodbye Clerk, Hello Better Auth – Full Migration Guide!
Wrapping Up
Congratulations! You've successfully migrated from Clerk to Better Auth.
Better Auth offers greater flexibility and more features—be sure to explore the documentation to unlock its full potential.