For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Help on Discord
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
  • Introduction
    • What is BAML?
    • Why BAML?
    • What's the baml_src folder
    • What's baml_client
  • Installation: Editors
    • VSCode Extension
    • Cursor Extension
    • JetBrains IDEs
    • Zed
    • Claude Code
    • Others
  • Installation: Language
    • Python
    • Typescript
    • Go
    • Ruby
    • Rust
    • REST API (other languages)
    • Elixir
  • Framework Integration
      • Quick Start
      • Building a Chatbot
  • Development
    • Environment Variables
    • Terminal Logs
    • Upgrade BAML versions
  • BAML Basics
    • Prompting with BAML
    • Switching LLMs
    • Testing functions
    • Streaming
    • Multi-Modal (Images / Audio)
    • Error Handling
    • Configuring Timeouts
    • Concurrent Calls
    • AbortSignal / Cancellation
  • BAML Advanced
    • Collector (track tokens)
    • LLM Client Registry
    • Dynamic Types
    • Reusing Prompt Snippets
    • Prompt Caching / Message Role Metadata
    • Checks and Asserts
    • Modular API
    • Prompt Optimization
  • Boundary Cloud
  • Comparisons
    • BAML vs Langchain
    • BAML vs Marvin
    • BAML vs Ai-SDK
    • BAML vs OpenAI SDK
    • BAML vs Pydantic
    • Contact
Help on Discord
LogoLogo
On this page
  • Example Usage
  • Quick Start
  • Reference Documentation
  • Core Concepts
  • Hook Configuration
  • Next Steps
Framework IntegrationReact/Next.js

React/Next.js Setup

Was this page helpful?
Edit this page
Previous

Building a Chatbot with BAML React Hooks

Next
Built with

This guide walks you through setting up BAML with React/Next.js, leveraging Server Actions and React Server Components for optimal performance.

Requirements: This integration requires Next.js 15 or higher.

Example Usage

BAML automatically generates a server action and React hook for your BAML functions, with built-in support for both streaming and non-streaming modes. For details on the generated hooks, see Generated Hooks.

1class Story {
2 title string @stream.not_null
3 content string @stream.not_null
4}
5
6function WriteMeAStory(input: string) -> Story {
7 client "openai/gpt-5"
8 prompt #"
9 Tell me a story
10
11 {{ ctx.output_format() }}
12
13 {{ _.role("user") }}
14
15 Topic: {{input}}
16 "#
17}

Quick Start

Follow the step-by-step instructions below to set up BAML in a new or existing Next.js project.

1

Create a New Next.js Project

First, create a new Next.js project with the App Router:

$npx create-next-app@latest my-baml-app

When prompted, make sure to:

  • Select Yes for “Would you like to use TypeScript?”
  • Select Yes for “Would you like to use the App Router? (recommended)”
  • Configure other options as needed for your project
2

Install Dependencies

Next, install BAML and its dependencies:

$npm install @boundaryml/baml @boundaryml/baml-nextjs-plugin
3

Configure Next.js

Update your next.config.mjs:

1import { withBaml } from '@boundaryml/baml-nextjs-plugin';
2import type { NextConfig } from 'next';
3
4const nextConfig: NextConfig = {
5 // ... existing config
6};
7
8export default withBaml()(nextConfig);
4

Initialize BAML

Create a new BAML project in your Next.js application:

$npx baml-cli init

This will create a baml_src directory with starter code.

5

Setup Environment Variables

Setup provider specific API Keys.

.env.local
1OPENAI_API_KEY=sk-...
(Optional) BAML Observability

To enable observability with BAML, you’ll first need to sign up for a Boundary Studio account.

.env.local
1BOUNDARY_API_KEY=your_api_key_here
2
3OPENAI_API_KEY=sk-...
6

Setup BAML Next.js Generator

Update the baml_src/generators.baml file to use the React/Next.js generator.

baml_src/generators.baml
1generator typescript {
2- output_type "typescript"
3+ output_type "typescript/react"
4 output_dir "../"
5 version "0.76.2"
6}
7

Generate BAML Client

$npx baml-cli generate

If you need baml_client to be ‘ESM’ compatible, you can add the following generator configuration to your .baml file:

1generator typescript {
2 ...
3 module_format "esm" // the default is "cjs" for CommonJS
4}
8

Generated React Hooks

BAML automatically generates type-safe Next.js server actions and React hooks for your BAML functions.

1class Story {
2 title string @stream.not_null
3 content string @stream.not_null
4}
5
6function WriteMeAStory(input: string) -> Story {
7 client "openai/gpt-5"
8 prompt #"
9 Tell me a story
10
11 {{ ctx.output_format() }}
12
13 {{ _.role("user") }}
14
15 Topic: {{input}}
16 "#
17}
9

Update Package Scripts

Update your package.json scripts:

1{
2 "scripts": {
3 "prebuild": "npm run generate",
4 "generate": "baml-cli generate",
5 "dev": "next dev",
6 "build": "next build",
7 "start": "next start",
8 }
9}

Reference Documentation

For complete API documentation of the React/Next.js integration, see:

Core Concepts

  • Generated Hooks - Auto-generated hooks for each BAML function

Hook Configuration

  • HookInput - Configuration options for hooks
  • HookOutput - Return value types and states
  • Error Types - Error handling and types

Next Steps

  • Check out the BAML Examples for more use cases