# Schedule maintenance on one or many hosts

> POST /v1/maintenance-windows. .

Source: https://legion.ambient.xyz/docs/api/hosts/schedule-maintenance

`POST https://api.legion.ambient.xyz/v1/maintenance-windows`

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

. Schedules one window, `start` for `duration_minutes`, on each of up to 100 of the organization's hosts, in
one request. The start is on a whole minute, 5 minutes to 90 days ahead (never in the past), and the duration
15 minutes to 72 hours.

**What renters get.** Each renter organization with a rental on a host is told (feed and one grouped email per
person listing every host and rental), reminded 24 hours and 1 hour before the start, told if the window moves
or is cancelled, and told when it ends and what restarted. Nobody is told for a host with no rentals. At the
start, its rentals are stopped (`stop_reason` `host_maintenance`, their files kept, nothing is deleted) after a
5-minute notice on agents that support it, and start again on the same host when the window ends, unless the
renter opted out. No GPU or storage is charged while a host is in maintenance.

**Penalty-free.** Decided once, five minutes before the start, from the rentals the window stops: at least 72
hours of notice if any is on-demand, 1 hour if all are interruptible, any notice if none. A penalty-free window's
minutes are excused in the host's uptime score, up to 72 hours per host per 30 days; overrun minutes and the
minutes of a window with too little notice count as downtime. `previewMaintenance` says first.

**While it is scheduled.** New rentals of a tier are refused on the host inside the tier's notice before the start
(409 `host_maintenance`) and every start is refused from 5 minutes before the start until the window ends; offers
show only `available_until`, the start.

**Limits.** At most one open window per host; 4 windows created per host per 30 days (cancelled ones count); 30
scheduling and rescheduling requests per organization per hour, and apart from them 120 cancelling and ending
requests (429 `rate_limited`): a spent schedule budget never keeps a provider from ending or cancelling a window.

**Bulk is all or nothing for ownership.** A host that does not exist or is not the organization's makes the whole
request 404 and nothing is written. A host the organization owns but cannot schedule now is skipped, and says why
in `skipped` (`host_removed`, `host_removing`, `maintenance_exists`, `maintenance_quota`); the rest are scheduled.
**A request whose every host is skipped is still 200**, with `windows` empty: a script succeeded for a host only
when `windows` has a window with its `host_id` (`windows` is ordered by host id, not request order).

**Errors.** A body the schema refuses (a duration outside 15 to 4320 minutes, an unknown `reason`, more than 100
hosts) is 400 `invalid_request`; a start in the past, less than 5 minutes or more than 90 days ahead, or not on a
whole minute is 400 `invalid_maintenance_window`. A `note` over 280 characters is 422 `maintenance_note_too_long`
(it is never cut). `Idempotency-Key` is optional: a retry with the same key and body returns the same batch, even
once the start is inside the 5 minutes and without using up the hourly limit; the same key with another body is
422 `idempotency_key_reused`. Needs the `hosts.manage` permission (an organization owner). Host ids come from
`listHosts`.

Example: maintenance on two hosts tomorrow at 02:00 UTC for two hours:
`curl -X POST https://api.legion.ambient.xyz/v1/maintenance-windows -H "Authorization: Bearer $KEY" -H "Idempotency-Key: psu-swap-1"
-d '{"host_ids":["<host>","<host>"],"start":"2026-12-02T02:00:00Z","duration_minutes":120,"reason":"power"}'`.

## Headers

- `Idempotency-Key`, string: Client-chosen key, unique per request intent, at most 255 characters.

## Request body

- `host_ids`, array of string (uuid), required: The organization's hosts (from `listHosts`), 1 to 100. One the organization does not own makes the whole request 404.
- `start`, string (date-time), required: RFC 3339, on a whole minute, 5 minutes to 90 days from now.
- `duration_minutes`, integer, required: The planned length in minutes, 15 to 4320 (72 hours).
- `reason`, MaintenanceReason, required: . Why a provider takes a host down, shown to the renters of rentals on it. The list grows: accept a value you do not know.
- `note`, string: Free text for the provider's own records, at most 280 characters after cleaning (control characters removed); longer is 422 `maintenance_note_too_long`, never cut. Renters never see it.

