---
title: "Run a command and stream its output."
method: POST
path: "/v1/machines/{id}/exec/stream"
tags: ["machines"]
---

# Run a command and stream its output.

`POST /v1/machines/{id}/exec/stream`

## Request

- Base URL: `https://api.smolmachines.com`
- URL: `https://api.smolmachines.com/v1/machines/{id}/exec/stream`
- Auth: HTTP bearer
- TypeScript: <https://skmtc.dev/smolmachines/apis/smolfleet-api/docs/v1/machines/:id/exec/stream/post.ts> (a function with typed inputs; its response is checked against the schema)

Runs a command and returns stdout, stderr, and the exit code as Server-Sent Events. Requires the `machine:exec` scope.

## Path parameters

- `id` string, required

## Request body

- MachineCommandRequest
  - `background` boolean: Run the command detached: spawn it in the background and return its PID immediately. Use for long-lived daemons (dev server, agent runner).
  - `command` union, required
    - string[]
    - string
  - `cwd` string, nullable
  - `env` object
  - `stdin` string, nullable
  - `stream` boolean: Stream stdout and stderr as Server-Sent Events instead of buffering the whole run into one JSON body. The response is then `text/event-stream`, not `MachineExecResponse`: frames are `event: stdout|stderr|error|exit` with `data:` lines, and the terminal `exit` frame carries `{"exitCode": N}`. Streamed output is not capped, so this is the right path for long-running or high-volume commands.
  - `timeoutSeconds` integer, nullable
  - `user` string, nullable: Run as this user: a name from the image or a numeric `uid[:gid]`, overriding the image's USER. Forwarded to the machine's runtime; an older runtime that cannot honour it rejects the request rather than running the command as the wrong account.

## Response `200`

Command output as Server-Sent Events, written as it is produced and never truncated. Frames are `event: stdout|stderr|error|exit` with `data:` lines; the terminal `exit` frame carries `{"exitCode": N}`. Identical to `POST /v1/machines/{id}/exec` with `stream: true` — a dedicated path for clients that route by URL rather than by request body.

## Other responses

- `401`: Missing or invalid API key
- `403`: Key lacks the required scope
- `404`: Not found
- `429`: Rate limited; retry after the Retry-After header
- `500`: Internal error

## Changes

- **2026-10-16** `bc31fc04a568`: 1 info
  - endpoint added

[Operation history](https://skmtc.dev/smolmachines/apis/smolfleet-api/changes/v1/machines/:id/exec/stream/post.md)

---

[API](https://skmtc.dev/smolmachines/apis/smolfleet-api.md) - [All operations](https://skmtc.dev/smolmachines/apis/smolfleet-api/llms.txt) - [OpenAPI document](https://skmtc.dev/smolmachines/apis/smolfleet-api/revisions/bc31fc04a568?raw)
