Skip to content

Answer the queries

In the backend and sandbox setups, your code answers the queries of the components. This page describes what it receives and what it returns. It is the same contract in both:

Setup Your code is It receives
2 · Your backend The endpoint named by <source-api url> { "queries": { id: query } }, and returns { "answers": { id: answer } }
3 · Sandbox The resolveQuery function given to embedDashboard, and usually the endpoint it calls One query, and returns one answer

A query is a JSON description of what a component shows. An answer is { rows, meta }. In a static page there is nothing to write: DuckDB-WASM answers.

From a tag to a query

<bar-chart measure="revenue" by="country" compare="yoy" sort-by="revenue" limit="10"></bar-chart>
{
  "mode": "semantic",
  "metrics": ["revenue"],
  "dimensions": ["country"],
  "attributes": null,
  "date_granularity": null,
  "date_range": ["2026-09-01", "2026-11-29"],
  "filters": { "channel": ["Web", "Store"] },
  "period_comparison_method": "yoy",
  "sort_by": "revenue",
  "sort_ascending": false,
  "limit": 10
}
Field Comes from
mode The absence of table "semantic": names of measures. See three kinds of queries.
metrics measure, or measures on a <data-table> Names of measures. Empty when a filter asks for the values of a dimension.
dimensions by, then breakdown-by Names to group by, or null.
attributes attributes Columns that describe a dimension (the city of a store), or null.
date_granularity granularity day, week, month, quarter or year: a time series is asked.
date_range The period selected in the page ["YYYY-MM-DD", "YYYY-MM-DD"], or null.
filters The values selected in the page { dimension: value } or { dimension: [values] }, or null.
period_comparison_method compare yoy, mom or prev_period: a comparison is asked.
sort_by, sort_ascending, limit sort-by, sort-asc, limit A column of the answer, a direction, a number of rows.
params param-* attributes Present only when the tag has some: see Your own parameters.

The filter on a component's own dimension is left out of its query: a bar chart by country keeps showing every country when one is clicked, with the selection highlighted.

The answer

{
  "rows": [
    { "country": "France", "revenue": 417942.98, "revenue_prev": 389120.5 },
    { "country": "Spain", "revenue": 174938.66, "revenue_prev": 181004.1 }
  ],
  "meta": {
    "formats": { "revenue": "eur" },
    "labels": { "revenue": "Revenue" },
    "comparisons": { "revenue": "revenue_prev" }
  }
}

rows is a list of plain objects, keyed by the names the query used: row.country, row.revenue. Values are numbers, strings, booleans or null; dates are "YYYY-MM-DD" strings.

meta is optional help for the display. Everything in it can be left out:

Used by
formats { column: "eur" \| "pct" \| "int" \| "dec" } Axes, tooltips, score cards, tables. A format attribute on the tag wins.
labels { column: "Displayed name" } Titles of score cards, legends, column headers.
time_col The name of the time column of a time series Charts with granularity and no by.
comparisons { metric: column holding its comparison value } The badge of a score card, the dashed series of a chart.

Giving formats and labels from the server keeps them out of every dashboard, which matters most when a model writes them.

To refuse a query, answer { "error": "Unknown metrics: margin" } to <source-api>, or throw an Error in resolveQuery. The message is shown in the component that asked.

What each component expects

A key figure

<score-card measure="revenue"> sends metrics: ["revenue"] with no dimension, and reads the first row: [{ "revenue": 592881.64 }].

Several score cards of a page may arrive as one query with all their metrics (the sandbox groups them): answer one row holding every metric.

A chart or a table by dimension

One row per value of the dimensions, in the order to display: [{ "country": "France", "revenue": 417942.98 }, ...].

With breakdown-by, dimensions holds two names and the rows are long: one row per pair, { "country": "France", "channel": "Web", "revenue": 120443.1 }. The chart makes one series per value of the second dimension.

A <data-table measures="revenue, orders" attributes="city"> asks for several metrics and attributes at once, and shows its columns in the order it declared them.

A time series

<line-chart measure="revenue" granularity="month"> sends date_granularity: "month" and no dimension. Answer one row per period, with the first day of the period in a column of your choice, named in meta.time_col:

{ "rows": [{ "date": "2026-09-01", "revenue": 31840.2 }, { "date": "2026-10-01", "revenue": 35310.9 }], "meta": { "time_col": "date" } }

