メインコンテンツへスキップ

前提条件

セットアップ

npm install hono @mendable/firecrawl-js
.env に API キーを追加します:
FIRECRAWL_API_KEY=fc-YOUR-API-KEY

ウェブを検索する

import { Hono } from "hono";
import Firecrawl from "@mendable/firecrawl-js";

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

app.post("/search", async (c) => {
  const { query } = await c.req.json();
  const results = await firecrawl.search(query, { limit: 5 });
  return c.json(results);
});

export default app;

ページをスクレイピング

app.post("/scrape", async (c) => {
  const { url } = await c.req.json();
  const result = await firecrawl.scrape(url);
  return c.json(result);
});

ページを Interact で操作する

Interact を使うと、ライブのブラウザセッションを操作して、ボタンのクリック、フォーム入力、動的コンテンツの抽出を行えます。
app.post("/interact", async (c) => {
  const { url } = await c.req.json();

  const result = await firecrawl.scrape(url, { 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 c.json({ output: response.output });
});

どこでもデプロイ可能

Hono は複数のランタイムで動作します。Cloudflare Workers では、環境バインディングから API キーを渡します。
import { Hono } from "hono";
import Firecrawl from "@mendable/firecrawl-js";

type Bindings = { FIRECRAWL_API_KEY: string };
const app = new Hono<{ Bindings: Bindings }>();

app.post("/search", async (c) => {
  const firecrawl = new Firecrawl({ apiKey: c.env.FIRECRAWL_API_KEY });
  const { query } = await c.req.json();
  const results = await firecrawl.search(query, { limit: 5 });
  return c.json(results);
});

export default app;

次のステップ

スクレイピングのドキュメント

フォーマット、アクション、プロキシを含むスクレイピングのオプション一覧

検索ドキュメント

Webを検索してページ全体のコンテンツを取得

Interact ドキュメント

クリックやフォーム入力で動的コンテンツを抽出

Node SDK リファレンス

クロール、map、バッチスクレイプなどを含むSDKの完全なリファレンス