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

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-4o"
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-...

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

.env.local
1BOUNDARY_PROJECT_ID=sk-...
2BOUNDARY_SECRET=sk-...
3
4OPENAI_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
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-4o"
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

Hook Configuration

Next Steps

Built with