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

# Execute Code Snippet

> Run a TypeScript snippet in the sandbox against the browser session (query `sessionId` required).



## OpenAPI

````yaml /openapi.yaml post /v1/tools/execute-code
openapi: 3.1.0
info:
  title: AnchorBrowser API
  version: 1.0.0
  description: APIs to manage all browser-related actions and configuration.
servers:
  - url: https://api.anchorbrowser.io
    description: API server
security: []
paths:
  /v1/tools/execute-code:
    post:
      tags:
        - Tools
      summary: Execute Code Snippet
      description: >-
        Run a TypeScript snippet in the sandbox against the browser session
        (query `sessionId` required).
      parameters:
        - in: query
          name: sessionId
          required: true
          schema:
            type: string
            format: uuid
          description: Browser session to run the snippet against.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ExecuteCodeRequestSchema'
      responses:
        '200':
          description: Snippet execution finished.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ExecuteCodeToolResponseSchema'
        '400':
          description: Invalid request.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '500':
          description: Internal server error.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
      security:
        - api_key_header: []
components:
  schemas:
    ExecuteCodeRequestSchema:
      type: object
      required:
        - code
      properties:
        code:
          type: string
          description: >-
            TypeScript snippet (async function body) with injected page,
            browser, context, sessionId, anchorClient.
    ExecuteCodeToolResponseSchema:
      type: object
      required:
        - success
        - message
      properties:
        success:
          type: boolean
        message:
          type: string
        output:
          type: string
          description: JSON string of the return value when execution succeeds.
        error:
          type: string
        executionResultId:
          type: string
          format: uuid
    ErrorResponse:
      type: object
      properties:
        error:
          type: object
          properties:
            code:
              type: integer
            message:
              type: string
  securitySchemes:
    api_key_header:
      type: apiKey
      in: header
      name: anchor-api-key
      description: API key passed in the header

````