DuckDB Extension
HTTP Server
Serve SQL queries over HTTP from a running DuckDB process. Start a local endpoint, authenticate requests, and return query results as JSON.
On this page
Let an HTTP client query a running DuckDB database
HTTP Server starts a listener inside DuckDB. Clients submit SQL over HTTP and receive JSON results, giving scripts or an internal application access to an existing DuckDB session without a database client library.
The serving process must remain running. Choose which database and connection state it exposes, and configure authentication before giving other clients access. A read-only database is useful when the endpoint should serve queries without accepting changes.
Example
Start a local SQL endpoint
Run the SQL in an interactive DuckDB session, keep it open, and send the curl request from a second terminal.
Before you run it: Use a native DuckDB session and a free local port 9999. The process must remain running while you make the HTTP request.
Run this example in the DuckDB CLI or a native client.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL httpserver FROM query_farm;
LOAD httpserver FROM query_farm;
-- Demo credentials: change these before using your own data.
SELECT httpserve_start('127.0.0.1', 9999, 'demo:change-me');
-- In another terminal:
-- curl --user 'demo:change-me' --data 'SELECT 42 AS answer' \
-- http://127.0.0.1:9999/
-- After trying the request, stop the server with:
-- SELECT httpserve_stop();
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://haybarn-extensions.query.farm';
INSTALL httpserver FROM query_farm;
LOAD httpserver FROM query_farm;
-- Demo credentials: change these before using your own data.
SELECT httpserve_start('127.0.0.1', 9999, 'demo:change-me');
-- In another terminal:
-- curl --user 'demo:change-me' --data 'SELECT 42 AS answer' \
-- http://127.0.0.1:9999/
-- After trying the request, stop the server with:
-- SELECT httpserve_stop();
INSTALL httpserver FROM community;
LOAD httpserver;
-- Demo credentials: change these before using your own data.
SELECT httpserve_start('127.0.0.1', 9999, 'demo:change-me');
-- In another terminal:
-- curl --user 'demo:change-me' --data 'SELECT 42 AS answer' \
-- http://127.0.0.1:9999/
-- After trying the request, stop the server with:
-- SELECT httpserve_stop();
The endpoint executes SQL in this DuckDB process. The loopback address keeps this example local. See the read-only database recipe before serving data to other clients.
Common tasks
Deep Dive
Technical Details
These examples use DuckDB 2.0 or later. Complete the repository setup and installation first.
What you can do with one query
Turn a DuckDB process into an HTTP query API in one statement:
INSTALL httpserver FROM query_farm;LOAD httpserver FROM query_farm;
SELECT httpserve_start('localhost', 9999, 'user:pass');-- 'HTTP server started on localhost:9999'Now any HTTP client can POST SQL and read JSON back:
curl -X POST -d "SELECT version()" \ "http://user:pass@localhost:9999/?default_format=JSONCompact"httpserve_start spawns an in-process listener thread; httpserve_stop tears it down. There are no other functions — the entire extension is two SQL calls and a small set of HTTP endpoints.
Exposing a DuckDB connection over HTTP is genuinely risky. Treat this extension as a building block, not a finished product. Before binding to anything beyond localhost:
- Run DuckDB with
-readonly. Without it, any authenticated client canINSERT,ATTACH,COPY TO, install extensions, or call file-system functions (SQL injectionis the entire API here — there is no safe parameterization path). The upstream README’s first security note is exactly this. - Use TLS. By default the server speaks plain HTTP, and HTTP Basic over plaintext is effectively no auth. On the DuckDB 2.0 build, set
httpserver_tls_cert/httpserver_tls_keyto serve HTTPS directly (see Settings); otherwise use nginx, Caddy, or HAProxy as a TLS terminator. - One credential, no per-user scope. Auth is binary — the client either presents the configured key or it doesn’t. No users, no roles, no per-query allowlists, no rate limiting.
- Browsers on the same machine. The DuckDB 2.0 build refuses cross-origin browser requests (
403) unless the origin is listed inhttpserver_allowed_origins, and an unauthenticated loopback server rejects requests whoseHostheader isn’t a loopback name, so a web page can’t reach it through DNS rebinding. Earlier builds (including the Haybarn 1.5 build) sendAccess-Control-Allow-Origin: *on every response — there, always set a credential, even on loopback. - Lock down the DuckDB process itself. Follow DuckDB’s hardening guidance and consider disabling community-extension installation on the served process.
For internal APIs on a private network, behind a TLS proxy, against a read-only database — this extension is a great fit. For anything multi-tenant or internet-exposed, a dedicated SQL gateway is the right tool.
Architecture
httpserve_start starts an HTTP listener thread inside the DuckDB process. Each request runs on a fresh connection to the same database instance — so any extension you’ve LOADed, any database you’ve ATTACHed, and any secret you’ve CREATEd is visible to HTTP clients. Connection-local state is not: temporary tables, SET VARIABLE values, and session settings from the calling connection are invisible to HTTP queries. Each database runs at most one server; calling httpserve_start while it is running raises HTTP server is already running, so call httpserve_stop first. On the DuckDB 2.0 build the server belongs to the database that started it: closing that database stops the server, and two databases in one process can each serve on their own port. (Earlier builds have one global server per process that keeps the database alive until it is stopped.)
httpserve_start binds the port before it returns. If the address can’t be bound (port in use — including by another server in the same process, privileged port, unknown interface), it raises Failed to start HTTP server on <host>:<port> and nothing is left running, so you can retry with another port. Pass port 0 to let the OS pick a free port; the returned message names the port actually bound.
The endpoint surface is intentionally small:
| Endpoint | Methods | Purpose |
|---|---|---|
/ |
GET, POST | Execute the SQL in the request body or in ?query= / ?q=; a request with no query gets the embedded quackplay UI. |
/ping |
GET | Liveness check (unauthenticated, returns OK). |
There is no /metrics, no /health JSON, and no OpenAPI document — anything beyond send SQL, read JSON lives outside this extension.
The response format vocabulary mirrors the ClickHouse HTTP interface:
?default_format=JSONEachRow— newline-delimited rows (JSONEachRow). This is the default. On the DuckDB 2.0 build values keep their JSON types, as inJSONCompact({"a":42,"b":true,"c":[1,2]};HUGEINTstays a string); earlier builds render every non-NULLvalue as a JSON string ({"a":"42"}).?default_format=JSONCompact— single document withmeta,data,rows, andstatisticskeys (JSONCompact), with typed values.?default_format=CSV— a header row followed by comma-separated rows.?default_format=XML— a<results>document with one<row>per result row.
The format can also be sent in an X-ClickHouse-Format or format request header. A failed query returns HTTP 500 with the DuckDB error message as plain text.
Four environment variables tune the runtime: DUCKDB_HTTPSERVER_FOREGROUND=1 runs the server on the main thread (so the SQL caller blocks), DUCKDB_HTTPSERVER_DEBUG=1 enables stdout request logging, DUCKDB_HTTPSERVER_SYSLOG=1 routes logs to syslog, and DUCKDB_HTTPSERVER_BASEPATH=/duckdb/ serves the endpoints under a path prefix (/duckdb/ and /duckdb/ping) instead of /.
Authentication
The third argument to httpserve_start selects the auth mode:
auth argument |
Mode |
|---|---|
'' (empty) or NULL |
No auth. On the DuckDB 2.0 build only loopback addresses (127.x, ::1, localhost) may be served unless httpserver_allow_unauthenticated_remote is set. |
'user:pass' (one colon) |
HTTP Basic auth. |
| any other value | Shared key sent by clients in the X-API-Key header. |
Both auth modes are single-credential — there is one user/pass or one key per server. Each mode accepts only its own header: a 'user:pass' server rejects X-API-Key: user:pass, and a key server rejects HTTP Basic credentials. A Basic-mode 401 carries WWW-Authenticate, so browsers prompt for the login. Multi-tenant or per-user access control belongs at a reverse proxy. The credential is compared in constant time but travels in plaintext over HTTP, so TLS in front is non-negotiable for anything beyond a loopback bind.
To rotate a credential without restarting DuckDB, call httpserve_stop and then httpserve_start again with the new value.
Settings
The DuckDB 2.0 build adds four settings. httpserve_start reads them when it starts the server, so change them before starting (or restart the server).
| Setting | Default | Effect |
|---|---|---|
httpserver_allowed_origins |
'' |
Comma-separated origins (e.g. https://app.example.com) whose web pages may call the server; they get CORS headers. '*' allows any origin (still without Allow-Credentials). Other cross-origin browser requests — an Origin that isn’t the server itself, or Sec-Fetch-Site: cross-site — are refused with 403. Non-browser clients (curl, httpfs, scripts) aren’t affected. |
httpserver_allow_unauthenticated_remote |
false |
Allow an empty credential on a non-loopback address. Anyone who can reach the port can run any SQL. |
httpserver_tls_cert |
'' |
PEM certificate (chain) file. With httpserver_tls_key, the server speaks HTTPS and the start message reads HTTPS server started on …. |
httpserver_tls_key |
'' |
PEM private key file for httpserver_tls_cert. |
Starting a network listener is external access: when enable_external_access is false, httpserve_start raises a permission error.
Scoping the served database
The HTTP endpoint inherits the DuckDB connection’s surface in full. The most reliable way to make that surface safe is at the DuckDB level, not the HTTP level:
# Read-only — INSERT / UPDATE / COPY TO / ATTACH all fail.# FOREGROUND=1 keeps the listener on the main thread, so the process stays up.DUCKDB_HTTPSERVER_FOREGROUND=1 duckdb -readonly mydata.duckdb -c " LOAD httpserver FROM query_farm; SELECT httpserve_start('0.0.0.0', 9999, 'shared-api-key');"Once the server is running you can also switch off external access for everything it serves — SET enable_external_access = false; blocks file, network, and extension access for HTTP queries (it can’t be switched back on, and httpserve_start is refused afterwards, so start the server first).
See the DuckDB CLI docs and the securing DuckDB page for the full lockdown vocabulary — read-only mode, restricting extension installation, and disabling filesystem functions.
Calling the server from another DuckDB
Any DuckDB can hit the endpoint as an HTTP source. Configure an extra_http_headers secret for the API key, then use read_json_auto, or pair with the chsql community extension’s duck_flock macro to fan a single SQL statement out across multiple DuckDB endpoints:
CREATE SECRET extra_http_headers ( TYPE HTTP, EXTRA_HTTP_HEADERS MAP { 'X-API-Key': 'supersecretkey' });
-- Fan-out across three DuckDB nodes; results are unioned.SELECT * FROM duck_flock( 'SELECT version()', ['http://node-a:9999', 'http://node-b:9999', 'http://node-c:9999']);This is the closest pattern to “distributed DuckDB” that exists today — small, no consensus layer, no shared catalog, but adequate for read-side federation across curated slices.
Compared to alternatives
- The DuckDB CLI over SSH — works for one operator and trivial to set up, but doesn’t help when several services need to query the same DuckDB. The HTTP server replaces “SSH and run a query” with “POST a query.”
- Hand-rolled FastAPI / Express service — more flexible (per-user auth, request validation, custom response shapes) but adds another runtime to deploy. This extension is the right fit when the SQL is the API.
- Airport over Arrow Flight — the right tool when both ends are inside a data system and you want columnar Arrow on the wire. Use httpserver when the client is a generic HTTP consumer (a browser, a Lambda, a curl in a cron job).
- ADBC Scanner or Airport outbound — for querying something else from DuckDB. httpserver is the inverse direction: making this DuckDB queryable.
Changes in the DuckDB 2.0 release
- No auth means loopback only.
httpserve_start('0.0.0.0', port, '')now raises a permission error. Pass a credential, bind127.0.0.1, orSET httpserver_allow_unauthenticated_remote = true. - No wildcard CORS. Cross-origin browser requests get
403unless their origin is listed inhttpserver_allowed_origins. Set it to'*'for the old behaviour. JSONEachRowkeeps JSON types — numbers and booleans are no longer quoted.enable_external_access = falserefuseshttpserve_start.- A port that is already listening is an error, even when the other server is in the same process (earlier builds could silently share it).
Also new in the DuckDB 2.0 build:
- Per-database servers. The server belongs to the database that started it, stops when that database closes, and no longer keeps a closed database alive. Start and stop are thread-safe.
- Built-in TLS through
httpserver_tls_cert/httpserver_tls_key. - DNS-rebinding protection for unauthenticated loopback servers.
httpserve_stop()from a served query raises an error instead of hanging the server.- Crash fixes: a failed bind is reported as an error instead of aborting the process.
httpserve_start/httpserve_stopare volatile and documented induckdb_functions()(descriptions, examples, parameter names).
Recipe overview
Recipes — start the server, query it from curl, query it from another DuckDB, run quackplay — live in the Cookbook section below.
Reference
Extension Contents
Quick reference to all available functions and settings organized by category.
| Name | Type | Description |
|---|---|---|
|
Configuration
|
||
| httpserver_allow_unauthenticated_remote | Object type: Setting | Allow httpserve_start to listen on a non-loopback address without an auth credential, letting anyone who can reach it run any SQL. |
| httpserver_allowed_origins | Object type: Setting | Comma-separated origins (e.g. |
| httpserver_tls_cert | Object type: Setting | Path to a PEM certificate (chain); with httpserver_tls_key set, httpserve_start serves HTTPS. |
| httpserver_tls_key | Object type: Setting | Path to the PEM private key for httpserver_tls_cert. |
|
Server
Start and stop the embedded HTTP query listener. The third argument to |
||
| httpserve_start() | Object type: Scalar function | Start the embedded HTTP query server for the current database. |
| httpserve_stop() | Object type: Scalar function | Stop the current database's HTTP server, closing the listening socket and waiting for in-flight requests to finish. |
No extension contents match that search.
API Reference
Function Reference
Configuration
Settings
Configure the http server extension behavior using these settings.
httpserver_allow_unauthenticated_remote
Allow httpserve_start to listen on a non-loopback address without an auth credential, letting anyone who can reach it run any SQL.
false
httpserver_allowed_origins
Comma-separated origins (e.g. https://app.example.com) whose web pages may call the HTTP server, or * for any. Other cross-origin browser requests are refused. Read by httpserve_start.
—
httpserver_tls_cert
Path to a PEM certificate (chain); with httpserver_tls_key set, httpserve_start serves HTTPS.
—
httpserver_tls_key
Path to the PEM private key for httpserver_tls_cert.
—
Practical Examples
Cookbook
Real-world recipes and patterns for common use cases.
These examples use DuckDB 2.0 or later. Complete the repository setup and installation first.
Start the server
INSTALL httpserver FROM query_farm;LOAD httpserver FROM query_farm;
SELECT httpserve_start('localhost', 9999, 'user:pass');-- 'HTTP server started on localhost:9999'The third argument to httpserve_start selects the auth mode — empty disables auth, user:pass enables HTTP Basic, anything else is treated as a shared key in X-API-Key.
Query from curl with HTTP Basic auth
curl -X POST -d "SELECT version()" \ "http://user:pass@localhost:9999/?default_format=JSONCompact"Body is the SQL; response is JSONCompact — meta, data, rows, statistics.
Query from curl with a shared API key
SELECT httpserve_start('localhost', 9999, 'supersecretkey');curl -X POST --header "X-API-Key: supersecretkey" \ -d "SELECT 'hello', version()" \ "http://localhost:9999/?default_format=JSONEachRow"JSONEachRow (the default when no format is given) returns newline-delimited objects — on the DuckDB 2.0 build with typed values (SELECT 42 AS answer gives {"answer":42}), on earlier builds with every non-NULL value as a JSON string — see the ClickHouse format reference. CSV and XML are also accepted.
Pass SQL as a query parameter
GET / ?query= works for short queries (mind URL length limits):
curl --header "X-API-Key: supersecretkey" \ "http://localhost:9999/?q=SELECT%2042&default_format=JSONCompact"Pull JSON results into another DuckDB
A second DuckDB can read the endpoint with read_json_auto, passing the API key via an extra_http_headers secret:
LOAD httpfs;LOAD json;
CREATE SECRET extra_http_headers ( TYPE HTTP, EXTRA_HTTP_HEADERS MAP { 'X-API-Key': 'supersecretkey' });
SELECT * FROM read_json_auto( 'http://localhost:9999/?q=SELECT version()&default_format=JSONEachRow');Fan a query across multiple DuckDB nodes
The chsql community extension provides a duck_flock macro that broadcasts one SQL statement to several HTTP endpoints and unions the results:
INSTALL chsql FROM community;LOAD chsql;
CREATE SECRET extra_http_headers ( TYPE HTTP, EXTRA_HTTP_HEADERS MAP { 'X-API-Key': 'supersecretkey' });
SELECT *FROM duck_flock( 'SELECT now() AS server_time, version()', ['http://node-a:9999', 'http://node-b:9999', 'http://node-c:9999']);Run quackplay in the browser
SELECT httpserve_start('localhost', 9999, 'user:pass');Open http://localhost:9999/ in a browser. Authenticate, then paste queries into the embedded quackplay SQL UI. Useful for ad-hoc exploration without a separate client. (The UI is marked experimental upstream.)
Serve a read-only DuckDB to a private network
DUCKDB_HTTPSERVER_FOREGROUND=1 duckdb -readonly mydata.duckdb -c " LOAD httpserver FROM query_farm; SELECT httpserve_start('0.0.0.0', 9999, 'shared-api-key');"-readonly is the simplest hard guarantee against mutation through the HTTP endpoint — see the DuckDB CLI docs. DUCKDB_HTTPSERVER_FOREGROUND=1 makes httpserve_start block, which keeps the one-shot duckdb -c process alive until it is interrupted.
Serve HTTPS directly
On the DuckDB 2.0 build the server can terminate TLS itself — point it at a PEM certificate and key before starting:
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'curl --header "X-API-Key: shared-api-key" "https://duckdb.internal:9443/?q=SELECT%2042"Call the server from a web app on another origin
The DuckDB 2.0 build refuses cross-origin browser requests unless the page’s origin is allowed. List the origins (comma-separated) before starting the server:
SET httpserver_allowed_origins = 'https://dashboard.example.com, http://localhost:5173';SELECT httpserve_start('localhost', 9999, 'supersecretkey');Pages on those origins get CORS headers and can fetch() the endpoint with an X-API-Key or Authorization header; other sites get 403. '*' allows any origin. quackplay, served by the server itself, needs no setting.
Serve without auth on a private network
Without a credential, the DuckDB 2.0 build only binds loopback addresses. To serve an unauthenticated endpoint on a trusted network (for example behind an authenticating gateway), opt in explicitly:
SET httpserver_allow_unauthenticated_remote = true;SELECT httpserve_start('0.0.0.0', 9999, '');Foreground mode and logging
Set these in the environment before launching DuckDB:
DUCKDB_HTTPSERVER_FOREGROUND=1 # Run the listener on the main thread (the SQL caller blocks).DUCKDB_HTTPSERVER_DEBUG=1 # Verbose request logging to stdout.DUCKDB_HTTPSERVER_SYSLOG=1 # Route logs to syslog.DUCKDB_HTTPSERVER_BASEPATH=/duckdb/ # Serve / and /ping under /duckdb/ instead.FOREGROUND=1 is the simplest way to keep the process alive when launching from a one-shot duckdb -c '...' invocation in a container.
Health check
curl -i http://localhost:9999/ping/ping is unauthenticated and returns 200 with the body OK — wire it to a load balancer or docker healthcheck.
Rotate the credential
SELECT httpserve_stop();SELECT httpserve_start('localhost', 9999, 'rotated-key');httpserve_stop tears down the listener; the next httpserve_start can rebind on a different port or with a different credential without restarting DuckDB.
Stop the server
SELECT httpserve_stop();Or just exit the DuckDB process — the server thread shuts down with it. On the DuckDB 2.0 build, closing the database that started the server stops it too.
Platform Support
Compatibility
Extension availability may vary by platform and DuckDB version. Check below to ensure this extension supports your environment before installation.
Quick Facts
Platforms
DuckDB 2.0.0
- Linux x86_64aarch64
- Linux (musl) Not available
- macOS Apple Silicon
- Windows x86_64x86_64 (MinGW)
- WASM Not available
Compiled binary sizes
| Platform | Architecture | Size |
|---|---|---|
| Linux | x86_64 | 17.71 MB |
| Linux | aarch64 | 15.67 MB |
| macOS | Apple Silicon | 13.18 MB |
| Windows | x86_64 | 11.84 MB |
| Windows | x86_64 (MinGW) | 20.54 MB |
Compressed download sizes for the release shown above.
DuckDB & Haybarn
Release calendar- DuckDB v2.0.0 Query.Farm repository Supported
Browser examples use Haybarn-Wasm 2.0.0-rc12.
Checked .
Learn and explore
Tutorials and articles
Writing about HTTP Server: how it works, what changed, and why.
-
HTTP Server for DuckDB 2.0: a server that belongs to its database
DuckDB’s HTTP Server extension now owns servers per database, requires authentication for remote access, supports TLS, and reports startup failures clearly.