> ## 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.

# Bun

> 使用 Firecrawl 和 Bun 构建高性能的网页抓取与搜索服务器。

<div id="prerequisites">
  ## 前提条件
</div>

* Bun 1.0+
* 一个 Firecrawl API 密钥 — [免费获取](https://www.firecrawl.dev/app/api-keys)

<div id="install-the-sdk">
  ## 安装 SDK
</div>

```bash theme={null}
bun add firecrawl
```

将你的 API 密钥添加到 `.env` 文件中：

```bash theme={null}
FIRECRAWL_API_KEY=fc-YOUR-API-KEY
```

<div id="search-the-web">
  ## 进行网页搜索
</div>

Bun 内置了 HTTP 服务器。创建 `index.ts`：

```typescript theme={null}
import { Firecrawl } from "firecrawl";

const firecrawl = new Firecrawl({ apiKey: process.env.FIRECRAWL_API_KEY });

Bun.serve({
  port: 3000,
  async fetch(req) {
    const url = new URL(req.url);

    if (req.method === "POST" && url.pathname === "/search") {
      const { query } = await req.json();
      const results = await firecrawl.search(query, { limit: 5 });
      return Response.json(results);
    }

    return new Response("Not found", { status: 404 });
  },
});

console.log("Server running on port 3000");
```

运行：

```bash theme={null}
bun run index.ts
```

<div id="scrape-a-page">
  ## 抓取页面
</div>

在同一个服务器中添加 `/scrape` 路由：

```typescript theme={null}
if (req.method === "POST" && url.pathname === "/scrape") {
  const { url: targetUrl } = await req.json();
  const result = await firecrawl.scrape(targetUrl);
  return Response.json(result);
}
```

<div id="interact-with-a-page">
  ## 与页面交互
</div>

使用 交互 操控实时浏览器会话——点击按钮、填写表单，并提取动态内容。

```typescript theme={null}
if (req.method === "POST" && url.pathname === "/interact") {
  const { url: targetUrl } = await req.json();

  const result = await firecrawl.scrape(targetUrl, { formats: ['markdown'] });
  const scrapeId = result.metadata?.scrapeId;

  await firecrawl.interact(scrapeId, { prompt: 'Search for iPhone 16 Pro Max' });
  const response = await firecrawl.interact(scrapeId, { prompt: 'Click on the first result and tell me the price' });

  await firecrawl.stopInteraction(scrapeId);

  return Response.json({ output: response.output });
}
```

<div id="script-usage">
  ## 脚本用法
</div>

在独立的 Bun 脚本中使用 Firecrawl：

```typescript theme={null}
import { Firecrawl } from "firecrawl";

const app = new Firecrawl({ apiKey: process.env.FIRECRAWL_API_KEY });
const results = await app.search("firecrawl web scraping", { limit: 5 });
console.log(results);
```

```bash theme={null}
bun run search.ts
```

<div id="next-steps">
  ## 下一步
</div>

<CardGroup cols={2}>
  <Card title="抓取文档" icon="file-lines" href="/zh/features/scrape">
    所有抓取选项，包括 formats、actions 和代理
  </Card>

  <Card title="搜索文档" icon="magnifying-glass" href="/zh/features/search">
    进行网页搜索并获取完整页面内容
  </Card>

  <Card title="交互文档" icon="hand-pointer" href="/zh/features/interact">
    点击、填写表单并提取动态内容
  </Card>

  <Card title="Node SDK 参考" icon="node" href="/zh/sdks/node">
    完整的 SDK 参考，涵盖爬取、map、batch scrape 等
  </Card>
</CardGroup>
