# Admin API keys

Script API key and Subaccount policy setup with an admin API key: how it is created, what it can change, its guardrails, and its errors.

An admin API key lets a script manage your Main Account's API keys, their policies, and your Subaccount policies without signing in. Use it to set up many Subaccounts at once, for example when a market maker scripts account onboarding. An admin key never moves funds.

## Create or promote an admin key

Only the Main Account owner can create an admin key, or turn an existing key into one, in a signed-in session with a fresh step-up in `X-Auth-Step-Up`. An API key can never create, promote, change, or delete an admin key, including itself.

- **Create** · [`CreateApiKey`](https://testnet.polyester.com/docs/api-docs/connect/auth.v1.ApiKeyService/CreateApiKey) ([`POST /v1/auth/api-keys`](https://testnet.polyester.com/docs/api-docs/rest/POST/v1/auth/api-keys)) with `purpose` `ADMIN_KEY`, no `subaccountId`, and a non-empty `ipWhitelist`.
- **Promote or demote** · [`UpdateApiKey`](https://testnet.polyester.com/docs/api-docs/connect/auth.v1.ApiKeyService/UpdateApiKey) ([`PATCH /v1/auth/api-keys/{key_id}`](https://testnet.polyester.com/docs/api-docs/rest/PATCH/v1/auth/api-keys/key_id)) with `purpose` `ADMIN_KEY` or `STANDARD`. On ConnectRPC, select `purpose` in `updateMask`, plus `ip_whitelist` when the key has no allowlist yet.

An admin key acts for the Main Account only: a Subaccount key or an assistant key cannot become one (`AUTH_INVALID_REQUEST`), and neither can a key whose policy allows a money-out action (`AUTH_ADMIN_KEY_ACTION_NOT_ALLOWED`). [`ListApiKeys`](https://testnet.polyester.com/docs/api-docs/connect/auth.v1.ApiKeyService/ListApiKeys) and [`GetApiKey`](https://testnet.polyester.com/docs/api-docs/connect/auth.v1.ApiKeyService/GetApiKey) show an admin key with `purpose` `ADMIN_KEY`.

> **In development, coming soon**
>
> Creating and promoting admin keys in the Polyester app is in development, coming soon. Until then, use the API calls above from a signed-in session.

### IP allowlist

The allowlist is required. List every address your scripts use, up to 32 entries. Each entry is a single address or a range no broader than `/24` (IPv4) or `/48` (IPv6); IPv4-mapped IPv6 entries are refused. An empty list, a catch-all, or a broader range is refused with `AUTH_ADMIN_KEY_ALLOWLIST_INVALID`.

Keys the admin key creates or edits must use allowlist entries inside its own, so add your trading hosts' addresses to it as well. Only the owner's session, with a fresh step-up, can change an admin key's allowlist. See [IP allowlists](https://testnet.polyester.com/docs/developer-docs/authentication-security/api-key-ip-allowlists).

### Permissions

An admin key's rights come from its type and from three permissions in its API key policy. They are honored only on an admin key:

- **`MANAGE_API_KEYS`** · [`CreateApiKey`](https://testnet.polyester.com/docs/api-docs/connect/auth.v1.ApiKeyService/CreateApiKey), [`UpdateApiKey`](https://testnet.polyester.com/docs/api-docs/connect/auth.v1.ApiKeyService/UpdateApiKey), and [`DeleteApiKey`](https://testnet.polyester.com/docs/api-docs/connect/auth.v1.ApiKeyService/DeleteApiKey)
- **`MANAGE_API_KEY_POLICIES`** · [`SetApiKeyPolicy`](https://testnet.polyester.com/docs/api-docs/connect/auth.v1.PolicyService/SetApiKeyPolicy)
- **`MANAGE_SUBACCOUNT_POLICIES`** · [`SetSubaccountPolicy`](https://testnet.polyester.com/docs/api-docs/connect/auth.v1.PolicyService/SetSubaccountPolicy) and [`UpdateSubaccountPolicy`](https://testnet.polyester.com/docs/api-docs/connect/auth.v1.PolicyService/UpdateSubaccountPolicy)

An admin key can make these six calls over REST and ConnectRPC. Every other key and policy change still needs a session. See [API key policies](https://testnet.polyester.com/docs/developer-docs/account/api-key-policies) for building the policy.

## What an admin key can change

- Create, update, and revoke the standard API keys of the Main Account and of the Subaccounts it owns.
- Attach or clear those keys' policies, and apply [policy templates](https://testnet.polyester.com/docs/developer-docs/account/api-key-policies#policy-templates).
- Attach, clear, and update Subaccount policies.

## What an admin key can never do

- Withdraw or transfer funds, or change whitelists, the address book, or auto-withdraw rules. Its policy can never allow `EXTERNAL_WITHDRAW`, `INTERNAL_TRANSFER`, `MANAGE_ADDRESS_BOOK`, or `MANAGE_AUTO_WITHDRAW`.
- Change sign-in, MFA, wallet, or session settings.
- Create, change, or revoke an admin key, including itself.
- Reach an assistant key.
- Change a key or policy that allows a money-out action. It can revoke such a key.
- Change a locked Subaccount policy, or reach a Subaccount your Main Account does not own.
- Edit or replace the policy of a Subaccount marked `strategyBot`.
- Give a Subaccount key more actions or markets than its Subaccount policy allows.

Each call rechecks the admin key when it runs: its status, expiry, type, permissions, and your address against its allowlist. Revoking or demoting an admin key, or narrowing its allowlist, takes effect immediately.

## Audit and notifications

Every change an admin key makes is recorded in your [account activity](https://testnet.polyester.com/docs/developer-docs/account/account-activity), with the admin key and the source address. It also creates a security notification of type `ADMIN_KEY_ACTION` that names the admin key, the action, the key, Subaccount, or policy it changed, and the source address; see [Notifications](https://testnet.polyester.com/docs/developer-docs/account/price-alerts-and-notifications). If nobody on your account made a change, revoke the admin key and review your API keys.

## Script Subaccount onboarding

Build your policies once, as [templates](https://testnet.polyester.com/docs/developer-docs/account/api-key-policies#policy-templates) or regular policies, then repeat these steps for each Subaccount with the admin key:

1. Attach the Subaccount policy

   Call `SetSubaccountPolicy` ([`PATCH /v1/auth/subaccounts/&#123;subaccount_id&#125;/policy`](https://testnet.polyester.com/docs/api-docs/rest/PATCH/v1/auth/subaccounts/subaccount_id/policy)) with the Subaccount and its policy. Do this first: a Subaccount without a policy is read-only, and an admin key cannot give a Subaccount key more than its Subaccount policy allows, so attaching a trading key policy before it is refused with `AUTH_ADMIN_KEY_SCOPE_EXCEEDED`.

2. Create the Subaccount's key

   Call `CreateApiKey` with the Subaccount's `subaccountId`, the new key's public key, and an `ipWhitelist` inside the admin key's own allowlist.

3. Attach the key's policy

   Call `SetApiKeyPolicy` with the new key's `keyId` and the policy or template to apply.

The calls share the `ACCOUNT_SECURITY` [rate limit](https://testnet.polyester.com/docs/developer-docs/shared-concepts/rate-limits#authentication-and-account-management): 60 units a minute with a burst of 20. Creating, updating, or revoking a key costs 5 units, and each policy call costs 2, so pace scripted onboarding. Each change also sends a notification, about three per Subaccount with the steps above.

## Errors

Each entry gives the code and its HTTP status, then the condition and what to do. ConnectRPC returns the same code in an `auth.v1.AuthErrorDetail`.

- **`AUTH_ADMIN_KEY_REQUIRED`** · HTTP 403 · `permission_denied`\
  The key that signed the request is not an admin key. Use the admin key, or a session.
- **`AUTH_ADMIN_KEY_ESCALATION_DENIED`** · HTTP 403 · `permission_denied`\
  The request would create, promote, or change an admin key, or attach an admin permission, by API key. Only the owner's session with a fresh step-up can.
- **`AUTH_ADMIN_KEY_SCOPE_EXCEEDED`** · HTTP 403 · `permission_denied`\
  The change would give a key an allowlist entry outside the admin key's own, or a Subaccount key more actions or markets than its Subaccount policy.
- **`AUTH_ADMIN_KEY_FORBIDDEN_TARGET`** · HTTP 403 · `permission_denied`\
  The target is an assistant key, a key or policy that allows a money-out action, a locked Subaccount policy, or a Subaccount your Main Account does not own.
- **`AUTH_STRATEGY_BOT_MANAGED`** · HTTP 412 · `failed_precondition`\
  The target is managed or has a retained, restricted Subaccount policy. An attached policy cannot be edited or replaced, and may be cleared only after managed access ends.
- **`AUTH_ADMIN_KEY_ALLOWLIST_INVALID`** · HTTP 400 · `invalid_argument`\
  The admin key's allowlist is empty, or has an entry broader than `/24` or `/48`, or an IPv4-mapped IPv6 entry.
- **`AUTH_ADMIN_KEY_ACTION_NOT_ALLOWED`** · HTTP 412 · `failed_precondition`\
  The admin key's policy would allow a money-out action, or a key with such a policy was to become an admin key.
- **`AUTH_IP_NOT_ALLOWED`** · HTTP 401 · `unauthenticated`\
  The request came from an address outside the admin key's allowlist. It is refused at ingress, so REST answers with the short code `IP_NOT_ALLOWED` (HTTP 401); ConnectRPC carries `AUTH_IP_NOT_ALLOWED` in the `auth.v1.AuthErrorDetail`.
- **`AUTH_API_KEY_ACCESS_DENIED`** · HTTP 403 · `permission_denied`\
  The admin key's policy lacks the permission the call needs.
