> Documentation index: https://metorial.com/llms.txt. Every page is available as Markdown by adding .md to its URL.

# Create, rotate and revoke API keys for CI and agents
URL: https://metorial.com/docs/platform/cli/api-keys

Use metorial-admin api-keys to give each pipeline or agent its own key with an expiry and IP allowlist, rotate it with a grace period and revoke a leaked one.

---

Give every pipeline and every agent its own API key, so you can rotate or revoke one without touching the others. `metorial-admin api-keys create` makes a key with an optional expiry date and IP allowlist, `rotate` replaces its secret, and `revoke` turns it off at once. The secret is shown one time, when the key is created.

**What you'll learn**

* How to create a key for one instance or for the whole organization
* How to limit a key by expiry date and IP address
* How to rotate a key, and revoke it after a leak

**Before you start**

* The `metorial-admin` CLI, signed in ([install and sign in](https://metorial.com/docs/platform/cli))
* The ID of the instance the key is for, from `metorial-admin instance list`

## Choose a key type

`--type` takes one of three values:

| Type                                | What it is for                                                                         |
| ----------------------------------- | -------------------------------------------------------------------------------------- |
| `instance_access_token_secret`      | Server-side access to one instance. Needs `--instance-id`. Use this for CI and agents. |
| `instance_access_token_publishable` | A publishable key for one instance. Needs `--instance-id`.                             |
| `organization_management_token`     | Managing the organization itself. Not tied to an instance.                             |

Secret keys start with `metorial_sk_` and publishable keys with `metorial_pk_`.

## Create a key

### 1. Create the key

Name the key after the thing that uses it. Add `--expires-at` and `--ip-filters` to narrow it.

```bash
metorial-admin api-keys create \
  --type instance_access_token_secret \
  --instance-id <instance-id> \
  --name "ci-deploy" \
  --description "Deploy pipeline, main branch" \
  --expires-at 2027-06-30T00:00:00Z \
  --ip-filters 203.0.113.10,203.0.113.11
```

### 2. Store the secret

The response contains the key's ID and its `secret`. Copy the secret into your CI system's secret store now. Later `get` and `list` calls show a redacted form only.

### 3. Use it

Set the key in the environment of the job or agent:

```bash
export METORIAL_API_KEY=<secret>
export METORIAL_INSTANCE_ID=<instance-id>
metorial-admin sessions list --limit 1
```

The SDKs and the API read the same key. See [the API](https://metorial.com/docs/build/api) for the request header.

## List and inspect keys

```bash
metorial-admin api-keys list --type instance_access_token_secret --instance-id <instance-id>
metorial-admin api-keys list --type organization_management_token
metorial-admin api-keys get <api-key-id>
```

Each key shows its status, type, name, IP filters and when it was last used. A key you no longer recognize, or one that has not been used for months, is a candidate for revoking.

`metorial-admin api-keys reveal <api-key-id>` returns the secret of an existing key again.

## Change the limits on a key

```bash
metorial-admin api-keys update <api-key-id> \
  --expires-at 2027-01-31T00:00:00Z \
  --ip-filters 203.0.113.10
```

`update` also takes `--name` and `--description`. It has no flag for the type or the instance. To change those, create a new key.

## Rotate a key

Rotation gives the key a new secret. Pass `--current-expires-at` to keep the old secret valid until that time, so you can roll out the new one without downtime.

```bash
metorial-admin api-keys rotate <api-key-id> --current-expires-at 2026-12-01T12:00:00Z
```

Store the new secret from the response, update the job or agent, and let the old one expire.

## Revoke a key after a leak

If a secret was committed, logged or shared, revoke the key first and investigate second.

```bash
metorial-admin api-keys revoke <api-key-id>
```

Then check what the key did. The organization audit log and the session logs are both available from the CLI. See [See what happened from the terminal](https://metorial.com/docs/platform/cli/logs-and-audit).

A leaked MCP URL is a different credential. If the URL contains `?key=`, delete that token:

```bash
# A token you created as an admin
metorial-admin magic-mcp tokens delete <magic-mcp-token-id>

# A token the metorial CLI created when you ran install
metorial tokens revoke <token-id>
```

## Give each agent its own limited credential

The CLI lets you limit an API key in three ways: to one instance, to a set of IP addresses, and to an expiry date. It has no flag for limiting a key to certain resources or actions.

For an agent that only needs to call tools over MCP, a Magic MCP token is the narrower credential. It reaches only one server, one endpoint or a named group of servers. See [Magic MCP from the terminal](https://metorial.com/docs/platform/cli/magic-mcp).

## In the dashboard

Keys for an instance are under **Developer**, **API Keys**, where **Create API Key** opens the same options.

## Check it worked

Run a read-only command with the new key and no saved profile, for example in a clean shell:

```bash
METORIAL_API_KEY=<secret> metorial-admin instance list
```

After `revoke`, the same command fails with an authentication error.

## Next steps

* [Install the CLI and sign in](https://metorial.com/docs/platform/cli)
* [See what happened from the terminal](https://metorial.com/docs/platform/cli/logs-and-audit)
* [Magic MCP from the terminal](https://metorial.com/docs/platform/cli/magic-mcp)