granularity written without a value follows the option date_granularity of the page, which a <ui-filter option="date_granularity"> drives; the query always holds the resolved value.

A comparison

compare="yoy" sends period_comparison_method: "yoy": the same figure one year earlier (mom: one month earlier, prev_period: the period of the same length just before). Add the comparison value to each row and name its column in meta.comparisons:

{ "rows": [{ "revenue": 592881.64, "revenue_prev": 548112.3 }], "meta": { "comparisons": { "revenue": "revenue_prev" } } }

A score card shows the variation as a colored badge; a line or bar chart draws the comparison period as a second series. A column named <metric>_variation, holding a ratio (0.082), is used as the variation when present.

The values of a dimension

<ui-filter dimension="channel"> without options asks which values exist: metrics: [], dimensions: ["channel"], no filter and no period. Answer one row per value, [{ "channel": "Web" }, { "channel": "Store" }], limited to what the viewer may see.

Three kinds of queries

mode says how the query names data. The examples above are "semantic": named measures. An endpoint, or a resolveQuery, can also accept the two others, and refuses what it does not want.

"table": SQL expressions. A tag written with table="orders" sends expressions instead of names:

{
  "mode": "table", "table": "orders",
  "metrics": ["sum(revenue)"], "dimensions": ["country"], "breakdown": null, "attributes": [],
  "filters": {}, "date_range": null, "sort_by": "sum(revenue)", "sort_ascending": false, "limit": 20
}

Rows are keyed by the exact strings of the query: row["sum(revenue)"]. Accept this mode once your server checks it: the table is one you expose, every word of every expression is one of its columns or a safe function, and the viewer's rights are added as filters. The library's compileSQL(spec, { tables, dateColumns }) does all of it and writes the SQL: see SQL expressions and named queries. In the sandbox, embedDashboard has already refused statements, sub-queries and unsafe functions; a page with <source-api> sends the expressions as written, so your server is their only check.

"named": a query by its name. A script calls dashboard.query('category_mix'), and your code receives { "mode": "named", "name": "category_mix", "filters": ..., "date_range": ... } with the selection of the page. The definition stays on your side; answer { rows, meta } of any shape, and refuse a name you do not know.

Your own parameters

Anything the standard fields do not cover travels in params, from param-* attributes:

<score-card measure="revenue" param-scope="like_for_like" param-with-tax></score-card>
{ "mode": "semantic", "metrics": ["revenue"], "params": { "scope": "like_for_like", "with_tax": true } }

The library carries them without reading them. In the sandbox they are dropped unless you accept them: give embedDashboard a sanitizeParams: (params) => cleanParams function, which returns what it allows or throws.

A switch for the whole page is an option: <ui-filter option="exclude_discounts" type="toggle">. Options are not part of a query; read them where you send it, and every component asks again when one changes:

// Sandbox: the Host holds the state of the dashboard
resolveQuery: (query) => callYourEngine({ ...query, options: dashboard.state.options }),

// <source-api>: `body` can be a function of the dashboard
document.querySelector('source-api').body = (dashboard) => ({ options: dashboard.filters.getOptions() });

What your server must check

The page, and the Host of a sandbox, run in the viewer's browser. Whatever they check, anyone can call your endpoint with any query. Treat it as any public API:

  • Rebuild each query from lists you own. Measures, dimensions, granularities and comparisons are looked up in your dictionary; an unknown name or an unexpected field is refused.
  • Check SQL expressions word by word before they enter a query: a table you expose, its columns, safe functions, one expression each. compileSQL(spec, { tables }) does it; with no such check, refuse mode: "table". No other text of a query ever becomes SQL.
  • Pass values as query parameters: filter values, dates, limits.
  • Bound everything: the number of rows, of names, of filter values, the length of a text.
  • Apply the rights of the session to every query. Intersect what was asked with what the viewer may see, so a click still narrows the result. Fail closed: a viewer allowed nothing is refused, an empty list never means "no restriction".
  • Say why you refuse, in words safe to display. Unknown metrics: margin helps the author, and a model; a stack trace does not.

The engine of the playground does all of this in 250 lines of Python, on DuckDB: semantic_engine.py. The backend guide has a shorter one to start from.