# Execute Code in a Session (/api-reference/endpoint/browser-execute)

<!-- agent-signals: reading_time_min: 3 · est_tokens: 1435 · updated: 2026-07-30 -->
Related: [Search](/api-reference/endpoint/search.md), [Search Feedback](/api-reference/endpoint/search-feedback.md), [Scrape](/api-reference/endpoint/scrape.md), [Batch Scrape](/api-reference/endpoint/batch-scrape.md), [Get Batch Scrape Status](/api-reference/endpoint/batch-scrape-get.md), [Cancel Batch Scrape](/api-reference/endpoint/batch-scrape-delete.md)

## Headers [#headers]

| Header          | Value              |
| --------------- | ------------------ |
| `Authorization` | `Bearer <API_KEY>` |
| `Content-Type`  | `application/json` |

## Request Body [#request-body]

| Parameter  | Type   | Required | Default  | Description                                                                              |
| ---------- | ------ | -------- | -------- | ---------------------------------------------------------------------------------------- |
| `code`     | string | Yes      | -        | Code to execute (1-100,000 characters)                                                   |
| `language` | string | No       | `"node"` | Language of the code: `"python"`, `"node"`, or `"bash"` (for agent-browser CLI commands) |
| `timeout`  | number | No       | -        | Execution timeout in seconds (1-300)                                                     |

## Response [#response]

| Field      | Type    | Description                                                 |
| ---------- | ------- | ----------------------------------------------------------- |
| `success`  | boolean | Whether the code executed successfully                      |
| `stdout`   | string  | Standard output from the code execution                     |
| `result`   | string  | Standard output from the code execution                     |
| `stderr`   | string  | Standard error output from the code execution               |
| `exitCode` | number  | Exit code of the executed process                           |
| `killed`   | boolean | Whether the process was killed due to timeout               |
| `error`    | string  | Error message if execution failed (only present on failure) |

### Example Request [#example-request]

```bash
curl -X POST "https://api.firecrawl.dev/v2/interact/550e8400-e29b-41d4-a716-446655440000/execute" \
  -H "Authorization: Bearer $FIRECRAWL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "await page.goto(\"https://example.com\")\ntitle = await page.title()\nprint(title)",
    "language": "python"
  }'
```

### Example Response (Success) [#example-response-success]

```json
{
  "success": true,
  "result": "Example Domain"
}
```

### Example Response (Error) [#example-response-error]

```json
{
  "success": true,
  "error": "TimeoutError: page.goto: Timeout 30000ms exceeded."
}
```

> Are you an AI agent that needs a Firecrawl API key? See [firecrawl.dev/agent-onboarding/SKILL.md](https://www.firecrawl.dev/agent-onboarding/SKILL.md) for automated onboarding instructions.

`POST /interact/{sessionId}/execute`

Execute code in an interact session

## OpenAPI

```json
{
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "parameters": [
    {
      "name": "sessionId",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string"
      },
      "description": "The interact session ID"
    }
  ],
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "required": [
            "code"
          ],
          "properties": {
            "code": {
              "type": "string",
              "minLength": 1,
              "maxLength": 100000,
              "description": "Code to execute in the browser sandbox"
            },
            "language": {
              "type": "string",
              "enum": [
                "python",
                "node",
                "bash"
              ],
              "default": "node",
              "description": "Language of the code to execute. Use `node` for JavaScript or `bash` for agent-browser CLI commands."
            },
            "timeout": {
              "type": "integer",
              "minimum": 1,
              "maximum": 300,
              "description": "Execution timeout in seconds"
            }
          }
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "Code executed successfully",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "success": {
                "type": "boolean"
              },
              "stdout": {
                "type": "string",
                "nullable": true,
                "description": "Standard output from the code execution"
              },
              "result": {
                "type": "string",
                "nullable": true,
                "description": "Standard output (alias for stdout)"
              },
              "stderr": {
                "type": "string",
                "nullable": true,
                "description": "Standard error output from the code execution"
              },
              "exitCode": {
                "type": "integer",
                "nullable": true,
                "description": "Exit code of the executed process"
              },
              "killed": {
                "type": "boolean",
                "description": "Whether the process was killed due to timeout"
              },
              "error": {
                "type": "string",
                "nullable": true,
                "description": "Error message if the code raised an exception"
              }
            }
          }
        }
      }
    },
    "402": {
      "description": "Payment required",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "error": {
                "type": "string",
                "example": "Payment required to access this resource."
              }
            }
          }
        }
      }
    }
  }
}
```
