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

# NestJS

> 使用 Firecrawl 与 NestJS 构建结构化的网页抓取与搜索服务。

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

* NestJS 项目 (`@nestjs/cli`)
* 一个 Firecrawl API 密钥——[免费获取一个](https://www.firecrawl.dev/app/api-keys)

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

```bash theme={null}
npm install firecrawl
```

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

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

<div id="create-a-firecrawl-service">
  ## 创建 Firecrawl 服务
</div>

创建 `src/firecrawl/firecrawl.service.ts`：

```typescript theme={null}
import { Injectable } from "@nestjs/common";
import { Firecrawl } from "firecrawl";

@Injectable()
export class FirecrawlService {
  private readonly client: Firecrawl;

  constructor() {
    this.client = new Firecrawl({ apiKey: process.env.FIRECRAWL_API_KEY });
  }

  async search(query: string, limit = 5) {
    return this.client.search(query, { limit });
  }

  async scrape(url: string) {
    return this.client.scrape(url);
  }

  async interact(url: string, prompts: string[]) {
    const result = await this.client.scrape(url, { formats: ['markdown'] });
    const scrapeId = result.metadata?.scrapeId;

    const responses = [];
    for (const prompt of prompts) {
      const response = await this.client.interact(scrapeId, { prompt });
      responses.push(response);
    }

    await this.client.stopInteraction(scrapeId);
    return responses;
  }
}
```

<div id="create-a-controller">
  ## 创建控制器
</div>

新建 `src/firecrawl/firecrawl.controller.ts`：

```typescript theme={null}
import { Body, Controller, Post } from "@nestjs/common";
import { FirecrawlService } from "./firecrawl.service";

@Controller("firecrawl")
export class FirecrawlController {
  constructor(private readonly firecrawlService: FirecrawlService) {}

  @Post("search")
  async search(@Body("query") query: string) {
    return this.firecrawlService.search(query);
  }

  @Post("scrape")
  async scrape(@Body("url") url: string) {
    return this.firecrawlService.scrape(url);
  }

  @Post("interact")
  async interact(@Body("url") url: string, @Body("prompts") prompts: string[]) {
    return this.firecrawlService.interact(url, prompts);
  }
}
```

<div id="register-the-module">
  ## 注册模块
</div>

创建 `src/firecrawl/firecrawl.module.ts`：

```typescript theme={null}
import { Module } from "@nestjs/common";
import { FirecrawlService } from "./firecrawl.service";
import { FirecrawlController } from "./firecrawl.controller";

@Module({
  providers: [FirecrawlService],
  controllers: [FirecrawlController],
  exports: [FirecrawlService],
})
export class FirecrawlModule {}
```

在 `AppModule` 中导入 `FirecrawlModule`。

<div id="test-it">
  ## 试一试
</div>

```bash theme={null}
curl -X POST http://localhost:3000/firecrawl/search \
  -H "Content-Type: application/json" \
  -d '{"query": "firecrawl web scraping"}'
```

<div id="next-steps">
  ## 后续步骤
</div>

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

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

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

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