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

# Run a command

> Runs a command in a running sandbox and streams its output as NDJSON until the command exits. A command has no time limit unless you send `timeoutSeconds`. If you disconnect, the command gets SIGKILL. Send `detached: true` to get the command at once and keep it running after you disconnect. Needs the `sandboxes:exec` scope.



## OpenAPI

````yaml /api-reference/openapi.json post /v1/sandboxes/{id}/commands
openapi: 3.1.0
info:
  title: VMS API
  version: 1.0.0
  description: >-
    The public API of a VMS workspace. Each request needs an API key of the
    workspace in the Authorization header.
servers:
  - url: https://vms.dev/api
    description: The VMS API
security:
  - apiKey: []
paths:
  /v1/sandboxes/{id}/commands:
    post:
      tags:
        - Commands
      summary: Run a command
      description: >-
        Runs a command in a running sandbox and streams its output as NDJSON
        until the command exits. A command has no time limit unless you send
        `timeoutSeconds`. If you disconnect, the command gets SIGKILL. Send
        `detached: true` to get the command at once and keep it running after
        you disconnect. Needs the `sandboxes:exec` scope.
      operationId: createCommand
      parameters:
        - in: path
          name: id
          schema:
            type: string
            example: sbx_06d8k3m7q2v9x4b1n5c0e8f2ja
          required: true
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateCommandBody'
      responses:
        '200':
          description: >-
            The output of the command. Each line is one JSON event: `started`,
            then `stdout` and `stderr`, then `exit`. Each event has a `seq` that
            starts at 0. When no event comes for 15 seconds, the stream sends an
            empty line. Skip empty lines.
          content:
            application/x-ndjson:
              schema:
                $ref: '#/components/schemas/CommandEvent'
        '201':
          description: The detached command.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Command'
        '400':
          description: The request is not valid.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: The API key is missing, not correct, revoked, or expired.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '403':
          description: The API key does not have the scope of the endpoint.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '404':
          description: >-
            The workspace has no sandbox with this ID, or the sandbox has no
            command with this ID.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '409':
          description: >-
            The sandbox is not running (`sandbox_not_running`), is starting
            (`sandbox_starting`), or is stopping (`sandbox_stopping`).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '429':
          description: The API key sent more than 1,200 requests in one minute.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '503':
          description: Could not connect to the sandbox.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    CreateCommandBody:
      type: object
      properties:
        command:
          anyOf:
            - type: string
              minLength: 1
            - minItems: 1
              type: array
              items:
                type: string
          description: >-
            A shell command as one string, which runs with `bash -lc`, or the
            program and its arguments as a list.
          example: npm test
        cwd:
          $ref: '#/components/schemas/AbsolutePath'
          description: The working folder. The default is the home folder of root.
          example: /home/user/app
        env:
          $ref: '#/components/schemas/Environment'
          description: Environment variables to add for this command.
        timeoutSeconds:
          description: >-
            The time limit in seconds. At the limit, the command gets SIGKILL.
            Leave it out or send null for no time limit.
          anyOf:
            - type: integer
              exclusiveMinimum: 0
              maximum: 9007199254740991
            - type: 'null'
        stdin:
          description: >-
            Keep stdin open, so that you can send input with POST
            /v1/sandboxes/{id}/commands/{commandId}/stdin.
          type: boolean
        detached:
          description: >-
            Answer 201 with the command at once. The command runs after you
            disconnect.
          type: boolean
      required:
        - command
    CommandEvent:
      anyOf:
        - $ref: '#/components/schemas/CommandStartedEvent'
        - $ref: '#/components/schemas/CommandOutputEvent'
        - $ref: '#/components/schemas/CommandExitEvent'
    Command:
      type: object
      properties:
        id:
          type: string
          example: cmd_01jz8x3n9q2v7k4m6p8r0t2w4y
        sandboxId:
          type: string
        argv:
          minItems: 1
          type: array
          items:
            type: string
        cwd:
          anyOf:
            - $ref: '#/components/schemas/AbsolutePath'
            - type: 'null'
        status:
          type: string
          enum:
            - running
            - exited
        pid:
          anyOf:
            - type: integer
              exclusiveMinimum: 0
              maximum: 9007199254740991
            - type: 'null'
        detached:
          type: boolean
        startedAt:
          type: string
          format: date-time
          pattern: >-
            ^(?:(?:\d\d[2468][048]|\d\d[13579][26]|\d\d0[48]|[02468][048]00|[13579][26]00)-02-29|\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\d|30)|(?:02)-(?:0[1-9]|1\d|2[0-8])))T(?:(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:Z|([+-](?:[01]\d|2[0-3]):[0-5]\d)))$
        exitedAt:
          anyOf:
            - type: string
              format: date-time
              pattern: >-
                ^(?:(?:\d\d[2468][048]|\d\d[13579][26]|\d\d0[48]|[02468][048]00|[13579][26]00)-02-29|\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\d|30)|(?:02)-(?:0[1-9]|1\d|2[0-8])))T(?:(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:Z|([+-](?:[01]\d|2[0-3]):[0-5]\d)))$
            - type: 'null'
        exitCode:
          anyOf:
            - type: integer
              minimum: -9007199254740991
              maximum: 9007199254740991
            - type: 'null'
        signal:
          anyOf:
            - $ref: '#/components/schemas/Signal'
            - type: 'null'
          description: The signal that stopped the command, or null.
        oomKilled:
          type: boolean
          description: true when the command ran out of memory.
        timedOut:
          type: boolean
          description: true when the command stopped at its time limit.
        lastSeq:
          type: integer
          minimum: 0
          maximum: 9007199254740991
          description: The seq of the newest event of the command.
      required:
        - id
        - sandboxId
        - argv
        - cwd
        - status
        - pid
        - detached
        - startedAt
        - exitedAt
        - exitCode
        - signal
        - oomKilled
        - timedOut
        - lastSeq
      additionalProperties: false
    Error:
      type: object
      properties:
        error:
          type: object
          properties:
            code:
              type: string
              example: no_capacity
            message:
              type: string
            requestId:
              type:
                - string
                - 'null'
            retryAfterSeconds:
              type: integer
              minimum: 0
              maximum: 9007199254740991
            sandboxId:
              description: The sandbox that the error is about.
              type: string
          required:
            - code
            - message
            - requestId
          additionalProperties: false
      required:
        - error
      additionalProperties: false
    AbsolutePath:
      type: string
      minLength: 1
      maxLength: 4096
      pattern: ^\/
    Environment:
      type: object
      propertyNames:
        type: string
        pattern: ^[A-Za-z_][A-Za-z0-9_]*$
      additionalProperties:
        type: string
    CommandStartedEvent:
      type: object
      properties:
        type:
          type: string
          const: started
        seq:
          type: integer
          minimum: 0
          maximum: 9007199254740991
        at:
          type: string
          format: date-time
          pattern: >-
            ^(?:(?:\d\d[2468][048]|\d\d[13579][26]|\d\d0[48]|[02468][048]00|[13579][26]00)-02-29|\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\d|30)|(?:02)-(?:0[1-9]|1\d|2[0-8])))T(?:(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:Z|([+-](?:[01]\d|2[0-3]):[0-5]\d)))$
        commandId:
          $ref: '#/components/schemas/RecordId'
        pid:
          type: integer
          exclusiveMinimum: 0
          maximum: 9007199254740991
      required:
        - type
        - seq
        - at
        - commandId
        - pid
      additionalProperties: false
    CommandOutputEvent:
      type: object
      properties:
        type:
          type: string
          enum:
            - stdout
            - stderr
        seq:
          type: integer
          minimum: 0
          maximum: 9007199254740991
        data:
          type: string
        encoding:
          $ref: '#/components/schemas/Encoding'
      required:
        - type
        - seq
        - data
        - encoding
      additionalProperties: false
    CommandExitEvent:
      type: object
      properties:
        type:
          type: string
          const: exit
        seq:
          type: integer
          minimum: 0
          maximum: 9007199254740991
        at:
          type: string
          format: date-time
          pattern: >-
            ^(?:(?:\d\d[2468][048]|\d\d[13579][26]|\d\d0[48]|[02468][048]00|[13579][26]00)-02-29|\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\d|30)|(?:02)-(?:0[1-9]|1\d|2[0-8])))T(?:(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:Z|([+-](?:[01]\d|2[0-3]):[0-5]\d)))$
        exitCode:
          anyOf:
            - type: integer
              minimum: -9007199254740991
              maximum: 9007199254740991
            - type: 'null'
        signal:
          anyOf:
            - $ref: '#/components/schemas/Signal'
            - type: 'null'
        oomKilled:
          type: boolean
        timedOut:
          type: boolean
      required:
        - type
        - seq
        - at
        - exitCode
        - signal
        - oomKilled
        - timedOut
      additionalProperties: false
    Signal:
      type: string
      enum:
        - SIGTERM
        - SIGKILL
        - SIGINT
        - SIGHUP
        - SIGQUIT
        - SIGUSR1
        - SIGUSR2
    RecordId:
      type: string
      pattern: ^[a-z0-9_]{1,64}$
      description: The ID of a record, such as a slot or a command.
    Encoding:
      type: string
      enum:
        - utf8
        - base64
  securitySchemes:
    apiKey:
      type: http
      scheme: bearer
      description: An API key that starts with vms_. Make one in Settings > API keys.

````

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