メインコンテンツへスキップ
Web データを活用する AI アプリケーション向けに、Firecrawl を Google の Gemini と統合します。

セットアップ

npm install @mendable/firecrawl-js @google/genai
「.env」ファイルを作成:
FIRECRAWL_API_KEY=your_firecrawl_key
GEMINI_API_KEY=your_gemini_key
注意: Node < 20 を使用している場合は、dotenv をインストールし、コードに import 'dotenv/config' を追加してください。

スクレイプ + 要約

この例では、シンプルなワークフローを紹介します。ウェブサイトをスクレイプし、Geminiでコンテンツを要約します。
import FirecrawlApp from '@mendable/firecrawl-js';
import { GoogleGenAI } from '@google/genai';

const firecrawl = new FirecrawlApp({ apiKey: process.env.FIRECRAWL_API_KEY });
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const scrapeResult = await firecrawl.scrape('https://firecrawl.dev', {
    formats: ['markdown']
});

console.log('スクレイプしたコンテンツの長さ:', scrapeResult.markdown?.length);

const response = await ai.models.generateContent({
    model: 'gemini-2.5-flash',
    contents: `要約してください: ${scrapeResult.markdown}`,
});

console.log('要約:', response.text);

コンテンツ分析

この例では、Gemini のマルチターン会話機能を用いて、ウェブサイトのコンテンツを分析する方法を示します。
import FirecrawlApp from '@mendable/firecrawl-js';
import { GoogleGenAI } from '@google/genai';

const firecrawl = new FirecrawlApp({ apiKey: process.env.FIRECRAWL_API_KEY });
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const scrapeResult = await firecrawl.scrape('https://news.ycombinator.com/', {
    formats: ['markdown']
});

console.log('スクレイプしたコンテンツの長さ:', scrapeResult.markdown?.length);

const chat = ai.chats.create({
    model: 'gemini-2.5-flash'
});

// Hacker Newsのトップ3記事を取得
const result1 = await chat.sendMessage({
    message: `Hacker Newsのこのウェブサイトコンテンツに基づいて、現在のトップ3記事は何ですか?\n\n${scrapeResult.markdown}`
});
console.log('トップ3記事:', result1.text);

// Hacker Newsの4番目と5番目の記事を取得
const result2 = await chat.sendMessage({
    message: `次に、同じコンテンツからHacker Newsの4番目と5番目のトップ記事は何ですか?`
});
console.log('4番目と5番目の記事:', result2.text);

構造化抽出

この例では、スクレイピングしたウェブサイトのコンテンツから、GeminiのJSONモードを使って構造化データを抽出する方法を示します。
import FirecrawlApp from '@mendable/firecrawl-js';
import { GoogleGenAI, Type } from '@google/genai';

const firecrawl = new FirecrawlApp({ apiKey: process.env.FIRECRAWL_API_KEY });
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const scrapeResult = await firecrawl.scrape('https://stripe.com', {
    formats: ['markdown']
});

console.log('スクレイピングしたコンテンツの長さ:', scrapeResult.markdown?.length);

const response = await ai.models.generateContent({
    model: 'gemini-2.5-flash',
    contents: `企業情報を抽出: ${scrapeResult.markdown}`,
    config: {
        responseMimeType: 'application/json',
        responseSchema: {
            type: Type.OBJECT,
            properties: {
                name: { type: Type.STRING },
                industry: { type: Type.STRING },
                description: { type: Type.STRING },
                products: {
                    type: Type.ARRAY,
                    items: { type: Type.STRING }
                }
            },
            propertyOrdering: ['name', 'industry', 'description', 'products']
        }
    }
});

console.log('抽出された企業情報:', response?.text);
詳細な例については、Gemini のドキュメントをご覧ください。