Skip to main content

Node.js Quickstart

Cloud has the newest features. Some may not be in a release yet; the newest release is Identity & Access Management 7.3.0.

Use this guide to add Choreo Identity & Access Management authentication to a Node.js service using the @thunderid/node SDK.

Get the complete sample

What You Will Learn​

  • Create a Node.js project
  • Install the @thunderid/node package
  • Authenticate a service with the client_credentials grant
  • Use the resulting access token to call business logic

Prerequisites​

  • About 10 minutes
  • Node.js 18+ installed on your system
  • npm, yarn, or pnpm
  • Your preferred code editor

Create an Application​

  1. Sign in to the Console at https://console.cloud.choreolabs.com/iam/. Select your project in the project switcher in the header.
  2. Navigate to Applications.
  3. Click Add application.
  4. From the Choose a type page, select Backend service.
  5. Enter a name (e.g. My Node.js Service).
  6. Click Create.
Machine-to-Machine

Backend Service applications are pre-configured for the client_credentials grant with client_secret_basic authentication. No OAuth 2.0 redirect URI or extra setup is needed.

Note

Copy the Client ID from the General tab, and the Client secret from the window that pops up when the application is created; it's shown only once (if lost, regenerate it from the application's Credentials tab).

Data plane URL

Code in this guide uses https://<DATA_PLANE_URL> for your environment's data plane URL, where users sign in and tokens are issued. Replace it with the URL on the environment's Endpoints page. To open it, select your project, go to Deployment pipeline, and click Open environment on the environment's card. See Control Plane and Data Plane URLs.

Deploy your changes

Saving a change does not make it live. You author in the control plane, and your users sign in on a separate data plane, so a change takes effect in an environment only after you deploy it. Go to Deploy, deploy the latest version to your first environment, then promote it to the next environment when you are ready.

Auto-sync deploys saved changes to your first environment within seconds. Turning it on is your choice, per project. See Deploy a Version, or learn why you deploy and how auto-sync works.

Create a Node.js Project​

Initialize a new Node.js project:

mkdir my-node-service
cd my-node-service
npm init -y

Install @thunderid/node​

Install the Choreo Identity & Access Management Node.js SDK:

npm install @thunderid/node

Authenticate as the Service​

Create an index.mjs file and initialize ThunderIDNodeClient with grantType: 'client_credentials'. With that set, getAccessToken() authenticates as the service itself, no session and no sign-in, and transparently fetches, caches, and refreshes the token.

index.mjs
import { ThunderIDNodeClient } from '@thunderid/node';

const client = new ThunderIDNodeClient();

await client.initialize({
baseUrl: 'https://<DATA_PLANE_URL>',
clientId: '<your-client-id>',
clientSecret: '<your-client-secret>',
grantType: 'client_credentials',
});

const accessToken = await client.getAccessToken();
const { scope } = await client.decodeJwtToken(accessToken);

console.log(`Authenticated with scope: ${scope}`);
Configuration

Replace <your-client-id> and <your-client-secret> with the Client ID and Client secret from your Choreo Identity & Access Management application.

Configuration Parameters​

ParameterDescription
baseUrlYour Choreo Identity & Access Management instance URL (e.g., https://<DATA_PLANE_URL>)
clientIdThe Client ID from your Choreo Identity & Access Management application
clientSecretThe Client Secret from your Choreo Identity & Access Management application
grantTypeSet to 'client_credentials' to authenticate the service as itself, with no user and no browser redirect

Call Business Logic With the Token​

A real backend keeps authentication in its own module instead of scattering it across business logic. Update index.mjs so the rest of the app asks for a token only when it needs one, instead of handling getAccessToken() and the Authorization header directly:

index.mjs
import { ThunderIDNodeClient } from '@thunderid/node';

const client = new ThunderIDNodeClient();

await client.initialize({
baseUrl: 'https://<DATA_PLANE_URL>',
clientId: '<your-client-id>',
clientSecret: '<your-client-secret>',
grantType: 'client_credentials',
});

async function getStock(sku) {
const accessToken = await client.getAccessToken();

// A real backend would attach this as `Authorization: Bearer ${accessToken}`
// on a request to a separate inventory service. This quickstart just proves
// the token was obtained before answering.
console.log(`Requesting ${sku} with a valid access token`);

return { sku, inStock: true };
}

const item = await getStock('SKU-100');
console.log(item);

Run Your Service​

Start the script:

node index.mjs
Success

The script authenticates once, prints the scope it received, then calls getStock() and prints the result before exiting.

Run the Sample App​

The Node.js sample is a working version of the app this guide builds. To run it instead of building the app yourself:

  1. Download the sample from its releases and open its folder.

  2. Create .env in the sample's folder with your application's values:

    .env
    THUNDERID_BASE_URL=https://<DATA_PLANE_URL>
    THUNDERID_CLIENT_ID=<your-client-id>
    THUNDERID_CLIENT_SECRET=<your-client-secret>
  3. Install the dependencies and start the app:

    npm install
    npm start
  4. The sample gets an access token for itself with the client credentials grant, reads the inventory with it, prints the result, and exits.

What's Next​

  • Agent Authentication: Request specific scopes, narrow tokens to a resource server, and troubleshoot the token endpoint directly.

Was this page helpful?