Server functions in the HTTP Server DuckDB extension
Function category
Server
2 functionsStart and stop the embedded HTTP query listener. The third argument to httpserve_start selects the auth mode — empty (no auth), user:pass (HTTP Basic), or any other value (shared key in X-API-Key).
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
host
|
Type
VARCHAR
|
Mode Positional |
Description
Interface to bind on. Use 'localhost' (or '127.0.0.1' / '::1') to keep the server reachable only from the same machine. Use '0.0.0.0' to listen on all interfaces — only behind a firewall or reverse proxy. On the DuckDB 2.0 build a non-loopback address requires an auth credential unless httpserver_allow_unauthenticated_remote is set.
|
Argument
port
|
Type
INTEGER
|
Mode Positional |
Description
TCP port to listen on (0–65535). Any unused port; 9999 is conventional in the upstream examples. 0 lets the OS pick a free port, which the returned message names. A port another server is already listening on is an error.
|
Argument
auth
|
Type
VARCHAR
|
Mode Positional |
Description
Authentication selector. Empty string (or NULL) disables auth — on the DuckDB 2.0 build only allowed for loopback addresses unless httpserver_allow_unauthenticated_remote is set. A value containing ':' is parsed as user:password and enables HTTP Basic auth. Any other value is treated as a single shared key the client must send in the X-API-Key header. There is one credential per server — no per-user accounts. Each mode accepts only its own header: a user:password credential only through HTTP Basic, a key only in X-API-Key. Credentials are compared in constant time.
|
Returns
Confirmation string of the form 'HTTP server started on <host>:<port>' ('HTTPS server started on …' when TLS is configured).
Description
Start the embedded HTTP query server for the current database. The listener runs in a background thread inside the DuckDB process and runs each request on a fresh connection to the same database instance — extensions you've LOADed and databases you've ATTACHed are visible to HTTP clients; temporary tables and SET VARIABLE values are not.
Each database runs at most one server: calling httpserve_start while it is running raises HTTP server is already running — call httpserve_stop first. On the DuckDB 2.0 build the server belongs to the database that started it: closing that database stops it, and separate databases in one process can each serve on their own port. The port is bound before the function returns; if it can't be bound (in use, privileged, unknown interface), the call raises Failed to start HTTP server on <host>:<port> and nothing is left running.
Security defaults (DuckDB 2.0 build): without a credential only loopback addresses may be served; cross-origin browser requests are refused with 403 unless their origin is listed in httpserver_allowed_origins (no wildcard CORS by default); an unauthenticated loopback server rejects requests whose Host header isn't a loopback name (DNS rebinding); and httpserve_start is refused when enable_external_access is false. Set httpserver_tls_cert and httpserver_tls_key to serve HTTPS. The settings are read when the server starts.
Set DUCKDB_HTTPSERVER_FOREGROUND=1 before starting DuckDB to run the server on the main thread (the SQL caller blocks until Ctrl-C); set DUCKDB_HTTPSERVER_DEBUG=1 for stdout request logging or DUCKDB_HTTPSERVER_SYSLOG=1 to route logs to syslog; set DUCKDB_HTTPSERVER_BASEPATH=/prefix/ to serve the endpoints under a path prefix.
The served endpoints are:
/—GETandPOSTfor SQL queries. Pass SQL in the request body, or as?query=/?q=. The response isJSONEachRowby default; add?default_format=JSONCompact,CSV, orXMLto choose another shape (see ClickHouse formats — the names are borrowed from there). Failed queries return HTTP500with the error text./ping— unauthenticatedGEThealth check; returnsOK.- The browser UI (quackplay) is served from
/when a request carries no query.
SELECT httpserve_start('localhost', 9999, 'user:pass');
-- 'HTTP server started on localhost:9999'
-- From the shell:
-- curl -X POST -d "SELECT version()" http://user:pass@localhost:9999/
SELECT httpserve_start('localhost', 9999, 'supersecretkey');
-- From the shell:
-- curl -X POST --header 'X-API-Key: supersecretkey' \
-- -d 'SELECT 42' http://localhost:9999/?default_format=JSONCompact
SELECT httpserve_start('127.0.0.1', 9999, '');
-- Without a credential, other interfaces are refused (DuckDB 2.0 build):
-- SELECT httpserve_start('0.0.0.0', 9999, '');
-- Permission Error: httpserve_start: refusing to serve 0.0.0.0 without authentication ...
-- Launched as: duckdb -readonly mydata.duckdb
SELECT httpserve_start('0.0.0.0', 9999, 'shared-api-key');
SET httpserver_tls_cert = '/etc/duckdb/server.crt';
SET httpserver_tls_key = '/etc/duckdb/server.key';
SELECT httpserve_start('0.0.0.0', 9443, 'shared-api-key');
-- 'HTTPS server started on 0.0.0.0:9443'
SET httpserver_allowed_origins = 'https://dashboard.example.com';
SELECT httpserve_start('localhost', 9999, 'supersecretkey');
Signature
Returns
Confirmation string 'HTTP server stopped', also when no server was running.
Description
Stop the current database's HTTP server, closing the listening socket and waiting for in-flight requests to finish. Pair with httpserve_start when you want to rebind on a different port or rotate the auth credential without restarting DuckDB. Calling it when no server is running does nothing.
httpserve_stop can't be called from a query that the server itself is serving — that raises an error instead of waiting on its own request. On the DuckDB 2.0 build, closing the database (or exiting the process) also stops its server, so calling httpserve_stop is only needed for in-process restarts.
SELECT httpserve_stop();
SELECT httpserve_stop();
SELECT httpserve_start('localhost', 9999, 'rotated-key');