Skip to content

Server functions in the HTTP Server DuckDB extension

Function category

Server

2 functions

httpserve_start

Scalar function Server
Signature
httpserve_start( host: VARCHAR, port: INTEGER, auth: VARCHAR ) → VARCHAR
Arguments (Positional)
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
Description
1 HTTP Basic auth on localhost
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/
2 Single shared key (X-API-Key header)
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
3 No auth — loopback only
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 ...
4 Bind on all interfaces — read-only DuckDB, behind a TLS-terminating proxy
-- Launched as: duckdb -readonly mydata.duckdb
SELECT httpserve_start('0.0.0.0', 9999, 'shared-api-key');
5 Serve HTTPS directly (DuckDB 2.0 build)
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'
6 Let a web app on another origin call the server (DuckDB 2.0 build)
SET httpserver_allowed_origins = 'https://dashboard.example.com';
SELECT httpserve_start('localhost', 9999, 'supersecretkey');

httpserve_stop

Scalar function Server
Signature
httpserve_stop() → VARCHAR
Returns
Description
1 Graceful stop
SELECT httpserve_stop();
2 Rotate credential without restarting DuckDB
SELECT httpserve_stop();
SELECT httpserve_start('localhost', 9999, 'rotated-key');