Skip to main content

An agent gets its first token

Start an agent with a client ID and secret, get a token with its capabilities, and read its own identity.

RecipeReadyView source
Works with
JavaScript / TypeScriptPythonGoJavaC# / .NET

What it shows​

  1. The agent reads its client ID and its secret from the environment.
  2. It asks the token endpoint of the organization for a token, with the client credentials grant.
  3. It names no scope, so the token carries all of its capabilities as scope values.
  4. With that token, the agent asks who it is: its ID, its status, its organization and its capabilities.
  5. It reads its budget, and stops when a limit is reached.

The agent holds no API key and no token of a person. It acts under its own identity.

Before you start​

  • An agent with the identity type client_secret. The setup script registers one with the name "Report builder" and the capabilities reports:read and summaries:write. Its API key needs the scopes admin:agents:read and admin:agents:write.
  • The client ID of an agent is its agent ID (agt_…).
VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_API_KEYFor the setup script only: an API key with the scopes admin:agents:read and admin:agents:write. The agent itself needs no API key.
AGENT_CLIENT_IDThe client ID of the agent, which is its agent ID (agt_…). The setup script prints it.
AGENT_CLIENT_SECRETThe client secret of the agent. The setup script prints it.

The setup script creates what the recipe needs, through the Admin API. You can run it again at any time.

node agents/first-token/setup.mjs
setup.mjs
// Registers the agent of this recipe, with a client ID and secret and two
// capabilities. Run it again at any time.
//
// node agents/first-token/setup.mjs
//
// The API key needs the scopes admin:agents:read and admin:agents:write.
import { call, ensure, printEnv } from '../../_lib/admin.mjs';

const agent = await ensure('/admin/agents', (a) => a.name === 'Report builder', {
name: 'Report builder',
description: 'Reads sales reports and writes a weekly summary.',
identityType: 'client_secret',
capabilities: ['reports:read', 'summaries:write'],
});

// LumoAuth shows a secret once, in the answer that creates it. On a second
// run the script therefore asks for a new secret, and the old one stops.
const secret =
agent.clientSecret ??
(await call('POST', `/admin/agents/${agent.agentId}/rotate-credentials`)).data.clientSecret;

console.log(`Agent "${agent.name}" is ready. Its client ID is its agent ID.`);
printEnv({ AGENT_CLIENT_ID: agent.agentId, AGENT_CLIENT_SECRET: secret });

LumoAuth shows a secret once. A second run of the setup script therefore asks for a new secret, and the old one stops at once.

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization.

agents/first-token/js/agent.mjs
// An agent gets a token with its client ID and secret, and reads its own
// identity.
//
// node agents/first-token/js/agent.mjs
import { LumoAgent } from '@lumoauth/agent';

// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
const agent = new LumoAgent();

// The agent asks for no scope, so the token carries all of its capabilities.
await agent.authenticate();
console.log(`token scope: ${agent.tokenScopes.join(' ')}`);

// The agent asks who it is, with its own token.
const me = await agent.getCurrentAgent();
console.log(`agent: ${me.identity.agent_id} (${me.identity.status}) in ${me.identity.tenant}`);
console.log(`capabilities: ${me.capabilities.join(' ')}`);

// The budget holds the limits of the agent and its usage. An agent stops
// here when a limit is reached, because the next call would get 429.
const budget = await agent.budget();
console.log(`budget spent: ${budget.exhausted ? 'yes' : 'no'}`);
node agents/first-token/js/agent.mjs

Check the result​

Output
token scope: reports:read summaries:write
agent: agt_7213a44f6202334d (active) in Acme Corp
capabilities: reports:read summaries:write
budget spent: no
  • In the portal, open Audit logs under Security. Each run adds an entry with the event oauth.agent_token_issued, and the actor of the entry is the agent.
  • Run lumo agents list with the lumo CLI. The column LAST USED of the agent now shows a time.
  • Run the program with a wrong secret. The token endpoint answers 401, and the program stops before it calls anything else.

Limits​