DEV Community

Alex Spinov
Alex Spinov

Posted on

Vercel AI SDK Has a Free API You Should Know About

Vercel AI SDK is a TypeScript toolkit for building AI-powered applications with React Server Components, streaming, and tool calling — designed for the Next.js ecosystem.

Why Vercel AI SDK for Web AI Apps

A Next.js developer wanted to add a streaming chatbot. Raw fetch calls with SSE parsing, state management for messages, and UI updates took 500 lines. Vercel AI SDK: 20 lines.

Key Features:

  • Streaming — Token-by-token streaming with React hooks
  • Multi-Provider — OpenAI, Anthropic, Google, Mistral, and more
  • Tool Calling — Define and execute tools from the frontend
  • Generative UI — Stream React components from the server
  • Edge Runtime — Works with Vercel Edge Functions

Quick Start

npm install ai @ai-sdk/openai
Enter fullscreen mode Exit fullscreen mode

Server (Next.js Route)

import { openai } from "@ai-sdk/openai"
import { streamText } from "ai"

export async function POST(req: Request) {
  const { messages } = await req.json()
  const result = streamText({ model: openai("gpt-4"), messages })
  return result.toDataStreamResponse()
}
Enter fullscreen mode Exit fullscreen mode

Client (React)

import { useChat } from "ai/react"

export function Chat() {
  const { messages, input, handleInputChange, handleSubmit } = useChat()
  return (
    <div>
      {messages.map(m => <div key={m.id}>{m.role}: {m.content}</div>)}
      <form onSubmit={handleSubmit}>
        <input value={input} onChange={handleInputChange} />
      </form>
    </div>
  )
}
Enter fullscreen mode Exit fullscreen mode

Why Choose Vercel AI SDK

  1. React-native — hooks for chat, completion, and streaming
  2. Multi-provider — switch models with one line
  3. Next.js optimized — server components, edge runtime

Check out Vercel AI SDK docs to get started.


Building AI web apps? Check out my Apify actors or email spinov001@gmail.com for custom solutions.

Top comments (0)