# Inspect a container image before renting it

> POST /v1/images/inspect. Looks the image up in its registry, without pulling any layer, and answers what a rental of it would run: the digest the tag resolves to, the platforms…

Source: https://legion.ambient.xyz/docs/api/images/inspect-image

`POST https://api.legion.ambient.xyz/v1/images/inspect`

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

Looks the image up in its registry, without pulling any layer, and
answers what a rental of it would run: the digest the tag resolves to,
the platforms the image offers, its compressed size and its entrypoint,
command, user and stop signal. The rent form calls this while the
renter types, and `createRental` runs the same checks, so an image this
accepts for a host is one `createRental` accepts for it.

Only public images are supported, and only images anyone can pull
without signing in, plus the images Legion publishes in its own registry
(`registry.legion.ambient.xyz/public/legion/...`), which Legion
reads with its own credentials and which every renter can run.

**Private images.** An image its registry does not
serve anonymously can run when the organization saved a registry
credential for it (`createRegistryCredential`). The credential used is
`registry_credential_id` when given (it must be the organization's,
else 422 `registry_credential_not_found`, and on the image's host, else
422 `registry_credential_host_mismatch`), else the organization's
credential whose host equals the image's and whose path prefix is the
longest one the repository is under. Legion keeps no copy of the image
and runs no registry for it: Legion reads it with a
short-lived token it exchanges from the credential at the registry's
pinned token service, and the host later pulls it straight from that
registry with another such token (`getAgentRegistryCredentials`). Without a credential the answer is 422 `image_private` ("private, or
does not exist"), with the host in `registry_host` (and in `registry`
for `docker.io`, `ghcr.io`, `nvcr.io` and `quay.io`), and `detail`
says a credential can be added. A credential the registry refuses
(revoked, or without access to this repository) is 422
`registry_sign_in_rejected`, with `registry_host` and
`registry_credential_id`. `pinned_reference` of a private image is on
its own registry (`ghcr.io/acme/model@sha256:...`), never on Legion's,
and nothing goes through the mirror; `registry_credential_id` names
the credential it was read with. With the mirror on, an image on a
registry Legion does not mirror is read only with a credential (it is
never pulled anonymously from such a registry): without one it stays
422 `registry_not_supported`. What was read with a credential is
cached for that credential only, never shared with another
organization.

**The mirror.** Every image a rental runs is pulled through
Legion's own registry, which mirrors the public registries: hosts never
pull from an upstream. The supported upstreams are `docker.io` (Docker
Hub: `ubuntu`, `user/repo`, `docker.io/...`, `index.docker.io/...`),
`ghcr.io`, `nvcr.io` and `quay.io`. Legion reads the image's
metadata on its upstream, anonymously: the digest it resolves to, what
it weighs over every platform (the mirror copies them all), the
platforms it offers and its configuration. It never waits for the
mirror: copying the image into Legion's registry happens after a rental
is created (`createRental`). `pinned_reference` is what the host
pulls: `registry.legion.ambient.xyz/<upstream>/<path>@<digest>`. The
image as mirrored (`registry.legion.ambient.xyz/docker.io/library/ubuntu:24.04`)
is accepted as well.

What the mirror refuses, before it copies anything:

- 422 `registry_not_supported`: the image is on any other registry;
  `detail` lists the supported upstreams.
- 422 `image_not_allowed`: the image is in a namespace Legion never
  mirrors (Ambient's own).
- 422 `image_not_found`: the upstream does not have it, or does not
  serve it anonymously and registry sign-ins are not configured on Legion. With them an image the upstream does not serve
  anonymously is read with the organization's sign-in, or is
  `image_private` when it has none.
- 422 `image_too_large`: over every platform the image weighs more than
  the mirror's cap (60 GiB by default); `detail` gives both sizes.
- 429 `mirror_quota_exceeded`: the organization already asked the
  mirror to copy in 20 (by default) different tags or digests of this
  repository today; `Retry-After` is the time to midnight UTC. Asking
  for a tag it already counted is free.

An upstream that is rate limiting or unavailable is 503
`image_registry_unavailable`, with `Retry-After` set to the upstream's
own hint when it gave one (Docker Hub answers 429 to an address that
pulled too much anonymously). Safe to retry. The mirror's own errors
never reach the renter.

A tag can move; the `digest` and `pinned_reference` in the answer are
what a rental made from it would be pinned to. A digest the renter wrote
is the digest the host pulls.

With `host_id` the answer is also checked against that host, which must
be one the caller can rent or one of its own: the image must offer the
host's platform (when the host's agent reports it) and its compressed
size must fit the host's free disk. The `platform` in the answer is
the platform the size and config describe: the host's, or without a
host `linux/amd64` when offered, else the image's first platform.

Results are cached for a minute by reference and by digest. Each
organization may make 30 uncached lookups a minute; a cached answer is
free. Beyond that the answer is 429 `rate_limited` with `Retry-After`.

## Request body

- `reference`, string, required: Container image reference as the renter would type it: `ubuntu`, `ubuntu:24.04`, `pytorch/pytorch:2.4.0-cuda12.4-cudnn9-runtime`, `ghcr.io/org/app:tag`, `registry.ambient.xyz/public/legion/cuda`, or any of those with `@sha256:...`.
- `host_id`, string (uuid): Check the image against this host as well. It must be a host that is offered for rent or one of the caller's own; any other is 404.
- `registry_credential_id`, string (uuid): . Read a private image with this registry credential instead of the one matched by host and path prefix. It must be the organization's and on the image's host.

Example:

```json
{
  "reference": "string",
  "host_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
  "registry_credential_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b"
}
```

## Responses

### 200

The image, as a rental of it would run it.

