Quickstart
From zero to first query
In this tutorial, we’ll create a small shop database, point AskDB at it, ask a question in plain English, and run the SQL that AskDB returns.
Before you start
Section titled “Before you start”You need:
- Node.js 22.23.4 or newer, with npm, pnpm or Yarn 4. Run
node --versionto check. - An OpenAI API key. You can switch to another model provider later: see Bring your own model.
- macOS 11 or newer, Windows, or Linux with glibc 2.34 or newer, on x64 or arm64. The SQLite driver ships prebuilt binaries for these.
1. Create a sample database
Section titled “1. Create a sample database”First, make a folder for the project and set it up as a package:
mkdir askdb-shopcd askdb-shopnpm init -ymkdir askdb-shopcd askdb-shoppnpm initmkdir askdb-shopcd askdb-shopyarn init -2yarn init -2 sets the project to use Yarn 4. It also starts a Git repository in the folder.
Now create shop.sql in that folder. It defines three tables and adds 4 customers, 3 products and 6 orders:
CREATE TABLE customers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, country TEXT NOT NULL);CREATE TABLE products ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, category TEXT NOT NULL, price NUMERIC(10, 2) NOT NULL);CREATE TABLE orders ( id INTEGER PRIMARY KEY, customer_id INTEGER NOT NULL REFERENCES customers (id), product_id INTEGER NOT NULL REFERENCES products (id), quantity INTEGER NOT NULL, ordered_at TEXT NOT NULL);INSERT INTO customers VALUES (1, 'Ada', 'UK'), (2, 'Grace', 'US'), (3, 'Linus', 'FI'), (4, 'Margaret', 'US');INSERT INTO products VALUES (1, 'Keyboard', 'hardware', 49.99), (2, 'Monitor', 'hardware', 199.00), (3, 'IDE license', 'software', 89.00);INSERT INTO orders VALUES (1, 1, 1, 1, '2026-01-05'), (2, 1, 3, 1, '2026-01-06'), (3, 2, 2, 2, '2026-02-10'), (4, 3, 1, 1, '2026-03-01'), (5, 4, 2, 1, '2026-04-20'), (6, 4, 3, 2, '2026-04-21');Next, create make-shop.mjs. It loads shop.sql into a new SQLite file with Node’s built-in SQLite module:
import { readFileSync } from "node:fs";import { DatabaseSync } from "node:sqlite";
new DatabaseSync("shop.db").exec(readFileSync("shop.sql", "utf8"));console.log("Created shop.db");Run it:
node make-shop.mjsIt prints Created shop.db, and a new shop.db file appears in the folder. That’s our database. On Node 22, Node also prints an ExperimentalWarning about SQLite, which is safe to ignore here. To start over, delete shop.db first: running the script twice fails with table customers already exists.
2. Install AskDB and scaffold the config
Section titled “2. Install AskDB and scaffold the config”Install the AskDB CLI into the project. pnpm and Yarn install only releases that are at least a day old, and AskDB releases often: within a day of a release, they would pick an older one, which can fail. So their tabs first let AskDB’s packages skip that wait. Both settings cover AskDB’s packages only, and only in this folder. pnpm writes its setting to pnpm-workspace.yaml, and Yarn writes its setting to .yarnrc.yml.
npm install --save-dev askdbpnpm config set --location project minimumReleaseAgeExclude '["askdb","@askdb/*"]' --jsonpnpm add --save-dev askdbyarn config set npmPreapprovedPackages --json '["askdb","@askdb/*"]'yarn add --dev askdbThen let askdb init write the config for our database:
npx askdb init --yes --database sqlite --sqlite-file ./shop.dbpnpm exec askdb init --yes --database sqlite --sqlite-file ./shop.dbyarn askdb init --yes --database sqlite --sqlite-file ./shop.db--yes skips the setup questions and picks OpenAI as the model provider. The output starts with the files it wrote and the packages it installed:
Wrote: - …/askdb-shop/askdb.config.ts - …/askdb-shop/.env.example
Installing dependencies with npm in …/askdb-shop …
added 2 packages in 497msInstalled: @askdb/config, better-sqlite3.Wrote: - …/askdb-shop/askdb.config.ts - …/askdb-shop/.env.example
Installing dependencies with pnpm in …/askdb-shop ……dependencies:+ @askdb/config 1.0.0-rc.1+ better-sqlite3 13.0.3…Installed: @askdb/config, better-sqlite3.Wrote: - …/askdb-shop/askdb.config.ts - …/askdb-shop/.env.example
Installing dependencies with yarn in …/askdb-shop …➤ YN0000: · Yarn 4.18.1…➤ YN0004: │ better-sqlite3@npm:13.0.3 lists build scripts, but all build scripts have been disabled.…Installed: @askdb/config, better-sqlite3.The YN0004 warning is safe to ignore here. Yarn skipped the SQLite driver’s build script, and the driver doesn’t need one on the systems in Before you start: it ships prebuilt binaries for them.
Open askdb.config.ts:
import { defineConfig, env, type AskDbConfig } from "@askdb/config";
export default defineConfig({ ai: { provider: "openai", providerConfig: { openai: { apiKey: env("OPENAI_API_KEY"), }, }, language: { model: env("OPENAI_MODEL"), }, }, introspection: { provider: "sqlite", providerConfig: { sqlite: { file: "./shop.db", }, }, outputDir: "./askdb", }, rag: { embedder: "mock", store: "file", storeConfig: { file: {}, }, },} satisfies AskDbConfig);Notice two parts. introspection tells AskDB where our database is. ai tells it which model to use, and reads the API key from an environment variable called OPENAI_API_KEY.
3. Add your API key
Section titled “3. Add your API key”AskDB reads environment variables from a .env file in the folder you run it from. Copy the example file that askdb init wrote:
cp .env.example .envIn the Windows Command Prompt, use copy instead of cp.
Open .env and put your key after OPENAI_API_KEY=. Leave OPENAI_MODEL empty to use AskDB’s default OpenAI model:
OPENAI_API_KEY=<your OpenAI API key>OPENAI_MODEL=.env holds your key, so keep it out of version control. askdb.config.ts holds no secrets and is safe to commit.
4. Read the schema
Section titled “4. Read the schema”Now let AskDB read the structure of our database:
npx askdb introspectpnpm exec askdb introspectyarn askdb introspectIt prints nothing when it succeeds. Look in the new askdb folder instead: askdb/schema.json is the schema artifact. It lists our three tables, their columns and types, and the two links from orders to customers and products.
The schema artifact is what AskDB gives the model. When we ask a question, AskDB sends the model our question and this schema, never the rows in shop.db. Privacy model lists everything that’s sent.
5. Ask a question
Section titled “5. Ask a question”Ask the question in plain English:
npx askdb ask --question "How much has each customer spent in total, highest first?"pnpm exec askdb ask --question "How much has each customer spent in total, highest first?"yarn askdb ask --question "How much has each customer spent in total, highest first?"AskDB prints a -- sql -- line, then the SQL. It should look something like this:
SELECT c.id, c.name, SUM(o.quantity * p.price) AS total_spentFROM customers cJOIN orders o ON c.id = o.customer_idJOIN products p ON o.product_id = p.idGROUP BY c.id, c.nameORDER BY total_spent DESC;The model writes the SQL, so yours can differ: other aliases, another layout, or other columns. Notice what AskDB did and didn’t do. It checked the SQL before printing it: for example, it returns only read-only SQL. It didn’t run the SQL. Running it is our job.
If you see no AI API key configured, check that .env has your key (step 3).
6. Run the SQL
Section titled “6. Run the SQL”Copy the SQL that AskDB printed (everything after the -- sql -- line) into a new file called query.sql.
Then create run-query.mjs. It opens shop.db read-only and runs query.sql:
import { readFileSync } from "node:fs";import { DatabaseSync } from "node:sqlite";
const sql = readFileSync("query.sql", "utf8");const db = new DatabaseSync("shop.db", { readOnly: true });console.table(db.prepare(sql).all());Run it:
node run-query.mjsThe output should look something like this:
┌─────────┬────┬────────────┬─────────────┐│ (index) │ id │ name │ total_spent │├─────────┼────┼────────────┼─────────────┤│ 0 │ 2 │ 'Grace' │ 398 ││ 1 │ 4 │ 'Margaret' │ 377 ││ 2 │ 1 │ 'Ada' │ 138.99 ││ 3 │ 3 │ 'Linus' │ 49.99 │└─────────┴────┴────────────┴─────────────┘That’s the answer to our question: Grace bought two monitors, so she has spent the most.
What we built
Section titled “What we built”We made a database, and AskDB read its schema. Then we asked a question in plain English and got back checked SQL. Our own code ran that SQL read-only and printed the answer. Your own database works the same way: point askdb init at it, introspect, and ask. Try another question, such as “Which products has nobody from the US bought?”, and run its SQL the same way.
If SQLite won’t install: use Postgres in Docker
Section titled “If SQLite won’t install: use Postgres in Docker”The SQLite driver, better-sqlite3, has prebuilt binaries only for the systems listed in Before you start. On Linux with glibc older than 2.34 (Ubuntu 20.04, Debian 11 and Amazon Linux 2, for example), use Postgres in Docker instead. Run ldd --version to see your glibc version. You also need Docker running.
-
In step 1, create the folder and
shop.sql, but skipmake-shop.mjs. -
Start Postgres and load the tables:
Terminal window docker run -d --name askdb-shop-db -e POSTGRES_PASSWORD=askdb \-p 5432:5432 postgres:17docker exec -i askdb-shop-db psql -U postgres < shop.sqlIf the second command can’t connect, Postgres is still starting. Wait a few seconds and run it again.
-
In step 2, run the
askdb initcommand without the--databaseand--sqlite-fileflags. Postgres is the default. -
In step 3, also set
DATABASE_URLin.env:Terminal window -
Do steps 4 and 5 as written.
-
In step 6, save the SQL as
query.sql, then run it withpsqlinstead ofrun-query.mjs:Terminal window docker exec -i askdb-shop-db psql -U postgres < query.sql -
When you’re done, remove the container:
docker rm -f askdb-shop-db.
Read next
Section titled “Read next”© 2026 Yahya Gilany

