How to migrate Netlify GoTrue Users To Appwrite
Tags: javascript, webdev, tutorial, database
Published: 2022-09-22 09:18:58

With Appwrite 1.0, we are excited to announce that you can import users from different platforms to Appwrite. One of these platforms is Netlify GoTrue. In this tutorial, 
we will show you how to export users from a GoTrue based platform and import them into Appwrite.

First, you might be wondering what exactly Appwrite is. [Appwrite](https://appwrite.io/) is an open-source Backend-as-a-Service (BaaS) packaged as a set of Docker micro-services to give developers of any background the tools necessary to build modern apps quickly and securely.

For this tutorial, you will need [NodeJS](https://nodejs.org) installed on your machine, as we will be using it to export your GoTrue users and import them into Appwrite. We will also provide a script at the end of this tutorial that you can copy and paste to get started.

TL;DR - [GitHub to completed project](https://github.com/PineappleIOnic/gotrue-to-appwrite)

**Warning: Users with passwords that are less than six characters will not work with Appwrite; we suggest sending an email to your users to reset their password in this case.**

Now then, let's move on to our first stage.

## Exporting Users from GoTrue

To export users from GoTrue, you need to connect to your Postgres database and dump the `auth.users` table. This can be done using the `node-postgres` node module.

### Using the node-postgres module

First, create a project folder and install the `node-postgres` module using npm.

```bash
npm install pg
```

Next, create a file called `main.js` and add the following code.

```js
const { Client } = require('pg')

// Connect to your Postgres database
const client = new Client({
    user: 'postgres',
    host: 'postgres_url',
    database: 'postgres',
    password: 'yourpasword',
    port: 5432,
  })

client.connect()

// Dump the auth.users table
client.query('SELECT * FROM auth.users', (err, res) => {
    if (err) {
        console.log(err.stack)
        client.end()
    } else {
        console.log('Recieved ' + res.rowCount + ' users')
        client.end()
        return res.rows
    }
})
```

This code will connect to your GoTrue database and dump the `auth.users` table. You can then use the `res.rows` variable to access the users. In the next section, we will add this code to import the users into Appwrite.

## Importing Users to Appwrite

Now that we have exported the users from GoTrue, we can import them into Appwrite. To do this, we will be using the Appwrite Server SDK for NodeJS.

First, we are going to install the Appwrite SDK using NPM.

```bash
npm install node-appwrite
```

Once done, we will extend the code we wrote in the previous section to import the users into Appwrite.

Add the following at the top of the file, making sure to update the variables to match your Appwrite instance:

```js
const SDK = require('node-appwrite')

const appwriteClient = new sdk.Client()

appwriteClient
    .setEndpoint('http://localhost/v1') // Your API Endpoint
    .setProject('projectKey') // Your project ID
    .setKey('Server API Key') // Your secret API key
    .setSelfSigned() // Use only on dev mode with a self-signed SSL cert
;
```

Then overwrite the code in the `client.query` callback with the following:

```js
console.log('Received ' + res.rowCount + ' users')

for (const user of res.rows) { 
            console.log('Importing user' + user.email)

            await users.createBcryptUser(
                user.id,
                user.email,
                user.encrypted_password
            )
  }

console.log('Successfully Imported ' + res.rowCount + ' users')
client.end()
return res.rows
```

This code will loop through each user and create a new user in Appwrite using the `users.createBcryptUser` method. You can find more information about this method in the [Appwrite API Reference](https://appwrite.io/docs/server/users?sdk=nodejs-default#usersCreateBcryptUser).

With all that done, you can run the script using `node main.js`, and your users will be imported into Appwrite. Make sure you have a test account in GoTrue to validate that the import was successful.

## Conclusions

This tutorial showed you how to export users from a GoTrue based platform and import them into Appwrite. We also provided a script that you can copy and paste to get started. If you have questions or comments, feel free to join our [Discord](https://appwrite.io/discord) server.

A complete version of this project can be found in the GitHub repository [here](https://github.com/PineappleIOnic/gotrue-to-appwrite).

# More about Appwrite

Appwrite is an open-source Backend-as-a-Service (BaaS), packaged as a set of Docker micro-services to give developers of any background the tools necessary to build modern apps quickly and securely.

Chat with us on Discord, or, Learn more about Appwrite:
Check out Appwrite as the backend for your next web, Flutter, or server-side application. Here are some handy links for more information:
• [Appwrite Contribution Guide](https://github.com/appwrite/appwrite/blob/master/CONTRIBUTING.md)
• [Appwrite Discord](https://discord.com/invite/appwrite)
• [Appwrite Github](https://github.com/appwrite)
• [Appwrite Documentation](https://appwrite.io/docs)