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

# Execute Code

> Execute Python code in a sandbox



## OpenAPI

````yaml POST /v2/sandcastles/{id}/execute
openapi: 3.1.0
info:
  title: TextQL v2 API
  version: '2.0'
  description: >
    REST API for TextQL platform operations. All endpoints require Bearer token
    authentication.
servers:
  - url: https://app.textql.com
security:
  - bearerAuth: []
tags:
  - name: Chat
    description: Create and manage AI chat sessions
  - name: Connectors
    description: List available data connectors
  - name: Playbooks
    description: Create, configure, and run automated playbooks
  - name: Sandcastles
    description: Manage Python sandbox environments for code execution
  - name: Changes
    description: Review, approve, and deny Context Library changes
paths:
  /v2/sandcastles/{id}/execute:
    post:
      tags:
        - Sandcastles
      summary: Execute Code
      description: >
        Execute Python code in a sandbox. Returns stdout/stderr output,

        generated files, and dataframe info.


        Executions are recorded — see

        [GET
        /v2/sandcastles/{id}/executions](/api-reference/v2/sandbox/list-executions).
      operationId: v2.executeCode
      parameters:
        - $ref: '#/components/parameters/SandboxId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - code
              properties:
                code:
                  type: string
                  description: Python code to execute
      responses:
        '200':
          description: Execution result
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ExecuteCodeResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '429':
          $ref: '#/components/responses/RateLimited'
        '500':
          $ref: '#/components/responses/InternalError'
components:
  parameters:
    SandboxId:
      name: id
      in: path
      required: true
      schema:
        type: string
      description: Sandbox ID
  schemas:
    ExecuteCodeResponse:
      type: object
      properties:
        output:
          type: array
          nullable: true
          items:
            type: string
          description: >
            Stdout/stderr output as an array of strings, one element per print
            call.

            Null when execution fails with an error.
        error:
          type: string
          description: >
            Execution error message, including the Python exception type and
            message

            (e.g. "executing Python code: ZeroDivisionError('division by
            zero')").

            Only present when execution fails. When set, output is null.
        execution_time_ms:
          type: integer
          format: int64
          description: Execution duration in milliseconds
        files:
          type: array
          items:
            type: object
            properties:
              name:
                type: string
              url:
                type: string
              mime_type:
                type: string
        dataframes:
          type: array
          description: >
            Dataframes created or modified during execution. May be empty even
            if

            code creates dataframes — use GET /v2/sandcastles/{id} for the
            authoritative

            list of loaded dataframes.
          items:
            type: object
            properties:
              name:
                type: string
              num_rows:
                type: integer
                format: int64
              num_cols:
                type: integer
                format: int64
    ErrorResponse:
      type: object
      properties:
        error:
          type: object
          properties:
            code:
              type: string
              description: Machine-readable error code
              enum:
                - invalid_request
                - unauthenticated
                - permission_denied
                - not_found
                - conflict
                - rate_limit_exceeded
                - internal
                - timeout
                - cancelled
                - execution_failed
                - no_report
            message:
              type: string
              description: Human-readable error message
  responses:
    BadRequest:
      description: Invalid request
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          example:
            error:
              code: invalid_request
              message: Invalid request body
    Unauthorized:
      description: Missing or invalid authentication
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          example:
            error:
              code: unauthenticated
              message: Authentication required
    Forbidden:
      description: Insufficient permissions
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          example:
            error:
              code: permission_denied
              message: Insufficient permissions
    NotFound:
      description: Resource not found
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          example:
            error:
              code: not_found
              message: Resource not found
    RateLimited:
      description: Rate limit exceeded
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          example:
            error:
              code: rate_limit_exceeded
              message: Rate limit exceeded
    InternalError:
      description: Internal server error
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          example:
            error:
              code: internal
              message: Internal server error
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: API key or JWT token

````