Bun
Build blazing-fast, type-safe APIs with Igniter.js and Bun, the all-in-one JavaScript runtime.
Bun is a modern JavaScript runtime that's incredibly fast, with built-in bundling, testing, and package management. When combined with Igniter.js, you get exceptional performance with zero configuration—no build tools, no bundlers, just pure speed and developer happiness.
This guide shows you how to build production-ready APIs with Bun and Igniter.js. You'll experience Bun's lightning-fast startup times, instant hot reload, and native TypeScript support, all while maintaining Igniter.js's type-safe controllers and automatic API documentation.
Maximum Performance
Bun + Igniter.js delivers incredible performance out of the box. Bun's runtime is 4x faster than Node.js, with instant startup and built-in TypeScript support.
Quick Start
The fastest way to start is using the official CLI with Bun's native speed:
npx @igniter-js/cli@latest initpnpm dlx @igniter-js/cli@latest inityarn dlx @igniter-js/cli@latest initbunx @igniter-js/cli@latest initYour API runs at http://localhost:3000/api/v1, and interactive docs at http://localhost:3000/api/v1/docs 🚀
Direct Command
If you prefer to skip the interactive prompts, you can specify your project name and framework directly:
For REST API
npx @igniter-js/cli@latest init my-bun-api --framework bun-rest-apipnpm dlx @igniter-js/cli@latest init my-bun-api --framework bun-rest-apiyarn dlx @igniter-js/cli@latest init my-bun-api --framework bun-rest-apibunx @igniter-js/cli@latest init my-bun-api --framework bun-rest-apiFor Fullstack React App
npx @igniter-js/cli@latest init my-bun-app --framework bun-react-apppnpm dlx @igniter-js/cli@latest init my-bun-app --framework bun-react-appyarn dlx @igniter-js/cli@latest init my-bun-app --framework bun-react-appbunx @igniter-js/cli@latest init my-bun-app --framework bun-react-appThese commands will create Bun projects with all Igniter.js features pre-configured.
Manual Setup
Complete Bun Setup Guide
Prerequisites
Before you begin, ensure you have:
- Bun 1.0+ (install here)
- Basic knowledge of TypeScript
Install Dependencies
Install Igniter.js and Zod for schema validation:
bun add @igniter-js/core zodBun has native TypeScript support—no additional type packages or build tools needed!
Define Application Context
The context defines dependencies available throughout your API. Create src/igniter.context.ts:
/**
* Application Context Type
* Defines what dependencies are available to all API handlers
*/
export interface AppContext {
// Add your dependencies here, e.g.:
// db: Database
// cache: RedisClient
}
/**
* Context Factory
* Creates the context instance passed to handlers
*/
export function createIgniterAppContext(): AppContext {
return {
// Initialize dependencies here
}
}Initialize Igniter.js
Create the main Igniter instance. Create src/igniter.ts:
import { Igniter } from '@igniter-js/core'
import { createIgniterAppContext } from './igniter.context'
export const igniter = Igniter
.context(createIgniterAppContext())
.config({
baseURL: process.env.IGNITER_API_URL || 'http://localhost:3000',
basePATH: process.env.IGNITER_API_BASE_PATH || '/api/v1',
})
.docs({
info: {
title: 'My Bun API',
version: '1.0.0',
description: 'High-performance API built with Bun and Igniter.js',
}
})
.create()Create Your First Controller
Controllers group related endpoints. Create src/features/example/controllers/example.controller.ts:
import { igniter } from '@/igniter'
export const exampleController = igniter.controller({
name: 'Example',
path: '/example',
actions: {
hello: igniter.query({
path: '/hello',
handler: async ({ response }) => {
return response.success({
message: 'Hello from Igniter.js with Bun! ⚡',
timestamp: new Date().toISOString(),
runtime: 'Bun'
})
},
}),
},
})Create barrel export at src/features/example/index.ts:
export * from './controllers/example.controller'This creates GET /api/v1/example/hello with fully typed responses.
Create Application Router
The router assembles all controllers. Create src/igniter.router.ts:
import { igniter } from './igniter'
import { exampleController } from './features/example'
export const AppRouter = igniter.router({
controllers: {
example: exampleController
}
})
export type AppRouterType = typeof AppRouterSet Up Bun Server
Bun has a built-in HTTP server that's incredibly fast. Create src/index.ts:
import { serve } from 'bun'
import { AppRouter } from './igniter.router'
const IGNITER_API_BASE_PATH = process.env.IGNITER_API_BASE_PATH || '/api/v1/'
const server = serve({
routes: {
// Mount Igniter.js router
[IGNITER_API_BASE_PATH + '*']: AppRouter.handler,
},
})
console.log(`🚀 Server running at ${server.url}`)Bun's native routing is simple and performant—no framework overhead needed!
Step 7: Add Development Scripts
Update your package.json:
{
"scripts": {
"dev": "bun --hot src/index.ts",
"start": "bun src/index.ts"
}
}Now run bun dev for instant hot reload! Changes apply in milliseconds.
Bun-Specific Features
Native TypeScript
Bun runs TypeScript directly—no transpilation needed:
// Just write TypeScript and run it!
import type { User } from './types'
const user: User = { name: 'John' }Environment Variables
Bun has built-in .env support. Create .env:
IGNITER_API_BASE_PATH=/api/v1/
IGNITER_API_URL=http://localhost:3000Access them via Bun.env:
const port = Bun.env.PORT || 3000Built-in Testing
Bun includes a fast test runner compatible with Jest:
import { describe, test, expect } from 'bun:test'
import { api } from '@/igniter.client'
describe('Example Controller', () => {
test('returns hello message', async () => {
const result = await api.example.hello.query()
expect(result.message).toContain('Bun')
})
})Run tests with bun test - it's incredibly fast!
Full-Stack with Bun React
For full-stack applications, use the bun-react-app template which includes React:
import { serve } from 'bun'
import { AppRouter } from './igniter.router'
import index from './index.html'
const server = serve({
routes: {
// Serve React app for all unmatched routes
'/*': index,
// Serve API
'/api/v1/*': AppRouter.handler,
},
development: Bun.env.NODE_ENV !== 'production' && {
hmr: true, // Hot module reload
console: true, // Browser console logging
},
})
console.log(`🚀 Server running at ${server.url}`)This single file serves both your React frontend and Igniter.js API with hot reload!
Type-Safe Client
Create a type-safe client for consuming your API. Create src/igniter.client.ts:
import { createIgniterClient } from '@igniter-js/core/client'
import type { AppRouterType } from './igniter.router'
export const api = createIgniterClient<AppRouterType>({
baseURL: Bun.env.IGNITER_API_URL || 'http://localhost:3000',
basePATH: Bun.env.IGNITER_API_BASE_PATH || '/api/v1',
router: () => require('./igniter.router').AppRouter,
})
export type ApiClient = typeof apiUse it anywhere:
// Fully typed, instant autocomplete
const { message, runtime } = await api.example.hello.query()Generating Schema and Docs
For full-stack Bun projects (Bun + React), you'll need to generate the client schema whenever you modify your API.
When to Generate Schema
Critical: You MUST run schema generation BEFORE using the client in your frontend. The client depends on the generated schema to function correctly.
Run this command whenever you modify your API structure (add/remove/modify controllers or actions) OR before using the client for the first time.
Generate Schema
Run this command whenever you make changes to your API structure:
npx @igniter-js/cli@latest generate schemapnpm dlx @igniter-js/cli@latest generate schemayarn dlx @igniter-js/cli@latest generate schemabunx @igniter-js/cli@latest generate schemaThis creates:
src/igniter.schema.ts- Type-safe schema for client-side usagesrc/docs/openapi.json- OpenAPI 3.0 specification
Generate OpenAPI Documentation
To generate or update the OpenAPI specification and interactive documentation:
npx @igniter-js/cli@latest generate docspnpm dlx @igniter-js/cli@latest generate docsyarn dlx @igniter-js/cli@latest generate docsbunx @igniter-js/cli@latest generate docsIgniter Studio (API Playground)
Igniter.js includes an interactive API playground called Igniter Studio. It's automatically available at /api/v1/docs when you configure the .docs() method.
To access it:
- Start your dev server:
bun dev - Navigate to
http://localhost:3000/api/v1/docs - You'll see an interactive interface where you can:
- Browse all API endpoints
- View request/response schemas
- Test endpoints directly
- See auto-generated examples
The playground updates automatically as you add endpoints.
Performance Tips
Use Bun's Native APIs
Bun provides optimized native APIs for common tasks:
// File I/O (much faster than Node.js fs)
const file = Bun.file('data.json')
const data = await file.json()
// Hashing
const hash = Bun.hash('sha256', 'data')
// Environment variables
const secret = Bun.env.SECRET_KEYDatabase with Bun
Bun works great with Prisma, the recommended ORM for Igniter.js:
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()Project Structure
Here's the recommended structure: