Skip to main content

Nuxt with AI SDK

Learn how to integrate the AI SDK into Nuxt applications for server-side AI features and streaming responses.

Why Nuxt?

Nuxt is an excellent framework for AI applications:
  • Server Routes: Built-in API routes with TypeScript
  • Auto-imports: Composables and utilities available everywhere
  • SSR/SSG: Server-side rendering and static generation
  • File-based Routing: Intuitive project structure
  • Vue 3: Reactive UI with Composition API

Prerequisites

  • Node.js 18+
  • Basic knowledge of Vue and Nuxt
  • Vercel AI Gateway API key

Quick Start

Create a new Nuxt application:
Install AI SDK dependencies:
Configure environment variables:
Update nuxt.config.ts:

Basic Chat API

Create a streaming chat endpoint:

Chat Component

Create a Vue component with useChat:

Text Generation

Generate text on-demand: API Route:
Component:

Structured Output

Generate typed objects: API Route:
Component:

Tool Calling

Implement tools in your Nuxt API:

Server Composables

Create reusable AI utilities:
Use in API routes:

Multi-Modal Input

Handle images in chat:

Middleware

Protect AI routes with middleware:

Error Handling

Handle AI errors gracefully:

Deployment

Environment Variables

Set in .env or deployment platform:

Build and Deploy

Best Practices

  1. Use Server Routes: Keep AI logic server-side
  2. Lazy Event Handlers: Use defineLazyEventHandler for better performance
  3. Runtime Config: Access environment variables securely
  4. Type Safety: Leverage TypeScript throughout
  5. Error Handling: Use Nuxt’s error utilities
  6. Composables: Create reusable AI utilities
  7. Caching: Cache responses when appropriate

Example Repository

View the complete example: github.com/vercel/ai/examples/nuxt-openai

Resources