# Save a registry credential

> POST /v1/registry-credentials. .

Source: https://legion.ambient.xyz/docs/api/images/create-registry-credential

`POST https://api.legion.ambient.xyz/v1/registry-credentials`

Authenticate with an API key: `Authorization: Bearer $LEGION_API_KEY`.

. Saves a username and a secret (a read-only access token, robot token, deploy token or key)
for a registry, so the organization's rentals can run images that registry does not serve
anonymously. Any OCI registry that uses token authentication works: Docker Hub, GHCR, GitLab, Quay,
NGC, Harbor, Artifactory, Artifact Registry, Azure Container Registry, and self-hosted
registries with a token service. Needs `registry.manage`, which owners have; any member can then rent
images with it.

**Host and path prefix.** `image` is an image the credential is for. Its registry host is the
credential's `host` (Docker Hub is `docker.io`; a port other than 443 is refused). `path_prefix`
limits which images the credential is chosen for automatically: whole path segments, no leading or
trailing `/`. Absent, it is the image's namespace (the repository path without its last segment:
`acme` for `ghcr.io/acme/model`, `group/sub/project` for
`registry.gitlab.com/group/sub/project/image`); `""` means every image on the host. `image` must be
under the prefix (400 `image_outside_prefix`). `detectRegistryCredential` answers the same split
without saving anything.

**Several per host, never replaced.** An organization may save up to 20 credentials, several per
host (for example `ghcr.io` `acme` and `ghcr.io` `other-org`). A second credential with the same
host and path prefix is 409 `registry_credential_exists`, with the existing one's id in
`resource_id`: replace its secret with `updateRegistryCredential`. A 21st is 409
`registry_credential_limit`.

**Refused registries.** AWS ECR (`*.dkr.ecr.*.amazonaws.com`) is 422 `registry_ecr_unsupported`: its
passwords are 12-hour tokens minted from AWS IAM. A registry whose `/v2/` asks for HTTP Basic, or no
token service at all, is 422 `registry_auth_unsupported`: the only way a host could pull from it is
with the saved secret itself, which never leaves Legion. IP addresses, single-label names,
`localhost`, private and link-local addresses, cloud metadata addresses, Legion's and Ambient's own
domains, and any port but 443 are 422 `registry_host_not_allowed`. Use a mirror or another registry.

**Token service, discovered and pinned.** On save Legion reads the registry's anonymous
`/v2/` challenge and takes the Bearer `realm` and `service` from it. The realm must be https on port
443 and on the registry's own host, or on a known pair (`docker.io` and `auth.docker.io`,
`registry.gitlab.com` and `gitlab.com`); anything else is 422 `registry_realm_not_allowed`. The realm
is pinned with the credential (`token_realm`): the secret is only ever sent there, never to a realm
a registry names later, never to a redirect. A registry that moves its token service needs the
secret saved again.

**Checked before it is saved.** Legion exchanges the credential at the pinned realm for a
pull token of the image's repository and reads the image's manifest with it. Nothing is saved when
the registry refuses the credential, or it cannot read that repository (422
`registry_credential_rejected`), the image does not exist (422 `image_not_found`), or the registry or
its token service did not answer (503 `image_registry_unavailable`). Every request goes through the
SSRF-hardened registry client: https, port 443, public addresses only (checked again at connect
time, for the host and the realm), no proxy, no redirect followed for the token exchange, bounded
sizes and times.

**Username hints.** The username's meaning depends on the registry (`$oauthtoken` on `nvcr.io`,
`_json_key_base64` on Artifact Registry, `org+robot` on Quay, the deploy token's name on
GitLab); `detectRegistryCredential` returns the hint for a host. `username` is 1 to 255 printable
characters without spaces or colons and `secret` 1 to 8192 printable characters without spaces (400
`invalid_registry_credential`).

**How it is used.** The secret is kept in Vault, keyed by the credential's id, never in Legion's
database, a response, a log, an event or a problem. Hosts never see it: for a rental that is
starting, Legion exchanges it for the registry's own pull token for that rental's one
repository (`getAgentRegistryCredentials`). The pull token is short-lived, as the registry issues it,
and reads the whole repository (every tag), not just the pinned image. The image is pulled on the
provider's machine and runs there, where the provider has root. Make a read-only token for Legion
alone, limited to the images you rent, so it can be revoked without touching anything else.

