> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/zhcndoc/bun/llms.txt
> Use this file to discover all available pages before exploring further.

# Bun APIs

> Explore Bun's built-in APIs for common JavaScript tasks

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

<CardGroup cols={2}>
  <Card title="Bun.serve()" icon="server" href="/api/http">
    High-performance HTTP server with WebSocket support
  </Card>

  <Card title="Bun.file()" icon="file" href="/api/file-io">
    Fast file I/O operations with streaming support
  </Card>

  <Card title="Bun.write()" icon="pen-to-square" href="/api/file-io">
    Write files, sockets, or stdout with automatic type conversion
  </Card>

  <Card title="Bun.spawn()" icon="terminal" href="/api/spawn">
    Launch child processes with full control over stdio
  </Card>
</CardGroup>

## Database & Storage

<CardGroup cols={2}>
  <Card title="bun:sqlite" icon="database" href="/api/sqlite">
    Fast SQLite database with prepared statements
  </Card>

  <Card title="Bun.sql()" icon="database" href="/api/sql">
    Unified SQL API for PostgreSQL, MySQL, and SQLite
  </Card>

  <Card title="Bun.redis()" icon="circle-nodes" href="/api/redis">
    High-performance Redis client
  </Card>

  <Card title="Bun.s3()" icon="cloud" href="/api/s3">
    S3-compatible object storage client
  </Card>
</CardGroup>

## Utilities

<CardGroup cols={2}>
  <Card title="Bun.build()" icon="layer-group" href="/bundler/index">
    JavaScript bundler with tree-shaking
  </Card>

  <Card title="Bun.Transpiler" icon="arrows-rotate" href="/api/transpiler">
    Transpile TypeScript and JSX to JavaScript
  </Card>

  <Card title="bun:ffi" icon="plug" href="/api/ffi">
    Call native C/C++/Rust/Zig code from JavaScript
  </Card>

  <Card title="Bun.hash()" icon="hashtag" href="/api/hashing">
    Fast hashing functions (wyhash, xxHash, CRC32, SHA)
  </Card>
</CardGroup>

## 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:

```typescript server.ts theme={null}
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:

```typescript theme={null}
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:

```typescript theme={null}
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](/api/utils#importmeta) for more details.

## Node.js Compatibility

Bun implements most Node.js built-in modules. See [Node.js Compatibility](/runtime/nodejs-compat) for details.

## Web APIs

Bun implements web standards like fetch, WebSocket, and Streams. See [Web APIs](/runtime/web-apis) for details.

## Next Steps

<CardGroup cols={2}>
  <Card title="HTTP Server" icon="server" href="/api/http">
    Build high-performance web servers
  </Card>

  <Card title="File I/O" icon="file" href="/api/file-io">
    Read and write files efficiently
  </Card>

  <Card title="SQLite" icon="database" href="/api/sqlite">
    Work with SQLite databases
  </Card>

  <Card title="All APIs" icon="book" href="/runtime/index">
    Browse all runtime APIs
  </Card>
</CardGroup>
