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

# Execute Python

> Run a Python snippet on the computer and capture its stdout.

Executes Python code on the computer in a short-lived interpreter and returns its output.

## Path parameters

<ParamField path="id" type="string" required>
  Computer ID (UUID).
</ParamField>

## Body parameters

<ParamField body="code" type="string" required>
  Python code to execute.
</ParamField>

<ParamField body="timeout" type="integer" default="10">
  Timeout in seconds (1-300).
</ParamField>

## Response

<ResponseField name="output" type="string">
  Captured stdout from the interpreter.
</ResponseField>

<ResponseField name="success" type="boolean">
  `true` if the code ran without raising an uncaught exception.
</ResponseField>

<ResponseField name="error" type="string">
  Error message, if execution failed.
</ResponseField>

<ResponseField name="error_type" type="string">
  Python exception class (e.g., `SyntaxError`, `NameError`).
</ResponseField>

<ResponseField name="timeout" type="boolean">
  `true` if execution was killed by the timeout.
</ResponseField>

## Example

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://www.orgo.ai/api/computers/a3bb189e-8bf9-3888-9912-ace4e6543002/exec \
    -H "Authorization: Bearer $ORGO_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "code": "import os\nprint(os.getcwd())",
      "timeout": 10
    }'
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      f"https://www.orgo.ai/api/computers/{computer_id}/exec",
      headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
      json={
          "code": "import os\nprint(os.getcwd())",
          "timeout": 10
      }
  )

  result = response.json()
  if result["success"]:
      print(result["output"])
  else:
      print(f"Error: {result.get('error')}")
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch(`https://www.orgo.ai/api/computers/${computerId}/exec`, {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${apiKey}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      code: 'import os\nprint(os.getcwd())',
      timeout: 10
    })
  });

  const result = await response.json();
  if (result.success) {
    console.log(result.output);
  } else {
    console.error(`Error: ${result.error}`);
  }
  ```
</CodeGroup>

### Successful response

```json theme={null}
{
  "output": "/home/user\n",
  "success": true,
  "action": "exec",
  "timeout": false
}
```

### Error response

```json theme={null}
{
  "output": "",
  "success": false,
  "action": "exec",
  "error": "name 'undefined_var' is not defined",
  "error_type": "NameError"
}
```

<Tip>
  For shell commands, use [Execute bash](/api-reference/computers/bash) instead.
</Tip>

## Errors

| Status | Meaning                                                           |
| ------ | ----------------------------------------------------------------- |
| `400`  | Missing or non-string `code`, or computer instance not available. |
| `401`  | Missing or invalid API key.                                       |
| `403`  | You do not have access to this computer.                          |
| `404`  | Computer not found.                                               |
| `500`  | Upstream desktop agent failure.                                   |


## OpenAPI

````yaml POST /computers/{id}/exec
openapi: 3.1.0
info:
  title: Orgo API
  description: >-
    Launch cloud computers that AI agents can control and interact with. Create
    workspaces, provision computers, and control them programmatically.
  version: 2.0.0
  contact:
    name: Orgo Support
    email: spencer@orgo.ai
    url: https://orgo.ai
servers:
  - url: https://www.orgo.ai/api
    description: Production
security:
  - bearerAuth: []
tags:
  - name: Workspaces
    description: Organize computers into named workspaces
  - name: Computers
    description: Provision and manage virtual computers
  - name: Computer Lifecycle
    description: Start, stop, and restart computers
  - name: Computer Actions
    description: Control mouse, keyboard, and execute commands
  - name: Streaming
    description: Stream computer display via RTMP
  - name: Files
    description: Upload and download files
  - name: Templates
    description: Author, build, and launch reproducible computers from templates
paths:
  /computers/{id}/exec:
    post:
      tags:
        - Computer Actions
      summary: Execute Python
      description: Executes Python code on the computer.
      operationId: executePython
      parameters:
        - name: id
          in: path
          required: true
          description: Computer ID
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ExecRequest'
            example:
              code: |-
                import os
                print(os.getcwd())
              timeout: 10
      responses:
        '200':
          description: Code executed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ExecResponse'
              example:
                output: |
                  /home/user
                success: true
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
components:
  schemas:
    ExecRequest:
      type: object
      required:
        - code
      properties:
        code:
          type: string
          description: Python code to execute
        timeout:
          type: integer
          default: 10
          description: Timeout in seconds
          minimum: 1
          maximum: 300
    ExecResponse:
      type: object
      properties:
        output:
          type: string
          description: Code output
        success:
          type: boolean
          description: Whether execution succeeded
        error:
          type: string
          description: Error message if failed
        error_type:
          type: string
          description: Error type if failed
        timeout:
          type: boolean
          description: Whether execution timed out
    Error:
      type: object
      properties:
        error:
          type: string
          description: Error message
  responses:
    Unauthorized:
      description: Invalid or missing API key
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error: Invalid API key
    NotFound:
      description: Resource not found
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error: Resource not found
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: API key authentication. Get your key at orgo.ai/workspaces

````