---
title: "Issue and use a workspace key"
description: "Issue a workspace key for a tool that reads across a workspace's projects, read one project with it by naming the project on the request, and list the projects it reaches."
url: "https://agentpost.tools/docs/platform/guides/workspace-keys"
kind: guide
needs:
  - what: "The `api` helper that sends each request"
    from: "https://agentpost.tools/docs/platform/quickstart.md"
  - what: "Your credential, `token`, as an owner or admin of the workspace the key reads, `workspace`"
    from: "https://agentpost.tools/docs/platform/guides/sign-up.md"
  - what: "A project of that workspace, `project`, that adopted `eventwolf`"
    from: "https://agentpost.tools/docs/platform/guides/keys.md"
---

# 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](/docs/platform/guides/keys.md) instead. [Workspace keys](/docs/platform/concepts/workspace-keys.md) 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`](/docs/errors.md#forbidden), with the missing permission in `details`.

## Before you start

-   The `api` helper that sends each request, from [Platform quickstart](/docs/platform/quickstart.md).
-   Your credential, `token`, as an owner or admin of the workspace the key reads, `workspace`, from [Start a workspace](/docs/platform/guides/sign-up.md).
-   A project of that workspace, `project`, that adopted `eventwolf`, from [Create a project and issue a key](/docs/platform/guides/keys.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\. 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`:

```ts
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](/docs/platform/concepts/workspace-keys.md#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](/docs/platform/concepts/workspace-keys.md#expiry-and-last-use) gives its rule. This step may answer [`invalid_request`](/docs/errors.md#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:

```ts
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`](/docs/errors.md#invalid_request), and a project the key can’t reach answers [`not_found`](/docs/errors.md#not_found); [Naming the project](/docs/platform/concepts/workspace-keys.md#naming-the-project) lists each refusal. With the SDK, [create one client for each project](/docs/eventwolf/sdk.md#create-a-client), 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:

```ts
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](/docs/platform/guides/keys.md) shows.

## Check it worked

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

```ts
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](/docs/platform/concepts/workspace-keys.md#what-ends-its-access) says what ends the key’s access, and [Rotate a workspace key](/docs/platform/reference/keys.md#rotate-a-workspace-key) and [Revoke a workspace key](/docs/platform/reference/keys.md#revoke-a-workspace-key) replace and stop it.