Example:

```json
{
  "host_ids": [
    "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b"
  ],
  "start": "2026-10-15T12:00:00Z",
  "duration_minutes": 15,
  "reason": "hardware",
  "note": "string"
}
```

## Responses

### 200

The windows scheduled and the hosts skipped.

- `batch_id`, string (uuid): The batch `scheduleMaintenance` created.
- `windows`, array of MaintenanceWindow, required: The windows the request scheduled or acted on, as they are now, ordered by host id (schedule) or window id. Empty when every item was skipped (still 200).
  - `id`, string (uuid), required: The window.
  - `host_id`, string (uuid), required: The host it takes down.
  - `host_name`, string, required: The host's name (or hostname), as the provider sees it.
  - `batch_id`, string (uuid), required: The `scheduleMaintenance` request that created it, shared by the windows of one bulk schedule.
  - `state`, MaintenanceState, required: . `scheduled` (announced, renters told), `closing` (the last 5 minutes before the start: the penalty-free outcome is decided, nothing can start, rentals in their notice), `active` (the host is in maintenance, its rentals stopped), `overrun` (the planned end passed and the host has not synced back), `completed`, `cancelled`. The list grows: accept a value you do not know.
  - `reason`, MaintenanceReason, required: . Why a provider takes a host down, shown to the renters of rentals on it. The list grows: accept a value you do not know.
  - `note`, string: The provider's own note, cleaned (no control characters, 280 characters). Only the provider sees it; it is in no email and no renter's view.
  - `start`, string (date-time), required: The planned start (what renters were told). The actual start is `started_at`.
  - `planned_end`, string (date-time), required: The planned end. Never edited by an early end or an overrun; the actual end is `ended_at`.
  - `ended_at`, string (date-time): When the window actually ended: "done with maintenance" with the host online, the planned end with the host online, or the host's return (after an overrun, or after "done with maintenance" while the host was still away). Absent while it is open. The plan is `start` and `planned_end`; what happened is `started_at` and `ended_at`.
  - `end_requested_at`, string (date-time): When "done with maintenance" (`endMaintenance`, or Legion's end) was asked for. The uptime excuse stops here. While the host is still away the window stays open (`overrun`) until it is back, and `ended_at` is that return.
  - `scheduled_at`, string (date-time), required: When it was scheduled (it counts toward the host's 4 windows per 30 days from then).
  - `scheduled_by`, string (uuid): The person who scheduled it; absent when an API key did.
  - `notice_from`, string (date-time), required: Where the notice counts from: when it was scheduled, or when a reschedule moved it earlier or made it longer.
  - `reschedules_left`, integer, required: How many more times its start or end may be changed (a reason or note change does not count).
  - `penalty_free`, boolean, required: While `scheduled`, a forecast from the rentals now on the host; from `closing`, the decided outcome (`penalty_free_final` true).
  - `penalty_free_final`, boolean, required: Whether `penalty_free` is decided (from `closing` on) rather than a forecast. A window cancelled before closing is never decided: `false`.
  - `required_notice_seconds`, integer, required: 72 hours if any rental the window stops is on-demand, 1 hour if all are interruptible, 0 for none (and for a window cancelled before it was decided).
  - `notice_seconds`, integer, required: What the renters have, the start minus `notice_from`.
  - `earliest_penalty_free_start`, string (date-time): While `scheduled`, the earliest start that would be penalty-free for the rentals now on the host.
  - `rentals`, MaintenanceTierCounts, required: The rentals the window stops (decided at `closing`, a forecast before), by tier. Counts only: never a renter.
  - `may_restart`, integer, required: Stopped rentals with files that their renters could still start before the window closes. A forecast, not in `rentals`: if one is started, it counts at closing, and a window that is penalty-free now may not be then (an on-demand rental needs 72 hours). 0 for a completed or cancelled window.
  - `renters_notified`, integer, required: The renter organizations with a rental on the host now: told of the window, and told again if it is changed or cancelled. Counts only, never a renter; 0 for a completed or cancelled window.
  - `started_at`, string (date-time): When it actually became `active` (the sweep, at or just after `start`). Absent before.
  - `cancelled_at`, string (date-time): When it was cancelled. Absent unless `cancelled`.
  - `cancel_cause`, MaintenanceCancelCause: Who cancelled it: the provider, Legion, or the host's removal.
  - `end_cause`, MaintenanceEndCause: How it ended: at its planned end, ended early by the provider, by the host's return after an overrun, by the host's removal, or by Legion.
  - `overrun_seconds`, integer: How long after the planned end the host stayed away.
  - `excused_seconds`, integer, required: The host's uptime-score minutes this window excused so far, in seconds.
- `skipped`, array of MaintenanceSkip, required: The owned hosts or windows the request left alone, with why.
  - `id`, string (uuid), required: The host (schedule, preview) or window (reschedule, cancel, end).
  - `code`, string, required: Why it was left alone. The list grows: accept a value you do not know. `host_removed`: the host was removed. `host_removing`: the host is being removed. `maintenance_exists`: the host already has an open window. `maintenance_quota`: the host had 4 windows in 30 days (cancelled ones count). `maintenance_reschedule_limit`: the window's start or end was changed 3 times. `maintenance_not_open`: the window is completed or cancelled. `maintenance_started`: the window is closing, active or in overrun (too late to change or cancel it). `maintenance_not_active`: the window has not started (cancel it instead of ending it). `invalid_maintenance_window`: the new start or duration breaks the rules of `scheduleMaintenance`. `maintenance_retry`: a cancel or end of this window could not be done now (each window is changed in its own transaction, so the others named are done); nothing of this window was written, try it again. `maintenance_failed`: a cancel or end of this window failed for another reason; nothing of it was written, the others named are done, and Legion logged the error (try again later or contact support).
  - `detail`, string: The reason in words, for a person.

Example:

```json
{
  "batch_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
  "windows": [
    {
      "id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
      "host_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
      "host_name": "string",
      "batch_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
      "state": "scheduled",
      "reason": "hardware",
      "note": "string",
      "start": "2026-10-15T12:00:00Z",
      "planned_end": "2026-10-15T12:00:00Z",
      "ended_at": "2026-10-15T12:00:00Z",
      "end_requested_at": "2026-10-15T12:00:00Z",
      "scheduled_at": "2026-10-15T12:00:00Z",
      "scheduled_by": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
      "notice_from": "2026-10-15T12:00:00Z",
      "reschedules_left": 0,
      "penalty_free": false,
      "penalty_free_final": false,
      "required_notice_seconds": 0,
      "notice_seconds": 0,
      "earliest_penalty_free_start": "2026-10-15T12:00:00Z",
      "rentals": {
        "on_demand": 0,
        "interruptible": 0
      },
      "may_restart": 0,
      "renters_notified": 0,
      "started_at": "2026-10-15T12:00:00Z",
      "cancelled_at": "2026-10-15T12:00:00Z",
      "cancel_cause": "provider",
      "end_cause": "on_time",
      "overrun_seconds": 0,
      "excused_seconds": 0
    }
  ],
  "skipped": [
    {
      "id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
      "code": "host_removed",
      "detail": "string"
    }
  ]
}
```

### 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 Idempotency-Key was already used with a different request body.

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

## Errors

- 400 [`invalid_maintenance_window`](https://legion.ambient.xyz/docs/errors#invalid_maintenance_window)
- 403 [`no_organization`](https://legion.ambient.xyz/docs/errors#no_organization)
- 422 [`idempotency_key_reused`](https://legion.ambient.xyz/docs/errors#idempotency_key_reused)
- 422 [`maintenance_note_too_long`](https://legion.ambient.xyz/docs/errors#maintenance_note_too_long)
- 429 [`rate_limited`](https://legion.ambient.xyz/docs/errors#rate_limited)

## Example request

```sh
curl -s -X POST "https://api.legion.ambient.xyz/v1/maintenance-windows" \
  -H "Authorization: Bearer $LEGION_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
  "host_ids": [
    "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b"
  ],
  "start": "2026-10-15T12:00:00Z",
  "duration_minutes": 15,
  "reason": "hardware",
  "note": "string"
}'
```