Each organization may try 20 saves an hour, checked or not (429 `rate_limited`). On a controller
without registry credentials configured the answer is 503 `registry_credentials_unavailable`. Recorded as the event `registry_credential.created` (`org_id`, `user_id`, `registry_credential_id`,
`registry_host`, `client`), never with the username or the secret.

## Request body

- `name`, string: A name for it, shown as "Uses acme on ghcr.io". Printable characters; optional.
- `image`, string, required: An image the credential is for. Its host is the credential's host; its manifest is read with the credential before it is saved.
- `path_prefix`, string: Whole path segments without a leading or trailing `/`, or `""` for every image on the host. Absent: the image's namespace (its repository without the last segment).
- `username`, string, required: The registry username (`$oauthtoken` on `nvcr.io`, the robot account on `quay.io`).
- `secret`, string, required: A read-only token made for Legion. Write-only: kept in Vault, never returned or logged.

Example:

```json
{
  "name": "string",
  "image": "string",
  "path_prefix": "string",
  "username": "string"
}
```

## Responses

### 201

The credential as saved.

- `id`, string (uuid), required
- `name`, string: A name the organization gave it; absent when none.
- `host`, string, required: The registry host, as image references name it: `docker.io` for Docker Hub, `ghcr.io`, `nvcr.io`, `registry.gitlab.com`, `europe-docker.pkg.dev`, `acme.azurecr.io`.
- `path_prefix`, string, required: The repositories it is chosen for automatically: `""` for every image on the host, else whole path segments (`acme`, `group/sub/project`). Of several credentials for one image, the longest prefix wins.
- `username`, string, required: The username the secret goes with (`$oauthtoken` on `nvcr.io`).
- `token_realm`, string, required: The registry's token service, discovered when the secret was saved and pinned: the only address the secret is ever sent to.
- `created_by`, string (uuid): The person who saved it; absent when it was saved with an API key of no person.
- `created_at`, string (date-time), required
- `updated_at`, string (date-time), required: When it was last changed (renamed or its secret replaced).
- `last_used_at`, string (date-time): When a host last got a pull token made from it; absent until then.
- `last_checked_at`, string (date-time), required: When the registry last answered a token exchange with it.
- `last_check_result`, RegistrySignInCheckResult, required: How the sign-in's last check went: `ok`, or `rejected` when the registry refused it (when it was saved, when an image was inspected or a rental created with it, or when a host pulled with it).

Example:

```json
{
  "id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
  "name": "string",
  "host": "ghcr.io",
  "path_prefix": "acme",
  "username": "string",
  "token_realm": "https://ghcr.io/token",
  "created_by": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
  "created_at": "2026-10-15T12:00:00Z",
  "updated_at": "2026-10-15T12:00:00Z",
  "last_used_at": "2026-10-15T12:00:00Z",
  "last_checked_at": "2026-10-15T12:00:00Z",
  "last_check_result": "ok"
}
```

### 400

The request is malformed or fails validation. An image reference that
is not a container image reference answers `invalid_image_reference`.

### 401

Missing, unknown, expired or revoked credentials.

### 403

The caller lacks the permission this operation requires (`forbidden`),
or signed in without having joined an organization yet
(`no_organization`; `getOnboarding` and the invitation and
organization operations under it work), or could not sign in
(`email_unverified`, `account_conflict`, and `domain_not_allowed`). `not_invited` is no longer returned to
session tokens.

`access_not_approved`: a signed-in person who is not approved
for the beta yet. Every operation that takes a credential answers it to
them, except `getMe` and `joinMyWaitlist`; the problem carries
`access` (`waitlisted` or `not_requested`, and the email). Approved
means a member of an organization, holding a pending invitation, or Legion; an API key always belongs to a member.

`oauth_token_mcp_only`: the caller sent an OAuth access token (`lgo_`), which is accepted
only by `mcpMessage` and the tool calls it dispatches. Every other operation answers it, whatever
the token's scope. Use an API key or the Legion web app.

