---
title: "Let one principal act for another"
description: "Create an IdentityHound principal with its own API key, have another principal approve the delegation it requests, and act for that principal within exactly the permissions it approved."
url: "https://agentpost.tools/docs/identityhound/guides/agents"
kind: guide
needs:
  - what: "The `api` helper"
    from: "https://agentpost.tools/docs/platform/quickstart.md"
  - what: "The `identity` helper, and a config whose roles grant `report` permissions"
    from: "https://agentpost.tools/docs/identityhound/quickstart.md"
  - what: "Ana's sign-in, `ana`, and the workspace Ana owns, `acme`"
    from: "https://agentpost.tools/docs/identityhound/quickstart.md"
---

# 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](/docs/identityhound/concepts/delegations.md) 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

-   The `api` helper, from [Platform quickstart](/docs/platform/quickstart.md).
-   The `identity` helper, and a config whose roles grant `report` permissions, from [IdentityHound quickstart](/docs/identityhound/quickstart.md).
-   Ana's sign-in, `ana`, and the workspace Ana owns, `acme`, from [IdentityHound quickstart](/docs/identityhound/quickstart.md).

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:

```ts
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](/docs/identityhound/concepts/principals.md#api-keys) 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:

```ts
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`:

```ts
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](/docs/identityhound/concepts/delegations.md#delegated-tokens) with `POST /v1/identity/delegations/{id}/tokens`. Exchanging can answer [`conflict`](/docs/identityhound/errors.md#conflict). Calls with the token can answer [`unauthenticated`](/docs/identityhound/errors.md#unauthenticated). With the token, the agent finds what it may do on a resource before it acts:

```ts
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](/docs/identityhound/concepts/change-feed.md#events) records who the agent acted for.

## Check it worked

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

```ts
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`](/docs/identityhound/errors.md#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](/docs/identityhound/errors.md#check-reasons) lists every check reason, such as `delegation_expired` and `delegation_workspace`, and what to do for each.
