Skip to content

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.

askdb introspectaskdb ask+ your questionrun-query.mjs(your code)shop.dbthe sample databaseaskdb/schema.jsonthe schema artifactquery.sqlchecked SQLthe answerrows from shop.db
The files we’ll make, and the step that makes each. AskDB reads the schema and returns checked SQL. Our own code runs it.

You need:

  • Node.js 22.23.4 or newer, with npm, pnpm or Yarn 4. Run node --version to 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.

First, make a folder for the project and set it up as a package:

Terminal window
mkdir askdb-shop
cd askdb-shop
npm init -y

Now create shop.sql in that folder. It defines three tables and adds 4 customers, 3 products and 6 orders:

shop.sql
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:

make-shop.mjs
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:

Terminal window
node make-shop.mjs

It 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.

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.

Terminal window
npm install --save-dev askdb

Then let askdb init write the config for our database:

Terminal window
npx 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 497ms
Installed: @askdb/config, better-sqlite3.

Open askdb.config.ts:

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.

AskDB reads environment variables from a .env file in the folder you run it from. Copy the example file that askdb init wrote:

Terminal window
cp .env.example .env

In 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:

.env
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.

Now let AskDB read the structure of our database:

Terminal window
npx askdb introspect

It 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.

Ask the question in plain English:

Terminal window
npx 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_spent
FROM customers c
JOIN orders o ON c.id = o.customer_id
JOIN products p ON o.product_id = p.id
GROUP BY c.id, c.name
ORDER 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).

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:

run-query.mjs
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:

Terminal window
node run-query.mjs

The 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.

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.

  1. In step 1, create the folder and shop.sql, but skip make-shop.mjs.

  2. Start Postgres and load the tables:

    Terminal window
    docker run -d --name askdb-shop-db -e POSTGRES_PASSWORD=askdb \
    -p 5432:5432 postgres:17
    docker exec -i askdb-shop-db psql -U postgres < shop.sql

    If the second command can’t connect, Postgres is still starting. Wait a few seconds and run it again.

  3. In step 2, run the askdb init command without the --database and --sqlite-file flags. Postgres is the default.

  4. In step 3, also set DATABASE_URL in .env:

    Terminal window
    DATABASE_URL=postgres://postgres:[email protected]:5432/postgres
  5. Do steps 4 and 5 as written.

  6. In step 6, save the SQL as query.sql, then run it with psql instead of run-query.mjs:

    Terminal window
    docker exec -i askdb-shop-db psql -U postgres < query.sql
  7. When you’re done, remove the container: docker rm -f askdb-shop-db.