Skip to content
Agentpost robot in sunglasses on a red circleAgentpost

Issue and use a workspace key

Use this guide to give one tool, such as a dashboard, read access to every project of a workspace; for a server that works in one project, issue a secret key instead. Workspace keys says what the key reaches and who may issue one.

token is the API key or session of an owner or admin of the workspace, or a delegated token whose delegation lists the apoa.workspace_key permissions. Any other caller’s request answers forbidden, with the missing permission in details.

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. Issue the key

Send POST /v1/workspaces/{workspaceId}/keys with a name that says what the key is for, the environment it reads, and its permissions:

const { key: workspaceKey, record } = await api(
  'POST',
  `/v1/workspaces/${workspace.id}/keys`,
  token,
  { name: 'analytics', environment: 'dev', permissions: ['events:read'] },
);
console.log(workspaceKey.slice(0, 8), record.status); // wk_test_ active

workspaceKey is shown once: store it where the tool’s server reads it. For production, send environment: 'prd'. Permissions lists what a key may hold. For a tool with a known end, send expiresAt too, such as expiresAt: '2027-01-01T00:00:00Z'; Expiry and last use gives its rule. This step may answer invalid_request.

2. Read a project

Send the key as the credential and the project’s ID in the x-agentpost-project header, on the service’s own path. This example queries the project’s events:

const { events } = await api(
  'GET',
  '/v1/events/events?limit=20',
  workspaceKey,
  undefined,
  { 'x-agentpost-project': project.id },
);
console.log(events.length); // 0

It prints the number of events in the answer, 0 for a project that has published none. Send one request for each project the tool reads. A request without the header answers invalid_request, and a project the key can’t reach answers not_found; Naming the project lists each refusal. With the SDK, create one client for each project, and the client sends the header.

3. List what the key reaches

Send GET /v1/workspace-keys/current with the key itself and no project header:

const reach = await api('GET', '/v1/workspace-keys/current', workspaceKey);
const reached = reach.projects.map((p: { id: string }) => p.id);
console.log(reach.key.status, reached.includes(project.id)); // active true

reach.projects holds the ID and name of each project the key reaches now. Have the tool send this request to find the projects to read, and again to find projects added since. A project is listed once it adopts a service the key’s permissions read, as step 2 of Create a project and issue a key shows.

Check it worked

List the workspace’s keys with token and find the key by its ID:

type WorkspaceKey = {
  id: string;
  name: string;
  status: string;
  prefix: string;
};
const { keys } = await api('GET', `/v1/workspaces/${workspace.id}/keys`, token);
const listed: WorkspaceKey = keys.find((k: WorkspaceKey) => k.id === record.id);
console.log(listed.name, listed.status, workspaceKey.startsWith(listed.prefix)); // analytics active true

The list never holds a key itself: each key’s prefix tells keys apart. Workspace keys says what ends the key’s access, and Rotate a workspace key and Revoke a workspace key replace and stop it.