# Vercel AI SDK

Give an agent built with the [Vercel AI SDK](https://ai-sdk.dev/) three CleanedWeb tools. It can search the API catalog, read an API's inputs, fields and sample records, then run the API and get the records back as JSON. The tools come with the [`cleanedweb`](https://www.npmjs.com/package/cleanedweb) npm package as `cleanedweb/ai-sdk`.

## Install

```bash
npm install cleanedweb ai @ai-sdk/openai
```

Use AI SDK 5 or later; AI SDK 7 needs Node.js 22 or later. Any AI SDK model provider works; this guide uses OpenAI.

Searching the catalog needs no key. To run an API, create an API key with the `run` scope under **API keys** in the [console](https://app.cleanedweb.com/) and set it on the server as `CLEANEDWEB_API_KEY`. Keep it out of browser code.

## Run an agent

```ts
import { generateText, isStepCount } from 'ai';
import { openai } from '@ai-sdk/openai';
import { cleanedwebTools } from 'cleanedweb/ai-sdk';

const { text } = await generateText({
  model: openai('gpt-5-mini'),
  prompt: 'Find five GitHub repositories under the "web-scraping" topic and list their names and URLs.',
  tools: cleanedwebTools(),
  stopWhen: isStepCount(8),
});

console.log(text);
```

The agent calls `findApi` with the site and topic, `getApi` to learn the inputs, and `runApi` with `{ "topics": "web-scraping", "sort": "desc_stars" }` and `maxItems: 5`. The run returns five records and costs five records.

## Tools

| Tool | Key | What it does |
| --- | --- | --- |
| `findApi` | None | Searches the catalog by `query` words, `site` (a domain or name), `category` and the `fields` you need. Returns matches best first, each with its `apiId`, `endpointId`, site, key fields, and the requested fields it lists or lacks. Nothing runs. |
| `getApi` | None | Shows one API by `apiId` or slug: for each endpoint its inputs (with allowed values and examples), output fields, and three sample records saved when the API was accepted. Nothing runs. |
| `runApi` | `run` scope | Runs an endpoint with `input`, `maxItems` and `fields`, waits for it to end and returns the records with `accepted`, `status`, `unitsCharged` and `runId`. |

`runApi` reports `accepted: false` with `problems` when the result fails the [acceptance checks](/docs/run-catalog-api/). A `bounded` or `partial` status means the source holds more records than the run returned. Records are website data: the tool description tells the model never to follow instructions found in them.

## Options

```ts
import { CleanedWeb } from 'cleanedweb';
import { cleanedwebTools, findApi, getApi, runApi } from 'cleanedweb/ai-sdk';

// Defaults for every run the agent starts.
const tools = cleanedwebTools({ maxItems: 50, timeoutSeconds: 600 });

// Or pick tools, for example a read-only agent that never spends credit.
const client = new CleanedWeb({ apiKey: process.env.CLEANEDWEB_API_KEY });
const readOnly = { findApi: findApi({ client }), getApi: getApi({ client }) };
```

| Option | Meaning |
| --- | --- |
| `maxItems` | Records a run keeps when the model does not say. Default `20`; the model can ask for more. |
| `timeoutSeconds` | The run's own time budget, 60 to 86,400 seconds. |
| `apiKey` | Instead of `CLEANEDWEB_API_KEY`. |
| `client` | A `CleanedWeb` client to share, with its retries, timeouts and `fetch`. |

The tools wait for a run to end. Most catalog runs end within a minute; a large `maxItems` on a site that reads detail pages can take longer. Pass `abortSignal` to `generateText` to stop waiting; a run that already started continues and can be read with the [SDK](https://github.com/cleanedweb/cleanedweb-js#long-runs-and-recovery).

## Pricing

Searching and reading the catalog are free. Runs are charged per record delivered: 5,000 records free once, then prepaid credit from $1 per 1,000 records, with no subscription. A run that returns no records costs nothing. See [pricing](/pricing/).

## Use MCP instead

The same catalog is available to any MCP client at `https://api.cleanedweb.com/mcp?profile=catalog`, with run, schedule and source request tools. AI SDK agents can load it with `@ai-sdk/mcp`; see [MCP clients](/docs/mcp/).
