# Migrate from Firebase

Migrating your user base from Firebase to Clerk is a 2-part process that can be accomplished with just a few steps.

1. ## Export your Firebase users
2. ### Install the Firebase CLI

   If you haven't already, install the Firebase CLI:

   filename: terminal

   ```bash
   npm install -g firebase-tools
   ```
3. ### Log in to Firebase

   Login to your Firebase account via the CLI.

   filename: terminal

   ```bash
   firebase login
   ```
4. ### Find your Firebase project ID

   Use the firebase CLI to list your projects and find the project ID you want to export.

   filename: terminal

   ```bash
   firebase projects:list
   ```
5. ### Export your Firebase users using the CLI

   Use the firebase CLI to export your users to a JSON file.

   filename: terminal

   ```bash
   firebase auth:export firebase-users.json --format=json --project <YOUR_PROJECT_ID>
   ```

   Here's what this command is doing:

   1. `firebase auth:export`: it tells Firebase you're trying to export the user base.

   2. `firebase-users.json`: the name of the file you're about to create.

   3. `--format=json`: make it a JSON file.

   4. `--project <YOUR_PROJECT_ID>`: it tells Firebase which project you're trying to export from. Don't forget to replace `<YOUR_PROJECT_ID>` with the Project ID you found in the previous step.

   You should now have a JSON file called `firebase-users.json` that contains all your Firebase users.
6. ## Retrieve your password hash parameters

   In your Firebase project’s dashboard, navigate to **Authentication** and select the 3 vertical dots at the top of the user's list, then select **Password hash parameters**.

   On the new window that opens, you'll find the following values: `base64_signer_key`, `base64_salt_separator`, `rounds` and `mem_cost`.

   You can find more information about this page and the values above on [Firebase's documentation](https://firebaseopensource.com/projects/firebase/scrypt/).
7. ## Create a Node.js script

   Now you have all the information you need to import your Firebase users into Clerk. This example uses Node.js, but you can use any other language or method if you so wish.
8. ### init npm

   filename: terminal

   ```bash
   npm init -y
   ```
9. ### Install dependencies

   filename: terminal

   ```bash
   npm i node-fetch@2
   ```
10. ### Create a script

    filename: migrate-to-clerk.js

    ```js
    const fetch = require('node-fetch')
    const firebaseUsers = require('./firebase-users.json')

    async function migrateToClerk() {
      // You can find these values on your Firebase project's authentication settings
      const signerKey = ''
      const saltSeparator = ''
      const rounds = ''
      const memoryCost = ''

      // You can find this value on your Clerk application's dashboard
      const clerkBackendApiKey = ''

      for (let user of firebaseUsers.users) {
        const { email, localId, passwordHash, salt } = user

        const body = passwordHash
          ? {
              email_address: [email],
              external_id: localId,
              password_hasher: 'scrypt_firebase',
              password_digest: `${passwordHash}$${salt}$${signerKey}$${saltSeparator}$${rounds}$${memoryCost}`,
            }
          : {
              email_address: [email],
              external_id: localId,
              skip_password_requirement: true,
            }

        try {
          const result = await fetch('https://api.clerk.com/v1/users', {
            method: 'POST',
            headers: {
              Authorization: `Bearer ${clerkBackendApiKey}`,
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(body),
          })

          if (!result.ok) {
            throw Error(result.statusText)
          } else {
            console.log(`${email} added successfully`)
          }
        } catch (error) {
          console.error(`Error migrating ${email}: ${error}`)
        }
      }
    }

    migrateToClerk()
    ```

    Here, body will either hold the necessary information to migrate a password-based user or in the case of an OAuth-based user, it'll skip the password check. It will also have the previous user ID as `external_id`, so you can link the newly created users with their existing data.
11. ## Run the script

    filename: terminal

    ```bash
    node migrate-to-clerk.js
    ```

    > This implementation does not take rate limiting into account. If you have a large user base, you may want to implement a retry mechanism. Clerk will return a `429` status code if you exceed the rate limit. You can find more information about Clerk's rate limits [here](https://clerk.com/docs/guides/how-clerk-works/system-limits.md). Keep [Firebase's rate limits](https://firebase.google.com/docs/functions/quotas) in mind as well when exporting your users.

    Once the script has finished running, your user base will be fully migrated to Clerk.

---

## Sitemap

[Overview of all docs pages](https://clerk.com/docs/llms.txt)
