Skip to main content
Bun provides a set of built-in APIs that make common JavaScript tasks faster and easier. These APIs are available globally without imports and are optimized for performance.

Core APIs

Bun.serve()

High-performance HTTP server with WebSocket support

Bun.file()

Fast file I/O operations with streaming support

Bun.write()

Write files, sockets, or stdout with automatic type conversion

Bun.spawn()

Launch child processes with full control over stdio

Database & Storage

bun:sqlite

Fast SQLite database with prepared statements

Bun.sql()

Unified SQL API for PostgreSQL, MySQL, and SQLite

Bun.redis()

High-performance Redis client

Bun.s3()

S3-compatible object storage client

Utilities

Bun.build()

JavaScript bundler with tree-shaking

Bun.Transpiler

Transpile TypeScript and JSX to JavaScript

bun:ffi

Call native C/C++/Rust/Zig code from JavaScript

Bun.hash()

Fast hashing functions (wyhash, xxHash, CRC32, SHA)

Performance Features

All Bun APIs are designed for speed:
  • Zero-copy operations: File reads and writes use efficient memory operations
  • Streaming support: Handle large files without loading into memory
  • Native implementations: Written in Zig for maximum performance
  • Optimized allocations: Smart memory management reduces garbage collection

Example: Using Multiple APIs

Here’s an example that combines several Bun APIs:
server.ts
import { Database } from "bun:sqlite";

const db = new Database("mydb.sqlite");
db.run("CREATE TABLE IF NOT EXISTS visits (count INTEGER)");
db.run("INSERT INTO visits VALUES (0)");

Bun.serve({
  port: 3000,
  async fetch(req) {
    // Increment visit counter
    const result = db.query("UPDATE visits SET count = count + 1 RETURNING count").get();
    
    // Read HTML file
    const html = Bun.file("index.html");
    
    // Return response with counter
    return new Response(await html.text().replace("{{count}}", result.count), {
      headers: { "Content-Type": "text/html" }
    });
  },
});

console.log("Server running at http://localhost:3000");

Global Objects

Bun extends the global namespace with several utility objects:

Bun

The main Bun namespace contains most APIs:
Bun.version      // Bun version string
Bun.revision     // Git commit SHA
Bun.env          // Environment variables
Bun.main         // Path to entrypoint file
Bun.sleep(ms)    // Sleep for milliseconds
Bun.sleepSync(ms) // Synchronous sleep
Bun.which(bin)   // Find executable in PATH
Bun.peek(promise) // Check promise status without awaiting

import.meta

Enhanced with Bun-specific properties:
import.meta.dir    // Directory of current file
import.meta.file   // Filename of current file
import.meta.path   // Full path to current file
import.meta.main   // Is this the entrypoint?
import.meta.url    // File URL of current module
import.meta.resolve(specifier) // Resolve module path
See import.meta documentation for more details.

Node.js Compatibility

Bun implements most Node.js built-in modules. See Node.js Compatibility for details.

Web APIs

Bun implements web standards like fetch, WebSocket, and Streams. See Web APIs for details.

Next Steps

HTTP Server

Build high-performance web servers

File I/O

Read and write files efficiently

SQLite

Work with SQLite databases

All APIs

Browse all runtime APIs

Build docs developers (and LLMs) love