SDK modules
Besides the globals in the Handler API, MicroHost ships four optional modules. They are reserved import specifiers served by the host, written in plain JavaScript, versioned with the SDK API version (currently 1), and loaded lazily: a handler that imports none of them does no extra work.
| Module (import) | Manifest sdk name |
Purpose | Needs | Engines |
|---|---|---|---|---|
microhost/sql |
sql |
The sql tagged template for composable, parameterized SQL. |
nothing by itself | Wasm, native, Linux |
microhost/db (alias db) |
db |
Object rows, table objects, paging, all/one/run/tx, id(), now(). |
the database grant | Wasm, native, Linux |
microhost/auth |
auth |
hashPassword, verifyPassword (Argon2id). |
no database grant | Wasm, native, Linux |
microhost/session |
session |
Cookie sessions stored in your database. | the database grant, a site_sessions table |
all engines (see below) |
Reference pages: Database SDK for sql and db, Auth and sessions for auth and session.
Status: session on native and Linux. Implemented once in the shared SDK layer for every engine. Verified end to end on Wasm (dev VM, 2026-10-25); native and Linux load the same module and dispatch, but a full cycle against real SQLite is not yet tested there.
microhost/auth,microhost/sqlandmicrohost/dbwork on all three engines.
Importing
import { sql } from "microhost/sql";
import { all, one, run, tx, id, now, tasks, tables } from "microhost/db"; // same as: from "db"
import { hashPassword, verifyPassword } from "microhost/auth";
import { create, get, destroy, destroyAll } from "microhost/session";
- The names are matched exactly.
dbis an exact alias ofmicrohost/db. Your own files can never shadow them, because your relative imports must start with./or../and end in.js. - Only static
importworks; dynamicimport()does not. microhost/dbimportsmicrohost/sqlinternally. You do not need to listsqlseparately when you listdb.- The module named
microhost(withdefineSite,route, ...) is not a handler module. It exists only insidemicrohost.config.js.
Requesting modules in the manifest
List the modules your release uses in the manifest's sdk array:
{ "schemaVersion": 1, "sdk": ["db", "auth", "session"], "permissionsRequested": { "database": true } }
or, in microhost.config.js, defineSite({ sdk: ["db"], ... }).
Rules (checked at deploy; a bad list rejects the manifest):
- Allowed names:
auth,db,session,sql. Names must be unique; unknown names are rejected. The stored list is sorted. dbalso enablessqlinternally.- Current behaviour when the list is absent: a manifest without an
sdkkey leaves every module importable (subject to the site switch below). Once ansdklist is present, only the listed modules (plussqlfordb) can be imported; others fail withsdk_module_disabled. Treat the list as required documentation of intent and do not rely on omission. The resolved-manifest builder drops an emptysdkarray, so an empty list frommicrohost.config.jsis the same as omitting it.
The per-site module switch
An administrator can disable a module for a site without redeploying:
GET /admin/api/v1/sites/{id}/sdk-modulesreturns{ site_id, release_id, version, requested, disabled, effective, editable }.PATCH /admin/api/v1/sites/{id}/sdk-moduleswith{ "expected_version": <n>, "disabled": ["session"] }replaces the disabled list. Unknown or duplicate names give 400invalid_sdk_modules; a stale version gives 409sdk_modules_version_conflict. It needs site write access, and it is audited.- The effective set is requested minus disabled: the intersection of what the release asked for and what the site allows. Disabling
sqlalso disablesdb; thedbswitch and thedbalias are the same switch. - The change applies to the next request. It does not change the release.
- If a module is disabled (or not requested while an
sdklist exists), importing it fails withsdk_module_disabled. Visitors see HTTP 500; the code appears in operator diagnostics. The check happens before any schema or source is read.
The switch is a developer convenience, not authorization. It never grants anything. In particular, ctx.db is governed only by the database grant: disabling db does not remove raw ctx.db, and enabling db does not give database access. Every SDK database call goes through the same broker checks as ctx.db.
Versioning
The SDK API version (SDK_API_VERSION = 1) is pinned in each release record. Module behaviour for existing releases does not change when the platform upgrades. A behaviour change requires a new SDK version and new release pinning. Table exports of microhost/db come from the release's sealed schema snapshot (see Migrations); a release created before snapshots existed gets none until it is redeployed, but raw ctx.db keeps working for it.