Skip to content

Latest commit

 

History

History
153 lines (114 loc) · 2.25 KB

File metadata and controls

153 lines (114 loc) · 2.25 KB

ScrapeGraph JS SDK

npm version License: MIT

Official ScrapeGraphAI SDK for the ScrapeGraph AI API v2.

Install

npm i scrapegraph-js
# or
bun add scrapegraph-js

Quick Start

import { scrapegraphai } from "scrapegraph-js";

const sgai = scrapegraphai({ apiKey: "your-api-key" });

const result = await sgai.scrape("https://example.com", { format: "markdown" });

console.log(result.data);
console.log(result._requestId);

Every method returns:

type ApiResult<T> = {
  data: T;
  _requestId: string;
};

API

Create a client once, then call the available v2 endpoints:

const sgai = scrapegraphai({
  apiKey: "your-api-key",
  baseUrl: "https://api.scrapegraphai.com", // optional
  timeout: 30000, // optional
  maxRetries: 2, // optional
});

scrape

await sgai.scrape("https://example.com", {
  format: "markdown",
  fetchConfig: {
    mock: false,
  },
});

extract

Raw JSON schema:

await sgai.extract("https://example.com", {
  prompt: "Extract the page title",
  schema: {
    type: "object",
    properties: {
      title: { type: "string" },
    },
  },
});

Zod schema:

import { z } from "zod";

await sgai.extract("https://example.com", {
  prompt: "Extract the page title",
  schema: z.object({
    title: z.string(),
  }),
});

search

await sgai.search("What is the capital of France?", {
  numResults: 5,
});

schema

await sgai.schema("A product with name and price");

credits

await sgai.credits();

history

await sgai.history({
  page: 1,
  limit: 10,
  service: "scrape",
});

crawl

const crawl = await sgai.crawl.start("https://example.com", {
  maxPages: 10,
  maxDepth: 2,
});

await sgai.crawl.status((crawl.data as { id: string }).id);

monitor

await sgai.monitor.create({
  url: "https://example.com",
  prompt: "Notify me when the price changes",
  interval: "1h",
});

Development

bun install
bun test
bun run check
bun run build

License

MIT - ScrapeGraph AI