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

前提条件

セットアップ

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

Webを検索する

import express from "express";
import Firecrawl from "@mendable/firecrawl-js";

const app = express();
app.use(express.json());

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

app.post("/search", async (req, res) => {
  try {
    const { query } = req.body;
    const results = await firecrawl.search(query, { limit: 5 });
    res.json(results);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.listen(3000, () => console.log("Server running on port 3000"));

ページのスクレイピング

app.post("/scrape", async (req, res) => {
  try {
    const { url } = req.body;
    const result = await firecrawl.scrape(url);
    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

ページをInteractする

Interact を使うと、実行中のブラウザセッションを制御できます。ボタンをクリックしたり、フォームに入力したり、動的コンテンツを抽出したりできます。
app.post("/interact", async (req, res) => {
  try {
    const { url } = req.body;

    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);

    res.json({ output: response.output });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

試してみる

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

次のステップ

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

フォーマット、アクション、プロキシなど、スクレイピングのオプションを網羅

検索ドキュメント

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

Interact ドキュメント

クリック、フォーム入力、動的コンテンツの抽出

Node SDK リファレンス

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