`org_suspended`: the caller's organization is suspended. Every operation that takes a
credential answers it, by session or API key, except `getMe` (which shows `org.suspended_at` and
`org.suspension_reason`) and the support operations under `/v1/support/`.

`terms_not_accepted`: an approved caller who has not accepted
the current version of the Terms of Use and the Acceptable Use Policy. Every operation that takes a credential answers it, except `getMe`,
`acceptLegalDocuments`, `joinMyWaitlist` and the wind-down operations
(`listRentals`, `stopRental`, `deleteRental`, `listHosts`,
`updateListing` with `listed` false, `removeHost`); the problem
carries `legal`. An API key acts for the person who created it, so it
is refused until that person accepts. Terminal
operations also return
`session_required` (called with an API key) and `step_up_required`
(neither a sign-in nor a step-up challenge within the step-up window).

### 409

The resource's current state does not allow the operation. `legal_versions_stale`: a legal document version sent is not the current one; the
problem's `legal` holds the current documents.

### 422

The registry or its realm is not one Legion sends a credential to (`registry_ecr_unsupported`,
`registry_auth_unsupported`, `registry_host_not_allowed`, `registry_realm_not_allowed`), the
registry refused the credential (`registry_credential_rejected`), the image does not exist
(`image_not_found`), or it is in a format Legion cannot read (`image_manifest_unsupported`). Nothing was saved. `registry_host` names the host.

### 429

Rate limited (`code` is `rate_limited`), or the organization asked the
pull-through mirror to copy in too many different tags or digests of one
repository today (`mirror_quota_exceeded`). Retry after the `Retry-After`
seconds.

### 500

An unexpected server error. Safe to retry.

### 503

A dependency is unavailable (`terminal_signing_unavailable`;
`image_registry_unavailable` when the image's registry did not answer in
time or failed; `secrets_unavailable` when secret variables cannot be
stored or read). Safe to retry.

## Errors

- 400 [`image_outside_prefix`](https://legion.ambient.xyz/docs/errors#image_outside_prefix)
- 400 [`invalid_image_reference`](https://legion.ambient.xyz/docs/errors#invalid_image_reference)
- 400 [`invalid_path_prefix`](https://legion.ambient.xyz/docs/errors#invalid_path_prefix)
- 400 [`invalid_registry_credential`](https://legion.ambient.xyz/docs/errors#invalid_registry_credential)
- 403 [`no_organization`](https://legion.ambient.xyz/docs/errors#no_organization)
- 409 [`registry_credential_exists`](https://legion.ambient.xyz/docs/errors#registry_credential_exists)
- 409 [`registry_credential_limit`](https://legion.ambient.xyz/docs/errors#registry_credential_limit)
- 422 [`image_manifest_unsupported`](https://legion.ambient.xyz/docs/errors#image_manifest_unsupported)
- 422 [`image_not_found`](https://legion.ambient.xyz/docs/errors#image_not_found)
- 422 [`registry_auth_unsupported`](https://legion.ambient.xyz/docs/errors#registry_auth_unsupported)
- 422 [`registry_credential_rejected`](https://legion.ambient.xyz/docs/errors#registry_credential_rejected)
- 422 [`registry_ecr_unsupported`](https://legion.ambient.xyz/docs/errors#registry_ecr_unsupported)
- 422 [`registry_host_not_allowed`](https://legion.ambient.xyz/docs/errors#registry_host_not_allowed)
- 422 [`registry_realm_not_allowed`](https://legion.ambient.xyz/docs/errors#registry_realm_not_allowed)
- 429 [`rate_limited`](https://legion.ambient.xyz/docs/errors#rate_limited)
- 503 [`image_registry_unavailable`](https://legion.ambient.xyz/docs/errors#image_registry_unavailable)
- 503 [`registry_credentials_unavailable`](https://legion.ambient.xyz/docs/errors#registry_credentials_unavailable)

## Example request

```sh
curl -s -X POST "https://api.legion.ambient.xyz/v1/registry-credentials" \
  -H "Authorization: Bearer $LEGION_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "string",
  "image": "string",
  "path_prefix": "string",
  "username": "string"
}'
```
