Waiting for engine...
Skip to main content

Altair script

Programmatic access to GraphQL APIs is available through any application, typically through a GraphQL client library in the language of choice. Altair is a user interface tool that you can use to accomplish this. You can use the following script as an option to quickly access GraphQL APIs.

note

Altair supports multiple tabs and windows within the application. Each window must have the pre-request script entered and enabled separately, although any configured environments are available across the application.

  1. Install the Altair Chrome extension here.

  2. After installing, in Altair, select Environments.

  3. In Global environment, paste the following and replace your information in the accountId, username, and password fields:

    {
    "host": "https://api.boomi.com",
    "accountId": "<accountId>",
    "username": "<username>",
    "password": "<password>"
    }

  4. Add an environment and select it as active.

  5. Paste the code below in Altair's Pre-request script area and select Enable pre-request script.

    const btoa = globalThis.btoa || ((str) => Buffer.from(str, 'binary').toString('base64'));
    const atob = globalThis.atob || ((str) => Buffer.from(str, 'base64').toString('binary'));

    const debug = altair.helpers.getEnvironment('debug');
    debug && console.log('Debug mode is on');

    const currentTime = Date.now();
    debug && console.log(`currentTime: ${currentTime}`);

    const authorizationExpiresAt = altair.helpers.getEnvironment('authorizationExpiresAt');
    debug && console.log(`authorizationExpiresAt: ${authorizationExpiresAt}`);

    if(!!!authorizationExpiresAt || currentTime >= authorizationExpiresAt) {
    debug && console.log('Attempting to fetch new authorization token...');

    const host = altair.helpers.getEnvironment('host');
    debug && console.log(host);
    if(!!!host) {
    throw Error("'host' must be set in environment.");
    }

    const accountId = altair.helpers.getEnvironment('accountId');
    debug && console.log(`accountId: ${accountId}`);
    if(!!!accountId) {
    throw Error("'accountId' must be set in environment.");
    }

    const username = altair.helpers.getEnvironment('username');
    debug && console.log(`username: ${username}`);
    if(!!!username) {
    throw Error("'username' must be set in environment.");
    }

    const password = altair.helpers.getEnvironment('password');
    debug && console.log(`password: ${password}`);
    if(!!!password) {
    throw Error("'password' must be set in environment.");
    }

    const basicAuthorization = `Basic ${btoa(`${username}:${password}`)}`;
    debug && console.log(`basicAuthorization: ${basicAuthorization}`);

    try {
    const res = await fetch(new Request(`${host}/auth/jwt/generate/${accountId}`), {
    method: 'GET',
    headers: new Headers({
    Authorization: basicAuthorization,
    }),
    });
    debug && console.log(res);

    switch(res.status) {
    case 200:
    const resText = await res.text();
    debug && console.log(`resText: ${resText}`);

    const bearerAuthorization = `Bearer ${resText}`;
    debug && console.log(`bearerAuthorization: ${bearerAuthorization}`);

    const newAuthorizationExpiresAt = JSON.parse(atob(bearerAuthorization.split('.')[1])).exp;
    altair.helpers.setEnvironment('authorizationExpiresAt', newAuthorizationExpiresAt, true);
    altair.helpers.setEnvironment('headers', {
    Authorization: bearerAuthorization,
    }, true);

    break;
    case 401:
    throw Error(`Could not get Authorization token. Username or password is incorrect.`);
    case 500:
    throw Error(`Could not get Authorization token. Internal server error occurred.`);
    default:
    throw Error(`Could not get Authorization token. Received status code ${res.status}.`);
    }
    } catch (err) {
    debug && console.error(err);
    throw Error(err);
    }
    } else {
    debug && console.log('Doing nothing, authorization token has not expired.');
    }