Frameworks
/
Next.js

Next.js Integration Guide

Open LangGraph Server provides seamless integration with Next.js applications through its App Router. This guide covers everything you need to know to get started with Open LangGraph Server in your Next.js project.

Architecture Overview

The Next.js adapter is a thin wrapper (~40 lines of code) around the core fetch handler. It:

  • Extracts context from the x-langgraph-context header
  • Handles Next.js-specific initialization patterns
  • Passes requests to the standard fetch implementation

This architecture provides:

  • ✅ Framework-agnostic core logic
  • ✅ Consistent behavior across all platforms
  • ✅ Easy migration if you switch frameworks

Installation

Install Open LangGraph Server alongside your LangGraph dependencies:

For OpenAI integration (optional):

Project Structure

Recommended project structure for Next.js integration:

Basic Setup

1. Create the API Route

Create a new API route handler in your Next.js app:

Important: The Next.js adapter handles the framework's specific initialization requirements while using the same core fetch handler as other platforms. This ensures consistent API behavior across all deployments.

2. Register Your Graphs

Create a separate file for graph registration:

3. Create Your Graph

Define your LangGraph workflow:

Configuration

Environment Variables

Configure your storage backend and other settings:

Storage Setup

Advanced Usage

Context Injection with Middleware

Inject user-specific context into your graphs:

Access context in your graph:

Streaming Responses

Handle streaming responses in your client components:

Error Handling

Implement proper error handling for API calls:

Deployment Considerations

Environment Variables

Ensure all required environment variables are set in your deployment platform:

Database Migration

For production deployments with PostgreSQL:

Performance Optimization

  • Use Redis for checkpoint storage in production
  • Configure connection pooling for database connections
  • Implement proper caching strategies for frequently accessed data
  • Monitor memory usage and adjust TTL settings as needed

Troubleshooting

Common Issues

"Graph not found" error

  • Ensure your graph is properly registered in agent/index.ts
  • Check that the assistant_id matches the registered graph name

"Database connection failed"

  • Verify environment variables are correctly set
  • Check database server connectivity
  • Ensure proper permissions for database user

Streaming not working

  • Confirm stream_mode is set correctly in run requests
  • Check browser compatibility for Server-Sent Events
  • Verify middleware is not interfering with streaming responses

Context not injected

  • Ensure middleware is configured correctly
  • Check that x-langgraph-context header is being set
  • Verify context format matches expected structure

Alternative: Direct Fetch Handler

For simpler use cases or if you prefer direct control, you can use the core fetch handler:

This approach gives you full control while still using the same core implementation as other platforms.

Migration Benefits

Using the Next.js adapter (or fetch handler) provides:

  • Platform Flexibility: Same API logic can run on Vercel, Cloudflare, or any Node.js server
  • Consistent Behavior: Identical API responses across all platforms
  • Standard Web APIs: Based on WHATWG Fetch specification
  • Future Proof: Not locked into Next.js-specific APIs

Comparison with Other Platforms

FeatureNext.js AdapterHono AdapterDirect Fetch
Framework
Next.js App Router
Hono.js
Any
Context Source
x-langgraph-context header
Hono context variable
Custom
Initialization
ensureInitialized()
Standard
Manual
Code Size
~40 lines
~30 lines
0 lines
Core Logic
✅ Same
✅ Same
✅ Same

All adapters use the exact same core implementation, ensuring consistent behavior across platforms.

Next Steps

Built with

Show your support! Star us on GitHub ⭐️