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

# Execute Command

> Execute a command on a specific Machine and return a JSON response containing
stdout, stderr, exit_code, and exit_signal.

A completed command returns HTTP 200, including when the command exits
unsuccessfully.

HTTP 412 is returned when the Machine is not running. On Machines without
containers, it can also indicate an error before or during command execution.
For example, the executable may be missing or lack execute permission. The
`error` field describes the failure.

HTTP 413 responses include a `status` field identifying the size limit that was
exceeded:

* `exec_request_too_large`: the Machine rejected the exec request as too large.
  The error message is `exec request rejected: payload too large`. The limit
  applies to the encoded request sent to the Machine, which can differ in size
  from the public API request.
* `exec_output_too_large`: the command ran, but its output could not be returned
  because it exceeded the maximum allowed response size after JSON encoding.




## OpenAPI

````yaml /api/machines/openapi.json post /v1/apps/{app_name}/machines/{machine_id}/exec
openapi: 3.1.0
info:
  title: Machines API
  description: >-
    This site hosts documentation generated from the Fly.io Machines API OpenAPI
    specification. Visit our complete [Machines API
    docs](https://fly.io/docs/machines/api/) for how to get started, more
    information about each endpoint, parameter descriptions, and examples.
  contact: {}
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: '1.0'
servers:
  - url: https://api.machines.dev
security:
  - FlyBearerAuth: []
tags:
  - name: Apps
    description: >-
      This site hosts documentation generated from the Fly.io Machines API
      OpenAPI specification. Visit our complete [Machines API
      docs](https://fly.io/docs/machines/api/apps-resource/) for details about
      using the Apps resource.
  - name: Machines
    description: >-
      This site hosts documentation generated from the Fly.io Machines API
      OpenAPI specification. Visit our complete [Machines API
      docs](https://fly.io/docs/machines/api/machines-resource/) for details
      about using the Machines resource.
  - name: TLS Certificates
    description: >
      This site hosts documentation generated from the Fly.io Machines API
      OpenAPI specification. Visit our complete [Machines API
      docs](https://fly.io/docs/machines/api/certificates-resource/) for details
      about using the TLS Certificates resource.
  - name: Volumes
    description: >-
      This site hosts documentation generated from the Fly.io Machines API
      OpenAPI specification. Visit our complete [Machines API
      docs](https://fly.io/docs/machines/api/volumes-resource/) for details
      about using the Volumes resource.
  - name: Postgres Clusters
    description: >-
      Create and manage Postgres clusters, including databases, users,
      extensions, backups, and app attachments.
externalDocs:
  url: https://fly.io/docs/machines/working-with-machines/
paths:
  /v1/apps/{app_name}/machines/{machine_id}/exec:
    post:
      tags:
        - Machines
      summary: Execute Command
      description: >
        Execute a command on a specific Machine and return a JSON response
        containing

        stdout, stderr, exit_code, and exit_signal.


        A completed command returns HTTP 200, including when the command exits

        unsuccessfully.


        HTTP 412 is returned when the Machine is not running. On Machines
        without

        containers, it can also indicate an error before or during command
        execution.

        For example, the executable may be missing or lack execute permission.
        The

        `error` field describes the failure.


        HTTP 413 responses include a `status` field identifying the size limit
        that was

        exceeded:


        * `exec_request_too_large`: the Machine rejected the exec request as too
        large.
          The error message is `exec request rejected: payload too large`. The limit
          applies to the encoded request sent to the Machine, which can differ in size
          from the public API request.
        * `exec_output_too_large`: the command ran, but its output could not be
        returned
          because it exceeded the maximum allowed response size after JSON encoding.
      operationId: Machines_exec
      parameters:
        - name: app_name
          in: path
          description: Fly App Name
          required: true
          schema:
            type: string
        - name: machine_id
          in: path
          description: Machine ID
          required: true
          schema:
            type: string
      requestBody:
        description: Request body
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MachineExecRequest'
        required: true
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ExecResponse'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '412':
          description: An error occurred before or during command execution
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '413':
          description: >-
            Size limit exceeded; status is exec_request_too_large or
            exec_output_too_large
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
components:
  schemas:
    MachineExecRequest:
      type: object
      properties:
        cmd:
          type: string
          description: >-
            Command line to run, split into arguments like a shell would. Takes
            precedence over

            `command`.


            Deprecated: use `command`.
        command:
          type: array
          description: >-
            Command to run, as the program followed by its arguments. It is run
            directly, not by a

            shell.
          items:
            type: string
        container:
          type: string
          description: >-
            Name of the container to run the command in, for Machines with
            containers.
        machine:
          type: boolean
          description: >-
            Run the command in the Machine's own namespace instead of in a
            container. Mutually

            exclusive with `container`.
        stdin:
          type: string
          description: Data to write to the command's standard input.
        timeout:
          type: integer
          description: >-
            How long to let the command run, in seconds. Defaults to 10; at most
            600.
    ExecResponse:
      type: object
      properties:
        exit_code:
          type: integer
          description: The command's exit code.
        exit_signal:
          type: integer
          description: The signal that terminated the command, or 0 if it exited normally.
        stderr:
          type: string
          description: The command's standard error.
        stdout:
          type: string
          description: The command's standard output.
    ErrorResponse:
      type: object
      properties:
        details:
          type: object
          description: 'Deprecated: details about some errors. Do not rely on its contents.'
        error:
          type: string
          description: >-
            Human-readable description of the error. Its wording may change; use
            `status` to tell

            errors apart.
        status:
          type: object
          description: >-
            Machine-readable error code, set for some errors, such as
            `insufficient_capacity`,

            `volume_placement_capacity`, `name_taken`, `exec_request_too_large`,
            or

            `exec_output_too_large`.
          allOf:
            - $ref: '#/components/schemas/main.statusCode'
    main.statusCode:
      type: string
      enum:
        - unknown
        - insufficient_capacity
        - volume_placement_capacity
        - exec_request_too_large
        - exec_output_too_large
        - name_taken
      x-enum-varnames:
        - unknown
        - capacityErr
        - volumePlacementCapacityErr
        - execRequestTooLargeErr
        - execOutputTooLargeErr
        - nameTakenErr
  securitySchemes:
    FlyBearerAuth:
      type: http
      scheme: bearer
      description: >-
        A Fly API token with access to the requested organization or resource.
        Send it as `Authorization: Bearer <Fly API token>`.

````