React/Next.js Setup
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.
Quick Start
Follow the step-by-step instructions below to set up BAML in a new or existing Next.js project.
Create a New Next.js Project
First, create a new Next.js project with the App Router:
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
Initialize BAML
Create a new BAML project in your Next.js application:
This will create a baml_src
directory with starter code.
Setup Environment Variables
Setup provider specific API Keys.
(Optional) BAML Observability
To enable observability with BAML, you’ll first need to sign up for a Boundary Studio account.
Setup BAML Next.js Generator
Update the baml_src/generators.baml
file to use the React/Next.js generator.
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