Node.js example: Getting a bearer token

OAuth is hard, so here is a quick example of how to exchange your server credentials for a bearer token in order to access the API.

📘

This example is in Node.js using a specific library

However, there are plenty of certified libraries compatible with many popular programming languages. You can find a list here: https://openid.net/developers/certified/

1. Get your ClientId and ClientSecret

Please contact you customer success manager to obtain your server credentials

2. Install OAuth client library

For this example, we will be using node-openid-client

npm i openid-client

3. Generate token

Thanks to the maintainers of this library, it is relatively simple to generate a bearer token. Please note: bearer tokens expire, so you will need to repeat this process once your token expires. Our token lifespans are currently 1 hour.

const OpenIdClient = require("openid-client");

const identityUrl = "https://app.altruisticidentity.com";
const clientId = "fetch-from-config";
const clientSecret = "fetch-from-config";
const credentialsScope = "platformapiaccess";

async function GetAccessToken() {
    var issuer = await OpenIdClient.Issuer.discover(identityUrl);

    const client = new issuer.Client({
        client_id: clientId,
        client_secret: clientSecret
    });

    const grantResponse = await client.grant({
        grant_type: 'client_credentials',
        scope: credentialsScope,
    });

    const accessToken = grantResponse.access_token
}

GetAccessToken();

4. Make an API call

You can now use the accessToken in your API calls via the Authorization HTTP header as a bearer token.