Vercel AI SDK


title: Vercel AI SDK subtitle: Using OpenRouter with Vercel AI SDK headline: ‘Vercel AI SDK Integration | OpenRouter SDK Support’ canonical-url: https://openrouter.ai/docs/guides/community/vercel-ai-sdk og:site_name: OpenRouter Documentation og:title: ‘Vercel AI SDK Integration - OpenRouter SDK Support’ og:description: ‘Integrate OpenRouter using Vercel AI SDK. Complete guide for Vercel AI SDK integration with OpenRouter for Next.js applications.’ og:image: https://openrouter.ai/dynamic-og?title=Vercel%20AI%20SDK&description=Vercel%20AI%20SDK%20Integration og:image:width: 1200 og:image:height: 630 twitter:card: summary_large_image twitter:site: ‘@OpenRouterAI’ noindex: false nofollow: false

Vercel AI SDK

You can use the Vercel AI SDK to integrate OpenRouter with your Next.js app. To get started, install @openrouter/ai-sdk-provider:

$npm install @openrouter/ai-sdk-provider

And then you can use streamText() API to stream text from OpenRouter.

TypeScript
1import { createOpenRouter } from '@openrouter/ai-sdk-provider';
2import { streamText } from 'ai';
3import { z } from 'zod';
4
5export const getLasagnaRecipe = async (modelName: string) => {
6 const openrouter = createOpenRouter({
7 apiKey: '${API_KEY_REF}',
8 });
9
10 const response = streamText({
11 model: openrouter(modelName),
12 prompt: 'Write a vegetarian lasagna recipe for 4 people.',
13 });
14
15 await response.consumeStream();
16 return response.text;
17};
18
19export const getWeather = async (modelName: string) => {
20 const openrouter = createOpenRouter({
21 apiKey: '${API_KEY_REF}',
22 });
23
24 const response = streamText({
25 model: openrouter(modelName),
26 prompt: 'What is the weather in San Francisco, CA in Fahrenheit?',
27 tools: {
28 getCurrentWeather: {
29 description: 'Get the current weather in a given location',
30 parameters: z.object({
31 location: z
32 .string()
33 .describe('The city and state, e.g. San Francisco, CA'),
34 unit: z.enum(['celsius', 'fahrenheit']).optional(),
35 }),
36 execute: async ({ location, unit = 'celsius' }) => {
37 // Mock response for the weather
38 const weatherData = {
39 'Boston, MA': {
40 celsius: '15°C',
41 fahrenheit: '59°F',
42 },
43 'San Francisco, CA': {
44 celsius: '18°C',
45 fahrenheit: '64°F',
46 },
47 };
48
49 const weather = weatherData[location];
50 if (!weather) {
51 return `Weather data for ${location} is not available.`;
52 }
53
54 return `The current weather in ${location} is ${weather[unit]}.`;
55 },
56 },
57 },
58 });
59
60 await response.consumeStream();
61 return response.text;
62};