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

# API overview

> Every global Fairyfly injects, one row each, linking to its full reference.

Everything below is available with **no imports** — Fairyfly injects these
globals into every script. Bare specifiers still resolve through
`node_modules` for packages (see [Packages](https://fairyfly.dev/guides/packages.md)).

Native capabilities are **deny-by-default**. The Permission column names the
flag a global needs; `—` means it is always available. Full model:
[Permissions](https://fairyfly.dev/concepts/permissions.md).

## Globals

| Global | What it is | Permission | Reference |
| - | - | - | - |
| `console` | Output, colored aliases, `time`/`timeLog`/`timeEnd` | — | [console](https://fairyfly.dev/api/console.md) |
| `setTimeout`, `clearTimeout` | One-shot timer (max 128 live) | — | [Timers](https://fairyfly.dev/guides/timers.md) |
| `setInterval`, `clearInterval` | Repeating timer | — | [Timers](https://fairyfly.dev/guides/timers.md) |
| `queueMicrotask` | Run before the next timer or I/O callback | — | [Event loop](https://fairyfly.dev/concepts/event-loop.md) |
| `performance` | `performance.now()` — monotonic ms | — | [performance](https://fairyfly.dev/api/performance.md) |
| `URL`, `URLSearchParams` | WHATWG URL parser, query strings | — | [URL](https://fairyfly.dev/api/url.md) |
| `Headers` | Case-insensitive header collection | — | [Request/Response](https://fairyfly.dev/api/request-response.md) |
| `Request` | Fetch request object | — | [Request/Response](https://fairyfly.dev/api/request-response.md) |
| `Response` | Fetch response object + `json`/`redirect`/`error` | — | [Request/Response](https://fairyfly.dev/api/request-response.md) |
| `AbortController`, `AbortSignal` | Cancellation controller + signal (`abort()`, `timeout(ms)`) | — | [fetch](https://fairyfly.dev/api/fetch.md) |
| `structuredClone` | Deep binary structured clone (max 4 MB) | — | [Worker](https://fairyfly.dev/api/worker.md) |
| `Blob` | Immutable binary blob | — | [Blob/FormData](https://fairyfly.dev/api/blobs-formdata.md) |
| `FormData` | `multipart/form-data` container | — | [Blob/FormData](https://fairyfly.dev/api/blobs-formdata.md) |
| `TextEncoder`, `TextDecoder` | UTF-8 string ↔ bytes | — | [Text encoding](https://fairyfly.dev/api/text-encoding.md) |
| `fetch` | HTTP client (16 slots, HTTP/1.1) | `net` | [fetch](https://fairyfly.dev/api/fetch.md) |
| `WebSocket` | WebSocket client (max 64 sockets) | `net` | [WebSocket](https://fairyfly.dev/api/websocket.md) |
| `Worker` | OS-thread worker (max 8) | — (inherits) | [Worker](https://fairyfly.dev/api/worker.md) |
| `http` | `http.serve(options, handler)` | `net` (+ `read` for TLS files) | [http](https://fairyfly.dev/api/http.md) |
| `udp` | Datagram sockets (`udp.socket`, max 64) | `net` | [udp](https://fairyfly.dev/api/udp.md) |
| `fs` | Sync + Promise filesystem calls | `read` / `write` | [fs](https://fairyfly.dev/api/fs.md) |
| `process` | `argv`, `env`, `exit`, `pid`, `on`, `kill`, `memoryUsage`… | `env`; `read` for `chdir`; `run` for external `kill` | [process](https://fairyfly.dev/api/process.md) |
| `child_process` | Subprocesses: `spawn` / `exec` / `execSync` (max 8, POSIX) | `run` | [child\_process](https://fairyfly.dev/api/child-process.md) |
| `crypto` | `randomUUID`, `getRandomValues`, `subtle` (`digest`, HMAC `importKey`/`sign`/`verify`/`exportKey`), `bcrypt` (`hash`, `verify` off-event-loop) | — | [crypto](https://fairyfly.dev/api/crypto.md) |
| `btoa`, `atob` | Base64 encode / decode | — | [crypto](https://fairyfly.dev/api/crypto.md) |
| `Database` | SQLite (`Database.open(path)`) | `read` + `write` | [SQLite](https://fairyfly.dev/api/sqlite.md) |
| `sql`, `SQL` | Postgres tagged templates + pool | `net` (+ `read` for `sslrootcert`); global `sql` also `env` | [Postgres](https://fairyfly.dev/api/postgres.md) |
| `ffi` | C-ABI shared libraries (`--allow-ffi`) | `ffi` | [FFI](https://fairyfly.dev/api/ffi.md) |
| `permissions` | Enabled categories as six booleans | — | [Permissions](https://fairyfly.dev/concepts/permissions.md) |
| `import.meta.url` | `file://` URL of the current module | — | [Modules](https://fairyfly.dev/guides/modules.md) |

Notes:

* `process.argv`, `pid`, `cwd`, `exit`, `on`, `once`, `off`, `memoryUsage`, and
  `stats` are not gated. Only `process.env` (`env`), `process.chdir` (`read` on
  the target), and `process.kill` to a PID other than `process.pid` (`run` for
  `"kill"`) need a flag.
* `Worker` loading is a program-loading carve-out: `new Worker(path)` does not
  need `read`. The worker inherits the process policy for everything it calls.
* `http` always needs `net` for its `0.0.0.0:<port>` bind. `read` is only
  checked when `tls.cert` / `tls.key` are file paths rather than inline PEM.
* `sql` / `SQL` checks `net` against the target host:port. If TLS is configured with `sslrootcert`, `read` on the CA bundle is also checked. The global `sql` pool checks `env` to discover `DATABASE_URL` / `PG*` settings.
* `Database.open(":memory:")` needs no path permission.

### Timer handles

`setTimeout` / `setInterval` return a `Timeout` **object** (pass it to
`clearTimeout` / `clearInterval`, or call its methods):

```js theme={null}
const t = setTimeout(() => console.log("later"), 5000);
t.unref();      // don't hold the process open
t.refresh();    // restart the countdown
t.hasRef();     // boolean
t.ref();        // hold the process again
```

### Static helpers you'll use constantly

```js theme={null}
Response.json({ ok: true });              // Response with JSON content-type
Response.redirect("/login");              // 302 status only — no Location header
URL.parse("not a url");                   // null instead of throwing
new URL("/a/b", "https://x.example");     // base resolution
crypto.randomUUID();                      // v4 UUID
Database.open("app.db");                  // SQLite
permissions.net;                          // true when --allow-net is active
```

Full tables: [Response statics and Database methods](#quick-reference).

## Quick reference

### `Response` statics

| Method | Signature | Description |
| - | - | - |
| `Response.json(value, init?)` | `(any, { status?, headers? }) → Response` | JSON response shortcut |
| `Response.redirect(url, status?)` | `(string, number?) → Response` | Redirect status (default 302) — Known issue: no `Location` header emitted |
| `Response.error()` | `() → Response` | Error response, status `0` |

### `Database` methods

| Method | Signature | Description |
| - | - | - |
| `Database.open(path)` | `(string) → Database` | Open or create a database; needs `read` + `write` on the file and `write` on its parent |
| `db.exec(sql, params?)` | `(string, Array?) → undefined` | One statement, bound params |
| `db.execNoArgs(sql)` | `(string) → undefined` | Multi-statement SQL, no params |
| `db.row(sql, params?)` | `(string, Array?) → object \| null` | First row |
| `db.rows(sql, params?)` | `(string, Array?) → object[]` | All rows |
| `db.changes()` | `() → number` | Rows affected by the last write |
| `db.lastInsertRowId()` | `() → number` | Rowid of the last insert |
| `db.transaction(fn)` | `(Function) → any` | Auto-commit / rollback |
| `db.busyTimeout(ms)` | `(number) → undefined` | Busy timeout (default 5000) |
| `db.close()` | `() → undefined` | Close the handle |

`Database.open(":memory:")` is exempt from the path gates.

### `sql` / `SQL` methods

| Method | Signature | Description | | |
| - | - | - | - | - |
| `sql`…\`\` | `(template) → Promise<rows>` | Parameterized query | | |
| `sql.unsafe(str, params?)` | `(string, Array?) → Promise<rows>` | String-built, still parameterized | | |
| `sql.connect()` | `() → Promise<rows>` | Probe the pool — resolves with the `SELECT 1` rows | | |
| `sql.begin()` | `() → Promise<Tx>` | Transaction on one connection | | |
| `sql.close()` | `() → undefined` | Close the default pool | | |
| `tx.commit()` / `tx.rollback()` | `() → Promise<rows>` | End the transaction (resolves `[]`) | | |
| \`new SQL(dsn | options)\` | \`(string | object) → SQL\` | Pool bound to a DSN or options object (`{ host, port, user, password, database, max, sslmode, sslrootcert }`) |

`new SQL(dsn | options)` checks `net` against the target host and port (and `read` on `sslrootcert` if TLS is enabled). The global `sql`
also needs `env` if it should read `PG_TEST_DSN` / `DATABASE_URL` / `PG*` (including `PGSSLMODE`, `PGSSLROOTCERT`) at
startup; otherwise it falls back to `127.0.0.1:5432`.

## Not provided

These common Node/browser globals are **absent** — with the substitute to
use instead:

| Missing | Use instead |
| - | - |
| `Buffer` | `Uint8Array` / `ArrayBuffer` |
| `require()` / CommonJS | `import` / `export` |
| `process.nextTick` | `queueMicrotask(fn)` |
| `setImmediate` | `setTimeout(fn, 0)` |
| `addEventListener` on `WebSocket` | `ws.onopen = …` style handlers |
| `TextEncoder#encodeInto` | `encode()` |
| `process.stdout.write` | `console.log` (goes to **stderr**) |
| DOM, `window` | backend runtime |

Full list with rationale: [Compatibility](https://fairyfly.dev/reference/compatibility.md).

## See also

* [Permissions](https://fairyfly.dev/concepts/permissions.md) — flags, scopes, error contract
* [Event loop](https://fairyfly.dev/concepts/event-loop.md) — how these globals interact
* [Limitations](https://fairyfly.dev/reference/limitations.md) — every hard cap
* [CLI](https://fairyfly.dev/reference/cli.md) — how to run your code


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.