# 在会话中执行代码 (/zh/api-reference/endpoint/browser-execute)

<!-- agent-signals: reading_time_min: 2 · est_tokens: 1228 · updated: 2026-07-30 -->
Related: [Search（搜索）](/zh/api-reference/endpoint/search.md), [搜索反馈](/zh/api-reference/endpoint/search-feedback.md), [Scrape](/zh/api-reference/endpoint/scrape.md), [批量抓取](/zh/api-reference/endpoint/batch-scrape.md), [获取批量抓取状态](/zh/api-reference/endpoint/batch-scrape-get.md), [取消批量抓取](/zh/api-reference/endpoint/batch-scrape-delete.md)

<div id="headers">
  ## 请求头 [#请求头]
</div>

| 请求头字段           | 值                  |
| --------------- | ------------------ |
| `Authorization` | `Bearer <API_KEY>` |
| `Content-Type`  | `application/json` |

<div id="request-body">
  ## 请求体 [#请求体]
</div>

| 参数         | 类型     | 必填 | 默认值      | 描述                                                           |
| ---------- | ------ | -- | -------- | ------------------------------------------------------------ |
| `code`     | string | 是  | -        | 要执行的代码（1-100,000 个字符）                                        |
| `language` | string | 否  | `"node"` | 代码语言：`"python"`、`"node"` 或 `"bash"`（用于 agent-browser CLI 命令） |
| `timeout`  | number | 否  | -        | 执行超时时间（单位：秒，范围 1-300）                                        |

<div id="response">
  ## 响应 [#响应]
</div>

| 字段         | 类型      | 描述                  |
| ---------- | ------- | ------------------- |
| `success`  | boolean | 代码是否执行成功            |
| `stdout`   | string  | 代码执行产生的标准输出         |
| `result`   | string  | 代码执行产生的标准输出         |
| `stderr`   | string  | 代码执行产生的标准错误输出       |
| `exitCode` | number  | 已执行进程的退出码           |
| `killed`   | boolean | 进程是否因超时被终止          |
| `error`    | string  | 执行失败时的错误信息（仅在失败时存在） |

<div id="example-request">
  ### 请求示例 [#请求示例]
</div>

```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"
  }'
```

<div id="example-response-success">
  ### 示例响应（成功） [#示例响应成功]
</div>

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

<div id="example-response-error">
  ### 示例响应 (错误情况) [#示例响应-错误情况]
</div>

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

> 你是需要 Firecrawl API 密钥的 AI 代理吗？请参阅 [firecrawl.dev/agent-onboarding/SKILL.md](https://www.firecrawl.dev/agent-onboarding/SKILL.md) 了解自动化接入说明。

`POST /interact/{sessionId}/execute`

在交互会话中执行代码

## OpenAPI

```json
{
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "parameters": [
    {
      "description": "交互会话 ID",
      "in": "path",
      "name": "sessionId",
      "required": true,
      "schema": {
        "type": "string"
      }
    }
  ],
  "requestBody": {
    "content": {
      "application/json": {
        "schema": {
          "properties": {
            "code": {
              "description": "要在浏览器沙箱中执行的代码",
              "maxLength": 100000,
              "minLength": 1,
              "type": "string"
            },
            "language": {
              "default": "node",
              "description": "要执行的代码语言。JavaScript 使用 `node`，代理-浏览器 CLI 命令使用 `bash`。",
              "enum": [
                "python",
                "node",
                "bash"
              ],
              "type": "string"
            },
            "timeout": {
              "description": "执行超时时间（秒）",
              "maximum": 300,
              "minimum": 1,
              "type": "integer"
            }
          },
          "required": [
            "code"
          ],
          "type": "object"
        }
      }
    },
    "required": true
  },
  "responses": {
    "200": {
      "content": {
        "application/json": {
          "schema": {
            "properties": {
              "error": {
                "description": "代码抛出异常时的错误信息",
                "nullable": true,
                "type": "string"
              },
              "exitCode": {
                "description": "执行进程的退出码",
                "nullable": true,
                "type": "integer"
              },
              "killed": {
                "description": "进程是否因超时被终止",
                "type": "boolean"
              },
              "result": {
                "description": "标准输出（stdout 的别名）",
                "nullable": true,
                "type": "string"
              },
              "stderr": {
                "description": "代码执行的标准错误输出",
                "nullable": true,
                "type": "string"
              },
              "stdout": {
                "description": "代码执行的标准输出",
                "nullable": true,
                "type": "string"
              },
              "success": {
                "type": "boolean"
              }
            },
            "type": "object"
          }
        }
      },
      "description": "代码执行成功"
    },
    "402": {
      "content": {
        "application/json": {
          "schema": {
            "properties": {
              "error": {
                "example": "Payment required to access this resource.",
                "type": "string"
              }
            },
            "type": "object"
          }
        }
      },
      "description": "需要付费"
    }
  }
}
```
