|
| 1 | +# ScrapeGraphAI AI SDK Tools |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@scrapegraphai/ai-sdk) |
| 4 | +[](https://opensource.org/licenses/MIT) |
| 5 | + |
| 6 | +<p align="center"> |
| 7 | + <a href="https://scrapegraphai.com"> |
| 8 | + <img src="../../media/banner.png" alt="ScrapeGraphAI AI SDK Tools" style="width: 100%;"> |
| 9 | + </a> |
| 10 | +</p> |
| 11 | + |
| 12 | +Vercel [AI SDK](https://ai-sdk.dev/docs/introduction) tools for the ScrapeGraphAI API. |
| 13 | + |
| 14 | +## Install |
| 15 | + |
| 16 | +```bash |
| 17 | +npm i @scrapegraphai/ai-sdk ai |
| 18 | +# or |
| 19 | +bun add @scrapegraphai/ai-sdk ai |
| 20 | +``` |
| 21 | + |
| 22 | +`ai` is a peer dependency. Install the model provider package you use, for example: |
| 23 | + |
| 24 | +```bash |
| 25 | +npm i @ai-sdk/openai |
| 26 | +# or |
| 27 | +bun add @ai-sdk/openai |
| 28 | +``` |
| 29 | + |
| 30 | +## Quick Start |
| 31 | + |
| 32 | +### API key |
| 33 | + |
| 34 | +Log in to the [ScrapeGraphAI dashboard](https://scrapegraphai.com/) to create an API key. The dashboard also shows your request history, usage, credits, and crawl/monitor activity. |
| 35 | + |
| 36 | +Set it in your environment: |
| 37 | + |
| 38 | +```bash |
| 39 | +export SGAI_API_KEY=... |
| 40 | +``` |
| 41 | + |
| 42 | +Minimal scrape-only setup: |
| 43 | + |
| 44 | +```ts |
| 45 | +import { openai } from "@ai-sdk/openai"; |
| 46 | +import { generateText, stepCountIs } from "ai"; |
| 47 | +import { scrapeTool } from "@scrapegraphai/ai-sdk"; |
| 48 | + |
| 49 | +const result = await generateText({ |
| 50 | + model: openai("gpt-5-nano"), |
| 51 | + prompt: "Find the main headline on https://example.com", |
| 52 | + tools: { |
| 53 | + scrape: scrapeTool(), |
| 54 | + }, |
| 55 | + stopWhen: stepCountIs(5), |
| 56 | +}); |
| 57 | + |
| 58 | +console.log(result.text); |
| 59 | +``` |
| 60 | + |
| 61 | +Use every ScrapeGraphAI tool group: |
| 62 | + |
| 63 | +```ts |
| 64 | +import { openai } from "@ai-sdk/openai"; |
| 65 | +import { generateText, stepCountIs } from "ai"; |
| 66 | +import { |
| 67 | + crawlTools, |
| 68 | + extractTool, |
| 69 | + monitorTools, |
| 70 | + scrapeTool, |
| 71 | + searchTool, |
| 72 | +} from "@scrapegraphai/ai-sdk"; |
| 73 | + |
| 74 | +const result = await generateText({ |
| 75 | + model: openai("gpt-5-nano"), |
| 76 | + prompt: "Search for ScrapeGraphAI docs, scrape the best page, and summarize it.", |
| 77 | + tools: { |
| 78 | + scrape: scrapeTool(), |
| 79 | + extract: extractTool(), |
| 80 | + search: searchTool(), |
| 81 | + ...crawlTools(), |
| 82 | + ...monitorTools(), |
| 83 | + }, |
| 84 | + stopWhen: stepCountIs(10), |
| 85 | +}); |
| 86 | + |
| 87 | +console.log(result.text); |
| 88 | +``` |
| 89 | + |
| 90 | +Tools read `SGAI_API_KEY` from the environment by default. You can also pass it explicitly: |
| 91 | + |
| 92 | +```ts |
| 93 | +const tools = { |
| 94 | + scrape: scrapeTool({ apiKey: process.env.SGAI_API_KEY }), |
| 95 | +}; |
| 96 | +``` |
| 97 | + |
| 98 | +## Tools |
| 99 | + |
| 100 | +### scrapeTool |
| 101 | + |
| 102 | +Scrape a webpage with ScrapeGraphAI. Supports markdown, html, json extraction, links, images, summary, branding, and screenshots. |
| 103 | + |
| 104 | +```ts |
| 105 | +import { scrapeTool } from "@scrapegraphai/ai-sdk"; |
| 106 | + |
| 107 | +const tools = { |
| 108 | + scrape: scrapeTool(), |
| 109 | +}; |
| 110 | +``` |
| 111 | + |
| 112 | +### extractTool |
| 113 | + |
| 114 | +Extract structured JSON from a URL, HTML, or markdown with a natural-language prompt. |
| 115 | + |
| 116 | +```ts |
| 117 | +import { extractTool } from "@scrapegraphai/ai-sdk"; |
| 118 | + |
| 119 | +const tools = { |
| 120 | + extract: extractTool(), |
| 121 | +}; |
| 122 | +``` |
| 123 | + |
| 124 | +### searchTool |
| 125 | + |
| 126 | +Search the web and optionally extract structured data from search results. |
| 127 | + |
| 128 | +```ts |
| 129 | +import { searchTool } from "@scrapegraphai/ai-sdk"; |
| 130 | + |
| 131 | +const tools = { |
| 132 | + search: searchTool(), |
| 133 | +}; |
| 134 | +``` |
| 135 | + |
| 136 | +### crawlTools |
| 137 | + |
| 138 | +Start, poll, page through, stop, resume, and delete ScrapeGraphAI crawl jobs. |
| 139 | + |
| 140 | +```ts |
| 141 | +import { crawlTools } from "@scrapegraphai/ai-sdk"; |
| 142 | + |
| 143 | +const tools = { |
| 144 | + ...crawlTools(), |
| 145 | +}; |
| 146 | +``` |
| 147 | + |
| 148 | +Crawl page retrieval is paginated. Use `getCrawl` for status, then `getCrawlPages` for pages and resolved scrape results. |
| 149 | + |
| 150 | +```ts |
| 151 | +const tools = { |
| 152 | + startCrawl: startCrawlTool(), |
| 153 | + getCrawl: getCrawlTool(), |
| 154 | + getCrawlPages: getCrawlPagesTool(), |
| 155 | +}; |
| 156 | +``` |
| 157 | + |
| 158 | +### monitorTools |
| 159 | + |
| 160 | +Create, list, update, pause, resume, delete, and fetch activity for ScrapeGraphAI monitors. |
| 161 | + |
| 162 | +```ts |
| 163 | +import { monitorTools } from "@scrapegraphai/ai-sdk"; |
| 164 | + |
| 165 | +const tools = { |
| 166 | + ...monitorTools(), |
| 167 | +}; |
| 168 | +``` |
| 169 | + |
| 170 | +## Examples |
| 171 | + |
| 172 | +| Example | Description | |
| 173 | +|---------|-------------| |
| 174 | +| [`hacker-news.ts`](examples/hacker-news.ts) | Scrape Hacker News with AI SDK tools | |
| 175 | +| [`crawl-blog.ts`](examples/crawl-blog.ts) | Crawl ScrapeGraphAI blog pages, fetch paginated crawl results, and summarize them | |
| 176 | + |
| 177 | +Run an example: |
| 178 | + |
| 179 | +```bash |
| 180 | +OPENAI_API_KEY=... SGAI_API_KEY=... bun examples/crawl-blog.ts |
| 181 | +``` |
| 182 | + |
| 183 | +## Environment Variables |
| 184 | + |
| 185 | +| Variable | Description | |
| 186 | +|----------|-------------| |
| 187 | +| `SGAI_API_KEY` | ScrapeGraphAI API key | |
| 188 | +| `OPENAI_API_KEY` | Required by the OpenAI provider examples | |
| 189 | + |
| 190 | +## Development |
| 191 | + |
| 192 | +```bash |
| 193 | +bun install |
| 194 | +bun run build |
| 195 | +bun run check |
| 196 | +``` |
| 197 | + |
| 198 | +## License |
| 199 | + |
| 200 | +MIT - [ScrapeGraphAI](https://scrapegraphai.com) |
0 commit comments