> ## Documentation Index
> Fetch the complete documentation index at: https://docs.equals.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Auth0

> Example Javascript code for importing data from Auth0

You can use Import Scripts to import data from your Auth0 account directly into an Equals workbook.

# Create an Auth0 personal access token

Follow [the instructions here](https://auth0.com/docs/secure/tokens/access-tokens/management-api-access-tokens) to create an Auth0 personal access token. You'll also want to write down your Client ID.

# Add the key to a secret group

Copy the access token from the prior step. Then [create a new secret group](https://docs.equals.com/docs/secret-groups) and paste the key into a new secret named `auth0_api_secret`. Create a second secret in the same group called `auth0_api_client_id` with your Client ID.

# The code

Once you've completed the prior steps, create a new workbook, add a new import script to a sheet and copy + paste the script below. Once pasted select your secret group in the toolbar (make sure the key in the secret group is called `auth0_api_secret`).

```
/*
 - https://auth0.com/docs/api/management/v2
 - https://auth0.com/docs/api/management/v2#!/Users/get_users
*/

const equals = require("equals");
const axios = require("axios");

/*
 Page through the users and add ones that have not yet been seen to the spreadsheet
 Avoid PII
*/
const processAuth0UsersData = async () => {
    const accessToken = await getAccessToken();

    const headers = {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${accessToken}`,
    };

    const perPage = 100;

    // set next page call as true so we make the initial call
    let makeNextPageCall = true;

    // set the initial page to be 0
    let page = 0;

    equals.addHeaders(["user_id", "created_at", "updated_at", "last_login", "logins_count", "subscription_purchased", "user_metadata"]);

    while (makeNextPageCall) {
        // start with make next page call as false and only set to true if we need to make more
        makeNextPageCall = false;

        //console.log(page);
      
        await axios
            .get(
                equals.getSecret("auth0_api_host")&`/api/v2/users?page=${page}&per_page=${perPage}`,
                { headers }
            )
            .then(response => {
                if (response.data.length > 0) {
                  /*
                    Add to spreadsheet
                  */
                  let users = response.data;
                  for(const user of users) {
                    //console.log(user);

                    equals.addRow([
                      user.user_id,
                      formatDate(user.created_at),
                      formatDate(user.updated_at),
                      formatDate(user.last_login),
                      user.logins_count
                    ]);
                  }
                  
                }

                // we got all we can for this page, so make another page call
                // Limit is 1000 users: https://auth0.com/docs/manage-users/user-search/view-search-results-by-page#limitation
                if (response.data.length >= perPage && page < 10) {
                    makeNextPageCall = true;
                    page += 1;
                }
            })
            .catch(error => {
                console.log(`error getting users: ${error}`);
            });
    }
};

const getAccessToken = async () => {
    const timestamp = new Date().getTime();

    let accessToken = "";

        const oauthTokenHeaders = {
            'Content-Type': 'application/x-www-form-urlencoded',
        };

        let clientId = equals.getSecret("auth0_api_client_id")
        let clientSecret = equals.getSecret("auth0_api_secret")
        let grantType = 'client_credentials'
        let audience = equals.getSecret("auth0_api_audience")

        const data = `client_id=${clientId}&client_secret=${clientSecret}&grant_type=${grantType}&audience=${audience}`;

        await axios
            .post(equals.getSecret("auth0_api_oauth_url"), data, {
                headers: oauthTokenHeaders,
            })
            .then(response => {
                accessToken = response.data.access_token;
            })
            .catch(error => {
                console.error(error);
            });

    return accessToken;
};

const formatDate = (gaDate) => {
  if (!gaDate) {
    return ""
  }
  //console.log(gaDate)
  const year = gaDate.slice(0,4);
  const month = gaDate.slice(5,7);
  const day = gaDate.slice(8,10);
  //console.log(`Boom = ${year}-${month}-${day}`)
  return `${year}-${month}-${day}`
}

await processAuth0UsersData();
```

***

[Airtable](/docs/airtable)

[Deputy](/docs/deputy)
