Quick Setup
Initialize Tinybird
Run the init command in your Next.js project:This creates:
tinybird.config.json- Configuration filesrc/tinybird/datasources.ts- Define your datasourcessrc/tinybird/pipes.ts- Define your pipes/endpointssrc/tinybird/client.ts- Your typed Tinybird client
Add environment variables
Create The CLI automatically loads
.env.local with your Tinybird token:.env.local and .env files.Set up concurrent development
Install Update your
concurrently for running Next.js and Tinybird together:package.json scripts:Project Structure
Recommended structure for Next.js + Tinybird:Creating API Routes
Use Tinybird in Next.js API routes:App Router (Next.js 13+)
Pages Router (Next.js 12)
Server Components
Query Tinybird directly in React Server Components:Client Components
Fetch data in client components:Event Tracking
Implement analytics tracking:Server-Side Tracking
Client-Side Hook
Tracking API Route
Development Workflow
Local Development
Run both services with automatic sync:- Next.js - Serves your app and API routes
- Tinybird - Watches for schema changes and syncs to Tinybird
Making Schema Changes
- Edit datasource or pipe definitions in TypeScript
- Save the file
- Tinybird CLI automatically syncs changes
- TypeScript types update automatically
- Use updated types in your Next.js code
Building for Production
Environment Variables
Manage tokens across environments:Development (.env.local)
Production (Vercel/Deployment)
Set environment variables in your deployment platform:Multiple Environments
Best Practices
Use concurrent development
Always run
tinybird dev alongside next dev using concurrently for the best development experience.Use Server Components for analytics
Fetch analytics data in Server Components when possible to avoid exposing tokens to the client.
Implement error boundaries
Wrap analytics components in error boundaries to prevent tracking failures from breaking your app.
Deployment
Vercel Deployment
-
Set environment variables in Vercel dashboard:
TINYBIRD_TOKENTINYBIRD_BASE_URL(optional)
-
Add build command:
-
Deploy:
Other Platforms
For other platforms (Netlify, Railway, etc.):- Set
TINYBIRD_TOKENenvironment variable - Update build command to run
tinybird deployfirst - Ensure Node.js 20+ is available
Complete Example
A full Next.js app with Tinybird integration:Next Steps
JWT Tokens
Create secure tokens for client-side access
Type-Safe Client
Learn more about the Tinybird client API