Blog

Hybrid data: a SQLite database on your server that Cloudflare Workers can query

How to create a bridge in AI Hosting Panel, send only its query path through a Cloudflare Tunnel, and query the database from a Worker, with the statement rules that keep the database safe.

Hybrid data: a SQLite database on your server that Cloudflare Workers can query

2026-11-22

Hybrid data keeps a SQLite database on your own server, and lets Cloudflare Workers query it through a Cloudflare Tunnel. The data stays on this server: the Worker asks for rows, and the answer comes back through the tunnel. Each database is a bridge, with its own ID and token.

Create a bridge

  1. Open AI in the sidebar, then Hybrid data.
  2. Type a name for the bridge, for example Shop orders.
  3. Press Create bridge. The panel makes an empty SQLite database on this server and lists the bridge.

Set up the tunnel once

Create a Cloudflare Tunnel on this server with cloudflared tunnel create, and route a hostname to it. The ingress rule below sends only the bridge query path to the panel, and answers every other request with a 404. Replace bridge.example.com with your hostname, and your-panel-address:2087 with your panel's address. The httpHostHeader in the example is the panel's address and port.

ingress:
  - hostname: bridge.example.com
    path: ^/api/v1/hybrid/bridges/
    service: https://your-panel-address:2087
    originRequest:
      httpHostHeader: your-panel-address:2087
  - service: http_status:404
Hybrid data page: the New bridge form and the cloudflared ingress rule for the tunnel
The New bridge form, and the cloudflared ingress rule to copy into the tunnel's configuration.

Query it from a Worker

Give the Worker three secrets: BRIDGE_URL (the panel's address), BRIDGE_ID (the bridge's ID) and BRIDGE_TOKEN (the bridge's token). Then deploy a Worker like this one, which reads the last 20 notes:

export default {
  async fetch(request, env) {
    const res = await fetch(env.BRIDGE_URL + "/api/v1/hybrid/bridges/" + env.BRIDGE_ID + "/query", {
      method: "POST",
      headers: { "content-type": "application/json", authorization: "Bearer " + env.BRIDGE_TOKEN },
      body: JSON.stringify({ sql: "SELECT id, body FROM notes ORDER BY id DESC LIMIT 20", params: [] }),
    });
    return Response.json(await res.json());
  },
};

The statement rules

The panel checks each statement before it runs. Send one statement per request: a second statement after a semicolon is refused. ATTACH, DETACH, PRAGMA, VACUUM and extensions are refused, because they reach beyond the database. A request is refused when its SQL is longer than 20,000 characters or carries more than 100 parameters. At most 1000 rows come back. Put text values in params, not in the statement.

Good practice

Give each Worker its own bridge, so one token can be removed without affecting the others. Keep the tunnel's ingress as narrow as possible: only the bridge query path should reach the panel.

All posts