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

# 在会话中执行代码

> 在独立的 Interact 会话中执行 Playwright 或 agent-browser 代码。

<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 theme={null}
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 theme={null}
{
  "success": true,
  "result": "Example Domain"
}
```

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

```json theme={null}
{
  "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) 了解自动化接入说明。


## OpenAPI

````yaml zh/api-reference/v2-openapi.json POST /interact/{sessionId}/execute
openapi: 3.0.0
info:
  contact:
    email: support@firecrawl.dev
    name: Firecrawl Support
    url: https://firecrawl.dev/support
  description: 用于与 Firecrawl 服务交互，执行网页抓取和爬取任务的 API。
  title: Firecrawl API
  version: v2
servers:
  - url: https://api.firecrawl.dev/v2
security:
  - bearerAuth: []
paths:
  /interact/{sessionId}/execute:
    post:
      tags:
        - Interact
      summary: 在交互会话中执行代码
      operationId: executeBrowserCode
      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: 需要付费
      security:
        - bearerAuth: []
components:
  securitySchemes:
    bearerAuth:
      scheme: bearer
      type: http

````