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.
What it shows
- The agent reads its client ID and its secret from the environment.
- It asks the token endpoint of the organization for a token, with the client credentials grant.
- It names no scope, so the token carries all of its capabilities as scope values.
- With that token, the agent asks who it is: its ID, its status, its organization and its capabilities.
- 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 capabilitiesreports:readandsummaries:write. Its API key needs the scopesadmin:agents:readandadmin:agents:write. - The client ID of an agent is its agent ID (
agt_…).
| Variable | Value |
|---|---|
LUMOAUTH_URL | The address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region. |
LUMOAUTH_ORG_ID | Your organization, for example acme-corp. |
LUMOAUTH_API_KEY | For 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_ID | The client ID of the agent, which is its agent ID (agt_…). The setup script prints it. |
AGENT_CLIENT_SECRET | The 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.
- Node.js
- Python
- Go
- Java
- C#
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
agents/first-token/python/agent.py
"""An agent gets a token with its client ID and secret, and reads its own
identity.
python agents/first-token/python/agent.py
"""
from lumoauth import LumoAuthAgent
# Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
agent = LumoAuthAgent()
# The agent asks for no scope, so the token carries all of its capabilities.
if not agent.authenticate():
raise SystemExit("The agent could not get a token. Check AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.")
print(f"token scope: {' '.join(agent.token_scopes)}")
# The agent asks who it is, with its own token.
me = agent.get_identity()
identity = me["identity"]
print(f"agent: {identity['agent_id']} ({identity['status']}) in {identity['tenant']}")
print(f"capabilities: {' '.join(me['capabilities'])}")
# 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.
print(f"budget spent: {'yes' if agent.is_budget_exhausted() else 'no'}")
python agents/first-token/python/agent.py
agents/first-token/go/main.go
// An agent gets a token with its client ID and secret, and reads its own
// identity.
//
// go run ./agents/first-token/go
package main
import (
"context"
"fmt"
"log"
"strings"
lumoauth "github.com/lumoauth/lumo-auth-go"
)
func main() {
// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
agent, err := lumoauth.NewAgent(lumoauth.AgentConfig{})
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
// The agent asks for no scope, so the token carries all of its capabilities.
if _, err := agent.Authenticate(ctx); err != nil {
log.Fatal(err)
}
fmt.Printf("token scope: %s\n", strings.Join(agent.TokenScopes(), " "))
// The agent asks who it is, with its own token.
me, err := agent.Identity(ctx, false)
if err != nil {
log.Fatal(err)
}
fmt.Printf("agent: %v (%v) in %v\n", me.Identity["agent_id"], me.Identity["status"], me.Identity["tenant"])
fmt.Printf("capabilities: %s\n", strings.Join(me.Capabilities, " "))
// 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.
budget, err := agent.Budget(ctx)
if err != nil {
log.Fatal(err)
}
spent := "no"
if budget.Exhausted() {
spent = "yes"
}
fmt.Printf("budget spent: %s\n", spent)
}
go run ./agents/first-token/go
agents/first-token/java/FirstToken.java
// An agent gets a token with its client ID and secret, and reads its own
// identity.
//
// java -cp "java/lib/*" agents/first-token/java/FirstToken.java
import io.lumoauth.LumoAgent;
import io.lumoauth.model.AgentIdentity;
public class FirstToken {
public static void main(String[] args) {
// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
LumoAgent agent = LumoAgent.fromEnvironment();
// The agent asks for no scope, so the token carries all of its capabilities.
agent.authenticate();
System.out.println("token scope: " + String.join(" ", agent.tokenScopes()));
// The agent asks who it is, with its own token.
AgentIdentity me = agent.identity();
System.out.printf("agent: %s (%s) in %s%n",
me.identity().get("agent_id"), me.identity().get("status"), me.identity().get("tenant"));
System.out.println("capabilities: " + String.join(" ", me.capabilities()));
// 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.
System.out.println("budget spent: " + (agent.budget().exhausted() ? "yes" : "no"));
}
}
java -cp "java/lib/*" agents/first-token/java/FirstToken.java
agents/first-token/csharp/FirstToken.cs
#:package LumoAuth@1.7.0
#:property PublishAot=false
// An agent gets a token with its client ID and secret, and reads its own
// identity.
//
// dotnet run agents/first-token/csharp/FirstToken.cs
using LumoAuth;
// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
using var agent = LumoAgent.FromEnvironment();
// The agent asks for no scope, so the token carries all of its capabilities.
await agent.AuthenticateAsync();
Console.WriteLine($"token scope: {string.Join(' ', agent.TokenScopes)}");
// The agent asks who it is, with its own token.
var me = await agent.IdentityAsync();
Console.WriteLine($"agent: {me.Identity["agent_id"]} ({me.Identity["status"]}) in {me.Identity["tenant"]}");
Console.WriteLine($"capabilities: {string.Join(' ', me.Capabilities)}");
// 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.
var budget = await agent.BudgetAsync();
Console.WriteLine($"budget spent: {(budget.Exhausted ? "yes" : "no")}");
dotnet run agents/first-token/csharp/FirstToken.cs
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 listwith the lumo CLI. The columnLAST USEDof 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
- The budget of this agent has no limits, so
budget spentis alwaysno. Give an agent a budget sets limits. - A secret can be copied. An agent that signs in with a key replaces it with a private key, and Bind an agent token to the agent makes a copied token useless.
- The capabilities in the token say what the agent may ask for. Your API still has to check the scope of each token that it receives.