# Export a stopped rental's workspace

> POST /v1/rentals/{rental_id}/workspace-exports. Asks the host's agent to upload the workspace of a stopped rental (/workspace in the container) as one tar file, so the data of a rental that was…

Source: https://legion.ambient.xyz/docs/api/rentals/create-workspace-export

`POST https://api.legion.ambient.xyz/v1/rentals/{rental_id}/workspace-exports`

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

Asks the host's agent to upload the workspace of a `stopped` rental (`/workspace` in the container) as one
tar file, so the data of a rental that was preempted can be taken before it is deleted. Answers
202 with the export, `pending`; poll `getWorkspaceExport` until it is `ready`, then make a single-use download link for the tar with
`createWorkspaceExportDownloadLink` (charged; see `estimateWorkspaceExport`). The host's agent sees the request on its next sync (`DesiredRental.workspace_export`, within
`poll_interval_seconds`) and needs `preservation_v1`. The file is kept for 24 hours.

The rental must be `stopped` and its host online (409 `rental_not_stopped`, 409 `host_offline`); 409
`host_unsupported` when the agent cannot export; 409 `rental_held` for a rental on a preservation hold; 409 `export_in_progress` while an export of the rental is pending, with the pending export's ID
as `resource_id` when it is still pending as the answer is made (one pending for more than 24 hours is failed and replaced); 429 `rate_limited` after `exports_per_hour` exports an hour for the organization (3 by default, `getStoragePolicy`); 503
`exports_unavailable` when Legion has no export bucket. A rental with an export pending cannot be
started (409 `export_in_progress`) and is not started by auto-resume. Storage keeps being billed while it
is exported. Requires `rentals.manage`.

## Path parameters

- `rental_id`, string (uuid), required

## Responses

### 202

The export, pending.

- `id`, string (uuid), required
- `state`, WorkspaceExportState, required: `pending`: the agent has not finished uploading. `ready`: download it. `failed`: see `error`. `cancelled`: a member cancelled it while it was pending; nothing was kept.
- `size_bytes`, integer (int64): The size of the tar file; with `ready`.
- `download_url`, string: Removed in: never present. A ready export is downloaded through a single-use, charged link: `createWorkspaceExportDownloadLink`. Kept in the schema so generated clients keep compiling.
- `expires_at`, string (date-time): Removed in: never present (it was when `download_url` stopped working).
- `error`, string: Why a `failed` export failed.
- `total_bytes`, integer (int64): . The size the tar file is expected to have, measured by the host's agent (capability `workspace_size_v1`) while it uploads: the apparent size of every file under `/workspace` plus the tar headers. Absent until the agent has measured it. An estimate: files the workload changed, or that the engine adds, can make the file a little larger or smaller.
- `uploaded_bytes`, integer (int64): . With `pending`: the bytes of the parts the agent has finished uploading (0 before the first); the part in flight is not counted. With `ready`: `size_bytes`.
- `upload_rate_bytes_per_second`, integer (int64): . With `pending`: how fast the host has been uploading, in bytes per second, over its last 5 parts (fewer at the start). Absent until the agent has finished its first part.
- `estimated_completion_at`, string (date-time): . With `pending`, `total_bytes` and `upload_rate_bytes_per_second`: when the upload should finish at that rate. It may be more than 24 hours after `requested_at`; such an export fails at 24 hours unless the host speeds up.

Example:

```json
{
  "id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
  "state": "pending",
  "size_bytes": 0,
  "download_url": "string",
  "expires_at": "2026-10-15T12:00:00Z",
  "error": "string",
  "total_bytes": 0,
  "uploaded_bytes": 0,
  "upload_rate_bytes_per_second": 0,
  "estimated_completion_at": "2026-10-15T12:00:00Z"
}
```

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

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

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

- 403 [`no_organization`](https://legion.ambient.xyz/docs/errors#no_organization)
- 409 [`export_in_progress`](https://legion.ambient.xyz/docs/errors#export_in_progress)
- 409 [`host_offline`](https://legion.ambient.xyz/docs/errors#host_offline)
- 409 [`host_unsupported`](https://legion.ambient.xyz/docs/errors#host_unsupported)
- 409 [`rental_held`](https://legion.ambient.xyz/docs/errors#rental_held)
- 409 [`rental_not_stopped`](https://legion.ambient.xyz/docs/errors#rental_not_stopped)
- 409 [`rental_managed_by_service`](https://legion.ambient.xyz/docs/errors#rental_managed_by_service)
- 429 [`rate_limited`](https://legion.ambient.xyz/docs/errors#rate_limited)
- 503 [`exports_unavailable`](https://legion.ambient.xyz/docs/errors#exports_unavailable)

## Example request

```sh
curl -s -X POST "https://api.legion.ambient.xyz/v1/rentals/$RENTAL_ID/workspace-exports" \
  -H "Authorization: Bearer $LEGION_API_KEY"
```
