| title | Using ScrapeGraphAI with v0 |
|---|---|
| description | Integrate web scraping into UI components generated with Vercel v0 |
v0 by Vercel is an AI tool that generates React UI components from prompts. You can combine it with ScrapeGraphAI to build components that display live data extracted from any website.
v0 generates frontend components. ScrapeGraphAI runs server-side (e.g. in a Next.js API route or Server Action) to avoid exposing your API key and to bypass CORS restrictions.
Ask v0 to build a component, for example:
Create a card component that displays a company name, description, and founding year fetched from an external API.
Install the v2 SDK:
npm i scrapegraph-js@latestCreate app/api/scrape/route.ts in your Next.js project:
import { NextRequest, NextResponse } from "next/server";
import { ScrapeGraphAI } from "scrapegraph-js";
const sgai = ScrapeGraphAI(); // reads SGAI_API_KEY from env
export async function POST(req: NextRequest) {
const { url, prompt } = await req.json();
const res = await sgai.extract({ url, prompt });
if (res.status === "success") {
return NextResponse.json({ data: res.data?.json });
}
return NextResponse.json({ error: res.error }, { status: 502 });
}Add SGAI_API_KEY to your .env.local file:
SGAI_API_KEY=your-api-key-hereasync function getScrapedData(url: string, prompt: string) {
const res = await fetch("/api/scrape", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url, prompt }),
});
return res.json();
}- Use Server Components (
async function Page()) or Server Actions to callsgai.extractdirectly without an API route — the key stays on the server. - Pass a
schema(JSON Schema or Zod-compiled JSON Schema) tosgai.extract({ url, prompt, schema })to get typed, structured data back.