|
| 1 | +--- |
| 2 | +title: 🔺 Vercel AI SDK integration |
| 3 | +sidebar_label: Vercel AI SDK |
| 4 | +description: Learn how to integrate Apify Actors as tools for AI with Vercel AI SDK 🔺. |
| 5 | +sidebar_position: 2 |
| 6 | +slug: /integrations/vercel-ai-sdk |
| 7 | +--- |
| 8 | + |
| 9 | +**Learn how to integrate Apify Actors as tools for AI with Vercel AI SDK.** |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## What is the Vercel AI SDK |
| 14 | + |
| 15 | +[Vercel AI SDK](https://ai-sdk.dev/) is the TypeScript toolkit designed to help developers build AI-powered applications and agents with React, Next.js, Vue, Svelte, Node.js, and more. |
| 16 | + |
| 17 | +:::note Explore Vercel AI SDK |
| 18 | + |
| 19 | +For more in-depth details on Vercel AI SDK, check out its [official documentation](https://ai-sdk.dev/docs/introduction). |
| 20 | + |
| 21 | +::: |
| 22 | + |
| 23 | +## How to use Apify with Vercel AI SDK |
| 24 | + |
| 25 | +Apify is a marketplace of ready-to-use web scraping and automation tools, AI agents, and MCP servers that you can equip your own AI with. This guide demonstrates how to use Apify tools with a simple AI agent built with Vercel AI SDK. |
| 26 | + |
| 27 | + |
| 28 | +### Prerequisites |
| 29 | + |
| 30 | +- **Apify API token**: To use Apify Actors in Vercel AI SDK, you need an Apify API token. Learn how to obtain it in the [Apify documentation](https://docs.apify.com/platform/integrations/api). |
| 31 | +- **Node.js packages**: Install the following Node.js packages: |
| 32 | + |
| 33 | + ```bash |
| 34 | + npm install @modelcontextprotocol/sdk @openrouter/ai-sdk-provider ai |
| 35 | + ``` |
| 36 | + |
| 37 | +### Building a simple pub search AI agent using Apify Google Maps scraper |
| 38 | + |
| 39 | +First, import all required packages: |
| 40 | + |
| 41 | +```typescript |
| 42 | +import { experimental_createMCPClient as createMCPClient, generateText, stepCountIs } from 'ai'; |
| 43 | +import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; |
| 44 | +import { createOpenRouter } from '@openrouter/ai-sdk-provider'; |
| 45 | +``` |
| 46 | +
|
| 47 | +Connect to the Apify MCP server and get all available tools for the AI agent: |
| 48 | +
|
| 49 | +:::warning |
| 50 | +
|
| 51 | +Make sure to set the `APIFY_TOKEN` environment variable with your Apify API token before running the code. |
| 52 | +
|
| 53 | +::: |
| 54 | +
|
| 55 | +```typescript |
| 56 | +// Connect to the Apify MCP server and get the available tools |
| 57 | +const url = new URL('https://mcp.apify.com'); |
| 58 | +const mcpClient = await createMCPClient({ |
| 59 | + transport: new StreamableHTTPClientTransport(url, { |
| 60 | + requestInit: { |
| 61 | + headers: { |
| 62 | + "Authorization": `Bearer ${process.env.APIFY_TOKEN}` |
| 63 | + } |
| 64 | + } |
| 65 | + }), |
| 66 | +}); |
| 67 | +const tools = await mcpClient.tools(); |
| 68 | +console.log('Tools available:', Object.keys(tools).join(', ')); |
| 69 | +``` |
| 70 | +
|
| 71 | +Create Apify OpenRouter LLM provider so we can run the AI agent: |
| 72 | +
|
| 73 | +:::tip |
| 74 | +
|
| 75 | +By using the [Apify OpenRouter](https://apify.com/apify/openrouter) you don't need to provide an API key for the OpenRouter or any other LLM provider since only Apify API token is required - your token usage is automatically charged against your Apify account. |
| 76 | +
|
| 77 | +::: |
| 78 | +
|
| 79 | +```typescript |
| 80 | +// Configure the Apify OpenRouter LLM provider |
| 81 | +const openrouter = createOpenRouter({ |
| 82 | + baseURL: 'https://openrouter.apify.actor/api/v1', |
| 83 | + apiKey: 'api-key-not-required', |
| 84 | + headers: { |
| 85 | + "Authorization": `Bearer ${process.env.APIFY_TOKEN}` |
| 86 | + } |
| 87 | +}); |
| 88 | +``` |
| 89 | +
|
| 90 | +Run the AI agent with the Apify Google Maps scraper tool to find a pub near the Ferry Building in San Francisco: |
| 91 | +
|
| 92 | +```typescript |
| 93 | +// Run the AI agent and generate a response |
| 94 | +const response = await generateText({ |
| 95 | + model: openrouter('x-ai/grok-4-fast'), |
| 96 | + tools, |
| 97 | + stopWhen: stepCountIs(5), |
| 98 | + messages: [ |
| 99 | + { |
| 100 | + role: 'user', |
| 101 | + content: [{ type: 'text', text: 'Find a pub near the Ferry Building in San Francisco using the Google Maps scraper.' }], |
| 102 | + }, |
| 103 | + ], |
| 104 | +}); |
| 105 | +console.log('Response:', response.text); |
| 106 | +console.log('\nDone!'); |
| 107 | +await mcpClient.close(); |
| 108 | +``` |
| 109 | +
|
| 110 | +## Resources |
| 111 | +
|
| 112 | +- [Apify Actors](https://docs.apify.com/platform/actors) |
| 113 | +- [Vercel AI SDK documentation](https://ai-sdk.dev/docs/introduction) |
| 114 | +- [What are AI agents?](https://blog.apify.com/what-are-ai-agents/) |
| 115 | +- [Apify MCP Server documentation](https://docs.apify.com/platform/integrations/mcp) |
0 commit comments