Skip to content

OAuth2 machine credential using JavaScript

// qlik-api-m2m-oauth-example.js 
//
// To use this example, create an OAuth2 web configuration in Qlik Cloud
// and set the Allow Machine-to-Machine option.
//
// Copy the clientId and clientSecret to a secure location,
// then set the consent to trusted.
//
// This example uses qlik/api package.
// More information available at https://qlik.dev/toolkits/qlik-api/
// This example is written for Node.js using common.js for module use.

/*PARAMS
* host: the hostname of your tenant
* clientId: the clientId of the OAuth2 client you created
* clientSecret: the client secret for the OAuth2 client you created
*/

import { auth, users } from "@qlik/api";

// Configuration settings retrieved from .env file
const host = process.env.host; // "<tenant.region.qlikcloud.com>";
const clientId = process.env.clientId; // "<OAUTH_CLIENT_ID>";
const clientSecret = process.env.clientSecret; // "<OAUTH_CLIENT_SECRET>";

const hostConfig = {
    host: host,
    authType: "oauth2",
    clientId: clientId,
    clientSecret: clientSecret
};

auth.setDefaultHostConfig(hostConfig);

(async () => {
    try {
        let me = await users.getMyUser();
        console.log(me.data);
    } catch (e) {
        console.error(e);
    }
})();

Read the tutorial

Was this page helpful?