Node.js Quickstart
Use this guide to add Choreo Identity & Access Management authentication to a Node.js service using the @thunderid/node SDK.
What You Will Learn
- Create a Node.js project
- Install the
@thunderid/nodepackage - Authenticate a service with the
client_credentialsgrant - 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
- Sign in to the Console at https://console.cloud.choreolabs.com/iam/. Select your project in the project switcher in the header.
- Navigate to Applications.
- Click Add application.
- From the Choose a type page, select Backend service.
- Enter a name (e.g.
My Node.js Service). - Click Create.
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.
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).
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.
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:
- npm
- Yarn
- pnpm
- Bun
- Deno
mkdir my-node-service
cd my-node-service
npm init -y
mkdir my-node-service
cd my-node-service
yarn init -y
mkdir my-node-service
cd my-node-service
pnpm init
mkdir my-node-service
cd my-node-service
bun init -y
mkdir my-node-service
cd my-node-service
deno init
Install @thunderid/node
Install the Choreo Identity & Access Management Node.js SDK:
- npm
- Yarn
- pnpm
- Bun
- Deno
npm install @thunderid/node
yarn add @thunderid/node
pnpm add @thunderid/node
bun add @thunderid/node
deno add npm:@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.
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}`);
Replace <your-client-id> and <your-client-secret> with the Client ID and Client secret from your Choreo Identity & Access Management application.
Configuration Parameters
| Parameter | Description |
|---|---|
baseUrl | Your Choreo Identity & Access Management instance URL (e.g., https://<DATA_PLANE_URL>) |
clientId | The Client ID from your Choreo Identity & Access Management application |
clientSecret | The Client Secret from your Choreo Identity & Access Management application |
grantType | Set 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:
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:
- npm
- Yarn
- pnpm
- Bun
- Deno
node index.mjs
yarn node index.mjs
pnpm node index.mjs
bun index.mjs
deno run -A index.mjs
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:
-
Download the sample from its releases and open its folder.
-
Create
.envin the sample's folder with your application's values:.envTHUNDERID_BASE_URL=https://<DATA_PLANE_URL>THUNDERID_CLIENT_ID=<your-client-id>THUNDERID_CLIENT_SECRET=<your-client-secret> -
Install the dependencies and start the app:
npm installnpm start -
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.
- Node.js SDK Reference: Explore the full API reference for the Node.js SDK.
Was this page helpful?