Skip to main content

Built-in APIs

Pun provides several built-in APIs for common tasks.

Pun.serve()

Create a high-performance HTTP server.

Pun.serve({
port: 3000,
fetch(req: Request) {
return new Response("Hello, Pun!");
}
});

ServerConfig

  • port: number: The port to listen on.
  • fetch: (req: Request) => Response | Promise<Response>: The handler function for incoming requests.

Pun.file()

Access files for reading.

const file = Pun.file("data.json");
const data = await file.json();

Methods

  • text(): Promise<string>: Read file as text.
  • json(): Promise<any>: Read file as JSON.
  • arrayBuffer(): Promise<ArrayBuffer>: Read file as an ArrayBuffer.

Pun.write()

Write data to files.

await Pun.write("output.txt", "Hello, Pun!");
await Pun.write("data.json", { key: "value" });

Web Standard APIs

Pun supports many standard Web APIs out of the box:

  • fetch()
  • Request, Response, Headers
  • URL, URLSearchParams
  • TextEncoder, TextDecoder
  • AbortController
  • ReadableStream, WritableStream

Node.js Compatibility

Pun provides partial compatibility with Node.js APIs:

  • process: process.env, process.argv, process.cwd(), process.exit(), etc.
  • Buffer: Binary data handling.