- `reference`, string, required: The reference in its full form, with the registry, `library/` and the tag filled in.
- `digest`, ImageDigest, required: The content digest of the image a rental is pinned to: of its manifest list for a multi-platform image, so the same digest runs on any platform the image offers. Absent on rentals created before images were pinned.
- `pinned_reference`, string, required: What a rental of this image is pinned to and the agent pulls. The tag is gone. An image of a mirrored registry is pulled from Legion's registry, `registry.legion.ambient.xyz/<upstream>/<path>@<digest>`, so this differs from `reference` in its registry and repository.
- `platforms`, array of Platform, required: The platforms the image offers, attestation entries left out.
  
- `platform`, Platform, required: The OCI platform of the machine a host's containers run on: `os/architecture` or `os/architecture/variant`, for example `linux/amd64`, `linux/arm64` or `linux/arm/v7`. It is what an image's `platforms` list is matched against. Reported by the agent; absent for hosts whose agent predates it or cannot tell.
- `compressed_size_bytes`, integer (int64), required: Total size of the image's layers for `platform`, as stored in the registry (compressed).
- `config`, ImageConfig, required: What the image itself declares it runs, from its config.
  - `entrypoint`, array of string, required: Empty when the image sets none.
  - `cmd`, array of string, required: Empty when the image sets none.
  - `user`, string, required: Empty when the image sets none, which means root.
  - `stop_signal`, string, required: For example `SIGTERM`. Empty when the image sets none.
- `registry_credential_id`, string (uuid): . Set when the image is private: the registry credential it was read with, which a rental of it would use. Absent for an image anyone can pull.

Example:

```json
{
  "reference": "docker.io/library/ubuntu:24.04",
  "digest": "sha256:3f2a9c1d5b7e8f40a6c3d2b19e0f7a845c6d1e2f3a4b5c6d7e8f9a0b1c2d3e4f",
  "pinned_reference": "registry.legion.ambient.xyz/docker.io/library/ubuntu@sha256:3f2a9c1d5b7e8f40a6c3d2b19e0f7a845c6d1e2f3a4b5c6d7e8f9a0b1c2d3e4f",
  "platforms": [
    "linux/amd64"
  ],
  "platform": "linux/amd64",
  "compressed_size_bytes": 0,
  "config": {
    "entrypoint": [
      "string"
    ],
    "cmd": [
      "string"
    ],
    "user": "string",
    "stop_signal": "string"
  },
  "registry_credential_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b"
}
```

### 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).

### 404

The resource does not exist or belongs to another organization.

### 422

The image cannot be run, and `detail` says why in plain words. `code` is
`image_not_found` (the registry has no such image or tag),
`image_private` (the registry asks for a sign-in, so the image is private
or does not exist; Docker Hub answers the same for both; Legion can only
run public images and images from its own registry),
`image_platform_unsupported` (the image offers no build for the host's
platform), `image_too_large` (compressed, it is bigger than the host's
free disk or than the platform's limit), `image_manifest_unsupported`
(an image format Legion cannot read, or an image with no runnable
platform), `image_registry_not_allowed` (not a public registry
reachable over https), `registry_not_supported` (the image is on a
registry Legion does not mirror: `detail` lists the supported ones),
`image_not_allowed` (a namespace Legion never mirrors),
`registry_sign_in_rejected` (: the registry refused the
organization's saved credential for it; `registry_host` names the
host), `registry_credential_not_found` or
`registry_credential_host_mismatch` (: the
`registry_credential_id` given is not the organization's, or is for
another host). On a registry
Legion takes sign-ins for, `image_private` carries `registry` and means
the organization has no sign-in for it yet. For an image of a mirrored registry
`image_too_large` also covers the whole image over every platform, more
than the mirror's cap. Nothing was started or allocated.

### 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 [`invalid_image_reference`](https://legion.ambient.xyz/docs/errors#invalid_image_reference)
- 403 [`no_organization`](https://legion.ambient.xyz/docs/errors#no_organization)
- 422 [`image_manifest_unsupported`](https://legion.ambient.xyz/docs/errors#image_manifest_unsupported)
- 422 [`image_not_allowed`](https://legion.ambient.xyz/docs/errors#image_not_allowed)
- 422 [`image_not_found`](https://legion.ambient.xyz/docs/errors#image_not_found)
- 422 [`image_platform_unsupported`](https://legion.ambient.xyz/docs/errors#image_platform_unsupported)
- 422 [`image_private`](https://legion.ambient.xyz/docs/errors#image_private)
- 422 [`image_registry_not_allowed`](https://legion.ambient.xyz/docs/errors#image_registry_not_allowed)
- 422 [`image_too_large`](https://legion.ambient.xyz/docs/errors#image_too_large)
- 422 [`registry_credential_host_mismatch`](https://legion.ambient.xyz/docs/errors#registry_credential_host_mismatch)
- 422 [`registry_credential_not_found`](https://legion.ambient.xyz/docs/errors#registry_credential_not_found)
- 422 [`registry_not_supported`](https://legion.ambient.xyz/docs/errors#registry_not_supported)
- 422 [`registry_sign_in_rejected`](https://legion.ambient.xyz/docs/errors#registry_sign_in_rejected)
- 429 [`mirror_quota_exceeded`](https://legion.ambient.xyz/docs/errors#mirror_quota_exceeded)
- 429 [`rate_limited`](https://legion.ambient.xyz/docs/errors#rate_limited)
- 503 [`image_registry_unavailable`](https://legion.ambient.xyz/docs/errors#image_registry_unavailable)

## Example request

```sh
curl -s -X POST "https://api.legion.ambient.xyz/v1/images/inspect" \
  -H "Authorization: Bearer $LEGION_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "reference": "string",
  "host_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
  "registry_credential_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b"
}'
```
