Skip to content
Agentpost robot in sunglasses on a red circleAgentpost

Let one principal act for another

Use this guide when one principal, the delegate, should act for another, the delegator, such as an agent your product runs acting for Ana. Delegations explains what a delegation allows. Here the delegate is a new agent and Ana is the delegator; the same calls work for any two principals that aren’t guests. The delegator must hold each permission it delegates through a project role or its role in the workspace.

Before you start

This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.

1. Create the agent

Your server creates the agent as a principal of its own with POST /v1/identity/principals. The answer holds the agent’s first API key, agp_ak_…, shown only in this answer. The agent keeps the key:

const { principal: bot, token: botKey } = await identity(
  'POST',
  '/principals',
  { kind: 'agent', displayName: 'Report Bot' },
);
console.log(bot.kind, botKey.slice(0, 7)); // agent agp_ak_

A signed-in principal can create one the same way with its own credential. To cap a key whatever its delegations allow, issue another with restrictions; Principals explains them.

2. Request a delegation

The agent asks Ana, with its own key, for a delegation to view and edit reports in Acme for 30 days:

const { delegation } = await api(
  'POST',
  '/v1/identity/delegations/requests',
  botKey,
  {
    delegatorId: ana.principal.id,
    workspaceId: acme.id,
    permissions: ['report:view', 'report:edit'],
    expiresIn: '30d',
    reason: 'Keep the weekly reports up to date',
  },
);
console.log(delegation.status); // requested

3. Approve it

Ana approves the delegation with Ana’s own session or API key and POST /v1/identity/delegations/{id}/approve, or denies it with /deny. The agent reads GET /v1/identity/delegations/{id} with its key. Poll while it is requested and before expiresAt. Continue when it is active:

await api(
  'POST',
  `/v1/identity/delegations/${delegation.id}/approve`,
  ana.token,
);
const { delegation: approved } = await api(
  'GET',
  `/v1/identity/delegations/${delegation.id}`,
  botKey,
);
console.log(approved.status); // active

4. Act for Ana

The agent exchanges its key, or a session of its own, for a delegated token with POST /v1/identity/delegations/{id}/tokens. Exchanging can answer conflict. Calls with the token can answer unauthenticated. With the token, the agent finds what it may do on a resource before it acts:

const { token: asAna } = await api(
  'POST',
  `/v1/identity/delegations/${delegation.id}/tokens`,
  botKey,
  {},
);

const { permissions } = await api(
  'GET',
  `/v1/identity/access/permissions?resourceType=report&resourceId=r1&workspaceId=${acme.id}`,
  asAna,
);
console.log(permissions); // [ 'report:view', 'report:edit' ]

Ana’s owner role grants every report action, but the agent’s list holds only the two Ana delegated. The change feed records who the agent acted for.

Check it worked

Check, as Ana through the agent, an action Ana may take but didn’t delegate:

const share = await api('POST', '/v1/identity/access/check', asAna, {
  action: 'share',
  resource: { type: 'report', id: 'r1', workspaceId: acme.id },
});
console.log(share.allowed, share.reason); // false delegation_scope

On delegation_scope, request a delegation that names the permission instead of retrying the action. Once the wider one is active, the agent revokes the narrower one with DELETE /v1/identity/delegations/{id} and its own API key. The errors page lists every check reason, such as delegation_expired and delegation_workspace, and what to do for each.