> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mirage.strukto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Run shell command

> Run a shell command or script in the workspace. Check `exit_code` to determine whether it succeeded.

Set `stream=true` for live output. Add `open_stdin=true` to send more input while the command runs. See [Streaming and stdin](/api/streaming) for output formats and input examples.

```json 200
{
  "exit_code": 0,
  "stdout": "hello\n",
  "stderr": "",
  "refusal": null,
  "line_id": "line_019a5e2c-7b41-7c3e-9f20-4d8e1a6b5c37"
}
```

```json 200 Stream with open_stdin
{"line_id":"line_019a5e2c-7b41-7c3e-9f20-4d8e1a6b5c37"}
{"stream":"stdout","data":"aGVsbG8K"}
{"status":"done","result":{"exit_code":0,"stdout":"","stderr":"","refusal":null,"line_id":"line_019a5e2c-7b41-7c3e-9f20-4d8e1a6b5c37"},"error":null}
```

> [accordion] Errors
>
> | Status | Meaning |
> | - | - |
> | `401` | The token is missing or wrong |
> | `403` | A policy or mount mode refused the call |
> | `404` | The workspace, session, job or file does not exist |
> | `409` | Another server owns command execution for this workspace. The error detail identifies that server. |
> | `422` | Validation Error |
> | `429` | Past a rate limit, or a new session past max\_sessions |
> | `413` | The request body, multipart part, or stdin exceeds its size limit. |
> | `499` | The running shell job was canceled. |
> | `500` | The job or server operation failed internally. Inspect detail and server logs before retrying. |
>
> See [error examples and recovery steps](https://docs.mirage.strukto.ai/api/errors.md). The [complete OpenAPI contract](https://github.com/strukto-ai/mirage/blob/main/docs/openapi.yaml) includes every response schema.


## OpenAPI

````yaml /openapi.yaml post /v1/workspaces/{workspace_id}/shell
openapi: 3.2.0
info:
  title: Mirage API
  version: '0.1'
servers:
  - url: http://127.0.0.1:8765
    description: A local server on its default port
security:
  - bearerAuth: []
tags:
  - name: Workspace operations
    x-group: Workspaces
  - name: Workspaces
    x-group: Lifecycle
    parent: Workspace operations
  - name: Sessions
  - name: Shell
    parent: Workspace operations
  - name: VFS
    parent: Workspace operations
  - name: Tools
  - name: Documents
    x-group: Agent Instructions
    parent: Workspace operations
  - name: Snapshots
  - name: Templates
    parent: Configuration
  - name: Saved VFS
    x-group: VFS
    parent: Configuration
  - name: Saved CLIs
    x-group: CLI
    parent: Configuration
  - name: Saved runtimes
    x-group: Runtime
    parent: Configuration
  - name: Saved machines
    x-group: Machine
    parent: Configuration
  - name: Configuration
  - name: Policies
    parent: Configuration
  - name: Asks
    parent: Workspace operations
    x-group: Approval requests
  - name: Lines
    parent: Workspace operations
    x-group: Commands
  - name: Jobs
    parent: Workspace operations
  - name: Processes
    parent: Workspace operations
  - name: Machines
    parent: Workspace operations
  - name: Server
  - name: Service
    parent: Server
  - name: Daemon
    parent: Server
  - name: Auth
paths:
  /v1/workspaces/{workspace_id}/shell:
    post:
      tags:
        - Shell
      summary: Run shell command
      description: >-
        Run a shell command or script in the workspace. Check `exit_code` to
        determine whether it succeeded.


        Set `stream=true` for live output. Add `open_stdin=true` to send more
        input while the command runs. See [Streaming and stdin](/api/streaming)
        for output formats and input examples.
      operationId: shell_v1_workspaces__workspace_id__shell_post
      parameters:
        - $ref: '#/components/parameters/workspaceId'
        - $ref: '#/components/parameters/sessionId'
        - name: explain
          in: query
          description: Return what the call would do without running it.
          required: false
          schema:
            type: boolean
            default: false
        - name: stream
          in: query
          description: >-
            Stream stdout, stderr and the final status as NDJSON. Cannot be
            combined with explain.
          required: false
          schema:
            type: boolean
            default: false
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ShellRequest'
            example:
              command: echo hello
          multipart/form-data:
            schema:
              type: object
              properties:
                request:
                  $ref: '#/components/schemas/ShellRequest'
                stdin:
                  type: string
                  format: binary
                  description: Up to 64 MiB, read as it arrives.
              required:
                - request
      responses:
        '200':
          description: >-
            Command result, policy evaluation when `explain=true`, or streamed
            output ending with a status record when `stream=true`. With
            `open_stdin`, the first record contains `line_id`.
          content:
            application/json:
              schema:
                anyOf:
                  - $ref: '#/components/schemas/ShellResult'
                  - $ref: '#/components/schemas/ShellExplanation'
              example:
                exit_code: 0
                stdout: |
                  hello
                stderr: ''
                refusal: null
                line_id: line_019a5e2c-7b41-7c3e-9f20-4d8e1a6b5c37
            application/x-ndjson:
              schema:
                type: string
              examples:
                open_stdin:
                  summary: Stream with open_stdin
                  value: >
                    {"line_id":"line_019a5e2c-7b41-7c3e-9f20-4d8e1a6b5c37"}

                    {"stream":"stdout","data":"aGVsbG8K"}

                    {"status":"done","result":{"exit_code":0,"stdout":"","stderr":"","refusal":null,"line_id":"line_019a5e2c-7b41-7c3e-9f20-4d8e1a6b5c37"},"error":null}
      x-codeSamples:
        - lang: bash
          label: Keep stdin open
          source: |
            curl --no-buffer --request POST \
              --url 'http://127.0.0.1:8765/v1/workspaces/{workspace_id}/shell?stream=true' \
              --header 'Authorization: Bearer <token>' \
              --header 'Content-Type: application/json' \
              --data '{"command":"cat","open_stdin":true}'
components:
  parameters:
    workspaceId:
      name: workspace_id
      in: path
      required: true
      description: Workspace ID.
      schema:
        type: string
      example: demo
    sessionId:
      name: session_id
      in: query
      required: false
      description: Session ID to use. If omitted, the default session is used.
      schema:
        type: string
      example: analysis
  schemas:
    ShellRequest:
      type: object
      properties:
        command:
          type: string
          description: Shell command or script to execute.
        cwd:
          type: string
          description: Working directory for this command only.
        runtime:
          type: string
          description: Runtime name to use instead of automatic runtime selection.
        machine:
          type: string
          description: >-
            Machine name to use for this command instead of automatic machine
            selection.
        record:
          type: boolean
          default: true
          description: Include this command in session history.
        open_stdin:
          type: boolean
          default: false
          description: >-
            Keep stdin open for additional input. Requires `stream=true`; the
            first response record contains `line_id`.
      required:
        - command
    ShellResult:
      type: object
      properties:
        exit_code:
          type: integer
        stdout:
          type: string
        stderr:
          type: string
        refusal:
          anyOf:
            - type: object
            - type: 'null'
        line_id:
          type: string
          description: >-
            Command execution ID. Use it with `GET
            /v1/workspaces/{workspace_id}/lines/{line_id}`.
      required:
        - exit_code
        - stdout
        - stderr
        - line_id
      title: Shell result
    ShellExplanation:
      allOf:
        - $ref: '#/components/schemas/Explanation'
        - type: object
          properties:
            line:
              type: string
            exit_code:
              type: integer
              description: What the line is refused with, 0 when it runs.
            stderr:
              type: string
            node:
              type: object
              description: Parsed command structure and policy evaluation for each command.
      title: Shell explanation
    Explanation:
      type: object
      description: >-
        Policy evaluation for a request without executing it. Returned when
        `explain=true`.
      properties:
        type:
          type: string
          description: line, command or vfs.
        outcome:
          type: string
          enum:
            - ALLOW
            - DENY
            - ASK
        reason:
          type: string
          description: Reason returned by the policy that determined the outcome.
        source:
          type: string
          description: Where in the profile the deciding rule is written.
        answers:
          type: array
          items:
            type: object
          description: Evaluation results from each policy.
        refusal:
          anyOf:
            - type: object
            - type: 'null'
      required:
        - outcome
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: >-
        The server's token (MIRAGE_AUTH_TOKEN) or a JWT. A daemon in the default
        local mode with no token accepts requests without one.

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.