Skip to content

Written for: For developers

SDK reference

18 min read · Updated 16 Nov 2026

@astrato-io/sdk is a browser-only ESM client for Astrato. It provides governed data queries, semantic-layer metadata, in-memory selections and variables, and an event bus. It also provides the host services used by @astrato-io/visualizations.

How the client is created depends on the app model selected in Getting started with Data Apps:

  • An externally hosted app configures createAstratoClient() and owns its trusted authentication/server boundary.
  • A Data App calls createAstratoApp() from @astrato-io/app-runtime and uses the returned app.client. It must not configure another client, credentials or platform IDs.

Package details:

  • Name: @astrato-io/sdk.
  • ESM only. No CommonJS, no UMD. One root export, no subpaths.
  • Browser only. It requires fetch, Headers, and URL.
  • Install @astrato-io/sdk from npm.

SDK 0.6.0 supports visualization field metadata from custom SQL views and table views without a physical schema name. Field references continue to use their canonical semantic-layer keys.

For an externally hosted app or direct SDK use:

Terminal window
npm install @astrato-io/sdk

For a Data App, install @astrato-io/app-runtime instead. It depends on and configures the SDK through the hosted browser contract.

The root package exports these runtime values. Most other exports are types.

import {
createAstratoClient,
createAstratoEventBus,
createDirectDataService,
createVisualizationEngineService,
createMemorySelectionService,
createMemoryVariableService,
createFetchTransport,
createConsoleLogger,
AstratoTransportError,
} from '@astrato-io/sdk';

Most externally hosted applications only need createAstratoClient. The other factories exist for advanced composition and testing. Data Apps use the configured client returned by createAstratoApp().

createAstratoClient for externally hosted apps

Section titled “createAstratoClient for externally hosted apps”
const client = createAstratoClient(options);

baseUrl is the only required option. Do not call this factory in a Data App; see the Data App runtime reference instead.

The examples use window.location.origin for baseUrl because browsers cannot call the Astrato API from a third-party origin. During local development, configure your dev server to proxy /api requests to https://app.astrato.io, or to your organization’s Astrato host. See Authentication for supported authentication flows and the externally hosted app quickstart for a complete Vite configuration.

Option Type Behavior
baseUrl string Required. Base for the default transport. Ignored if you pass your own transport.
tenantId string Merged into the request context. Wins over context.tenantId.
context AstratoDataRequestContext Merged into query and query-insights context. Seven fields map to headers: tenantId, workbookVersionContainerId, sheetId, objectId, role, userId, workbookState. datasourceType remains engine context. Metadata GETs receive no context headers.
auth AstratoAuthProvider { token?, headers?, getAccessToken?() }. See Authentication.
transport AstratoTransport Replaces the default fetch transport. auth still applies on top.
visualizationEngine boolean Default false. Set true to enable client.services.engine. Without it, synchronous engine members throw and asynchronous members reject with "Astrato SDK engine service is not configured. Provide an engine service."
metadataCache { maxEntries?, ttlMs? } Bounds the per-client visualization metadata snapshot cache: { maxEntries?: number; ttlMs?: number }. Defaults: 16 snapshots, 300,000 ms. See The visualization engine service.
hostServices PartialAstratoHostServices Per-service overrides: data, engine, model, selections, variables. A custom data service replaces direct endpoint calls, but a default visualization engine still merges client context into requests passed to it.
logger AstratoLogger Default: console logger. Supply your own to silence output.
events AstratoEventBus Default: a client-owned bus.
endpoints { data? } Override individual data endpoint URL builders.
sessionId string Stamped as scope.sessionId on emitted events. No transport effect.

createAstratoClient returns:

interface AstratoClient {
baseUrl: string;
services: AstratoHostServices; // same object as hostServices
hostServices: AstratoHostServices;
logger: AstratoLogger;
events: AstratoEventBus;
request<TResponse, TBody>(request: AstratoRequest<TBody>): Promise<TResponse>;
reportError(error: unknown, context?: AstratoLogContext): void;
destroy(): void;
}

destroy() invalidates metadata in an engine created by the client and clears subscriptions on an event bus created by the client. A supplied engine or event bus remains owned by its caller.

In a Data App, retain the object returned by createAstratoApp() and call app.destroy() instead. It destroys both the SDK client and runtime transport.

SDK 0.5.0 adds a versioned iframe connection for hosts that keep authentication outside embedded application code. These exports are unavailable in SDK 0.4.0.

The host uses createAstratoIframeHostBridge() with the exact iframe, expected child origin, authorized context and a callback that validates each requested operation. Trusted child bootstrap uses connectAstratoIframeBridge() with the expected parent origin. After connection, getAstratoIframeBridge() returns the connection, whose createClient() supplies an ordinary SDK client with the visualization engine enabled.

An arbitrary iframe does not activate this connection. Invalid origins, incompatible protocol versions and failed connections do not fall back to direct HTTP. The bridge bounds requests and responses and supports cancellation, timeouts and destruction. The host must enforce permitted operations and resource access; the context’s capability list does not replace authorization. Host credentials and raw host errors are not sent to the child.

The host queues request bursts in arrival order while preserving maxConcurrentRequests. At most 32 requests may be active or waiting; excess requests fail with limit. The timeout includes time spent waiting. Cancelling a waiting request removes it before execution; cancelling an active request aborts it, and its slot remains occupied until the host operation settles. Disconnecting clears waiting work and aborts active operations.

maxConcurrentRequests defaults to 16 and must be an integer from 1 to 32; timeoutMs defaults to 30,000 ms and must be between 1 and 120,000. Outside those ranges the factory throws Invalid iframe bridge concurrency limit. or Invalid iframe bridge timeout. The timeout is set per bridge: a request carries none of its own.

Data App source continues to use createAstratoApp() from the app runtime. Host connection setup belongs to the embedding platform, not individual app source.

The SDK does not sign users in. In externally hosted code, it adds the authentication headers that you provide:

interface AstratoAuthProvider {
token?: string; // wins over getAccessToken
headers?: Record<string, string>;
getAccessToken?: () => string | Promise<string>; // resolved per request or default engine operation
}

Individual SDK requests resolve getAccessToken independently when auth.token is absent. A default engine metadata operation or query resolves authentication once and reuses those headers for all requests within that operation; concurrent engine operations share a pending resolution. The SDK does not reuse a completed callback result for later operations.

The resolved token supplies authorization: Bearer <token>. An authorization value in auth.headers overrides the generated header, and a per-request header overrides both.

For a quick local experiment, pass a PAT into the client from your development setup:

import { createAstratoClient } from '@astrato-io/sdk';
function createLocalDevelopmentClient(token: string) {
return createAstratoClient({
baseUrl: window.location.origin,
tenantId: 'your-tenant-id',
auth: { token },
});
}

Bearer tokens do not require a CSRF header. Anything passed through auth.token, however, is visible to the user and to scripts running on the page. Never use this pattern in a page delivered to other users. The externally hosted app quickstart shows how to keep the PAT in a development proxy instead.

The SDK does not provide login, token refresh, expiry handling, or automatic retry after a 401 response. A token also does not bypass the browser’s same-origin restriction.

client.services.data.query() posts a governed query and returns the engine response unmodified.

query(request: AstratoQueryRequest): Promise<AstratoQueryResult>
interface AstratoQueryRequest {
tenantId?: string; // falls back to client options
semanticLayerId?: string; // NOT sent by query(); used by the engine service only
query: unknown; // passed to the transport without an added envelope
context?: AstratoDataRequestContext;
}

Important behaviors:

  • Endpoint: POST {baseUrl}/api/message/tenants/{tenantId}/data/execute/query.
  • The low-level query() passes your query value to the transport without rewriting it or adding an envelope. The default transport JSON-serializes object bodies. Your payload must contain databaseID (the semantic layer ID) itself.
  • Prefer client.services.engine.getData() — it wraps your query into the { query: { ...query, databaseID } } envelope the engine expects.
  • The response is not validated or unwrapped. Type: AstratoQueryResult, a loose { response, correlationID?, telemetry?, ... } object.
  • No cancellation, timeout, or retry on query(). Only the low-level client.request() accepts an AbortSignal.
  • HTTP errors reject with AstratoTransportError carrying .status and .responseBody.

query() and getData() return the engine response unchanged. A typical result looks like this:

{
"response": {
// One entry per returned column, in the engine's order.
"fields": [
{ "name": "Cars.Manufacturer", "dataType": "string", "isDimension": true, "isMeasure": false },
{ "name": "SUM of Sales", "dataType": "int", "isDimension": false, "isMeasure": true },
],
// Row arrays keyed by row index.
"data": { "0": ["Ford", 1234], "1": ["Kia", 987] },
"meta": { "rows": 2, "cols": 2 },
"timezone": "UTC",
},
}

When reading the result:

  • data is an object keyed by row index. Use Object.values(data) to obtain the row arrays.
  • fields[i].name is a display label, not a stable field key. A raw dimension commonly has a dotted label such as Cars.Manufacturer; an aggregate or calculated column may be labelled SUM of Sales or CPC. Do not match result columns by these labels. Keep the field keys and selection order from the query you sent, then validate that the response has the expected number of columns, row widths and dimension/measure roles before reading values.

orders addresses a result column by its zero-based position, not by its field key. The result columns are dimensions first, followed by measures or calculations:

const query = {
dimensions: [{ fieldKey: '[Cars].[Manufacturer]' }, { fieldKey: '[Cars].[Year]' }],
measures: [],
orders: [{ columnIdx: 1, direction: 'desc' }], // by Year
};

Calculate columnIdx from the current result-column order so sorting remains correct when you add or remove columns. The response fields array and each row’s value array use that same order.

import { AstratoTransportError } from '@astrato-io/sdk';
// `client` is configured with one of the authentication flows described above.
try {
const result = await client.services.data.query({
query: {
query: {
databaseID: 'your-semantic-layer-id',
dimensions: [],
measures: [],
},
},
});
console.log(result);
} catch (error) {
if (error instanceof AstratoTransportError) {
console.error(`HTTP ${error.status}`, error.responseBody);
} else {
throw error;
}
} finally {
client.destroy();
}

Use generic client.request() with an authenticated platform transport for writeback inserts and updates, stored procedures and SQL SELECT readback. The SDK has no dedicated methods for these operations. See Writeback and SQL for scopes, payloads and complete examples.

For local Data Apps during astrato dev, use the App runtime’s capability-gated app.executeProcedure() and app.writeback() methods. SQL SELECT requests are unavailable through the CLI development gateway, and the hosted publication transport does not provide these operations. See App runtime availability.

The following methods are available on client.services.data. Every method except query is optional in the TypeScript interface, so call them with ?.. The default client implements all of them.

Method Returns
getSemanticLayerMetadata(semanticLayerId, tenantId?) The composite semantic-layer metadata, raw.
getDatasource(datasourceId, tenantId?) Datasource details, raw.
getAggregations(datasourceType, tenantId?) Available aggregation functions, unwrapped to an array.
getCapabilities(datasourceType, tenantId?) Datasource capabilities, unwrapped to an object.
getQueryInsights({ semanticLayerId, queryID, startTime?, tenantId? }) Warehouse execution insights for a queryID from getLastQueryMetadata. In Astrato Apps it requires query:sql-insights (see Showing the SQL and query insights behind a visualization). The only validated response: it must carry six booleans (cacheUsed, metaDataQuery, joinIssue, unionIssue, memoryIssue, pruningIssue) and four numbers (creditsBestCase, creditsWorstCase, creditsFairValue, rowsScanned).

The SDK fetches semantic layers and datasources by ID; it does not provide listing APIs. Use astrato_semantic_layer_list on a Nash MCP connection to find semantic layers and their semanticLayerId values.

Enable this service with visualizationEngine: true. It holds one active semantic-layer snapshot at a time and provides synchronous field lookup from that snapshot. Loading another layer activates its snapshot. @astrato-io/visualizations uses the same service.

Starting with SDK 0.4.0, metadata loads for the same tenant, semantic layer, datasource type and authentication headers share pending work and reuse completed snapshots for five minutes. One client retains at most 16 completed snapshots. Reuse the same client across charts to benefit from this cache; low-level client.services.data calls and query results are not cached by the SDK.

Externally hosted apps can configure these bounds with createAstratoClient({ ...options, metadataCache: { maxEntries: 16, ttlMs: 300_000 } }). The default engine resolves authentication once per metadata operation or query and uses those headers for the operation’s requests. Concurrent operations share pending authentication resolution; callbacks that return a new token on each invocation are supported. When any SDK request observes changed authentication headers, the default engine clears its cached and active metadata. Reload metadata before using synchronous field or connectivity lookups. When authentication is managed entirely by a custom transport, no SDK request observes the change, so nothing clears the cache. Bind it to an authority instead: Binding metadata to an authentication snapshot.

Binding metadata to an authentication snapshot

Section titled “Binding metadata to an authentication snapshot”

Most apps need none of this. createAstratoClient’s default engine already tracks the authority for you, and the paragraph above describes what it does. This section is for a host that builds the engine itself because authentication lives entirely in a custom transport.

Two of createVisualizationEngineService(data, options?)’s four options identify the authentication authority, and createAstratoClient exposes neither. Using them means building the engine yourself and passing it as hostServices.engine, which also leaves the other two options to you: the client passes them to its own engine, not to yours. All four are optional. metadataCache is validated on construction: maxEntries must be a positive safe integer and ttlMs a positive finite number.

Option Type Purpose Omitted
context AstratoDataRequestContext Request context merged into the engine’s own requests. No context is merged. createAstratoClient passes its context and tenantId here for you.
metadataCache { maxEntries?: number; ttlMs?: number } Snapshot count and lifetime for this engine. 16 snapshots, 300,000 ms. The client’s metadataCache option does not reach an engine you build.
metadataCacheAuthority () => string | Promise<string> Identifies the current authority before a metadata cache lookup. The authority is the literal string "default" unless metadataLoadContext supplies one.
metadataLoadContext (onAuthorityChange: (authority: string) => void) => Promise<{ authority: string; data: AstratoDataService; isCurrent(): boolean }> Binds one metadata load or query to one authentication snapshot. Nothing fails a load or query already in flight when the credentials change under it.

metadataLoadContext takes precedence: when both are supplied, metadataCacheAuthority is never called. The engine calls metadataLoadContext again for every query, so each operation resolves its own snapshot rather than inheriting the one the metadata load used. With neither option, every cache entry is filed under the single literal authority "default" — which is why a custom transport that switches credentials would otherwise hand one viewer’s metadata to the next. Keep the onAuthorityChange callback and call it when credentials change outside a load, including a change an ordinary request observes; an authority that differs from the one the engine currently holds drops its cached snapshots and its active metadata.

import {
createAstratoClient,
createVisualizationEngineService,
type AstratoDataService,
type AstratoTransport,
} from '@astrato-io/sdk';
// Supplied by your host: an authenticated transport, a credential store whose `authority` string
// changes whenever the credentials do, and a data service bound to one set of them.
declare const transport: AstratoTransport;
declare const credentials: {
current(): string;
resolve(): Promise<{ authority: string }>;
onChange(listener: (authority: string) => void): void;
};
declare function dataServiceFor(authority: string): AstratoDataService;
let reportAuthorityChange: ((authority: string) => void) | undefined;
credentials.onChange((authority) => reportAuthorityChange?.(authority));
const engine = createVisualizationEngineService(dataServiceFor(credentials.current()), {
context: { tenantId: 'your-tenant-id' },
metadataCache: { maxEntries: 16, ttlMs: 300_000 },
metadataLoadContext: async (onAuthorityChange) => {
reportAuthorityChange = onAuthorityChange; // replaced on each load, not accumulated
const { authority } = await credentials.resolve();
return {
authority,
data: dataServiceFor(authority),
isCurrent: () => credentials.current() === authority,
};
},
});
const client = createAstratoClient({
baseUrl: window.location.origin,
tenantId: 'your-tenant-id',
transport,
hostServices: { engine },
});

isCurrent() is what closes the window that invalidating after the fact cannot. The engine checks the snapshot at several points through a metadata load and on both sides of a query, so an operation already in flight fails at whichever stage the credentials change rather than returning a stale answer. Each rejection names its cause:

Message Means
“Astrato SDK visualization engine authentication changed during metadata load; retry.” The snapshot expired while metadata was loading.
“Astrato SDK visualization engine authentication changed before query execution; retry.” The snapshot had already expired when the query was about to run.
“Astrato SDK visualization engine authentication changed during query execution; retry.” The snapshot expired while the query ran; its response is discarded.
“Astrato SDK visualization engine metadata was invalidated; retry.” A deliberate invalidation, not an authority change.

With metadataLoadContext supplied, both the metadata fetch and every query run through the snapshot’s own data service rather than the one the engine was constructed with, so the credentials that fetched the metadata are the credentials that fetch the rows. Return the per-viewer service from the context rather than capturing one when you build the engine.

An engine you supply is yours to clean up. client.destroy() no longer invalidates its metadata or unhooks the authority callback; do both yourself.

A metadata operation or engine query rejects if a subsequent SDK operation observes changed authentication before it completes. Rejected query results do not update query metadata. An older authentication lookup also rejects if it finishes after a newer successful lookup with different credentials. Retry these operations using the current authentication.

After editing a model in an externally hosted app, refresh its metadata explicitly:

await engine.fields.refreshMetadata?.({
semanticLayerId: 'your-semantic-layer-id',
tenantId: 'your-tenant-id',
});

invalidateMetadata(request) removes matching snapshots without reloading them; omit the request to clear all snapshots. These methods are optional for custom engine implementations. For an Data App, reload the page after a model change to refresh both SDK and local runtime metadata.

const client = createAstratoClient({
baseUrl: window.location.origin,
tenantId: 'your-tenant-id',
visualizationEngine: true,
context: { objectId: 'sales-by-manufacturer' }, // used by getLastQueryMetadata()
});
const engine = client.services.engine;
// Load or replace the snapshot. Field and connectivity reads below are synchronous.
await engine.fields.getFieldMetadata?.({
semanticLayerId: 'your-semantic-layer-id',
tenantId: 'your-tenant-id',
});
engine.fields.getDimensionsList(); // synchronous after the load
engine.fields.getMeasuresList();
engine.areFieldsConnected(fieldA, fieldB); // join-graph connectivity
await engine.getData({
semanticLayerId: 'your-semantic-layer-id',
query: { dimensions: [], measures: [] }, // getData adds the databaseID envelope
});
const metadata = engine.getLastQueryMetadata?.('sales-by-manufacturer');

Important behavior:

  • The engine exposes one active snapshot at a time. Reading fields for a different semanticLayerId throws until you load it. Loading a cached ID activates its snapshot.
  • engine.capabilities, engine.databaseInfo, engine.localeSettings throw before the first load. They are not undefined.
  • Field identity is the opaque engine fieldKey (for example [Cars].[Manufacturer]). Never parse or construct field keys from names.
  • getGroupedDimensions() returns the active level of each grouped dimension. Levels default to 0 on a fresh metadata load and can be changed with setGroupedDimensions(). Cache hits preserve those choices separately for each cached snapshot.
  • getLastQueryMetadata(objectId) is keyed by context.objectId and returns the last successful query’s queryID, statement, rows, warnings, and more.

Showing the SQL and query insights behind a visualization

Section titled “Showing the SQL and query insights behind a visualization”

In an Astrato App, getLastQueryMetadata(objectId) carries the engine SQL statement, the queryID and startTime only when the App holds the query:sql-insights capability, and only after a successful query for that objectId has settled. With the query ID, engine.getQueryInsights returns warehouse insights for that query.

  • The App must be built with @astrato-io/app-runtime 0.6.0 or later; older runtimes do not advertise the capability and never receive it. Rebuild older Apps to use it.
  • astrato dev and the App Builder preview grant it. A published App grants it only when the tenant setting described in App publications is on.
  • Without it, statement is undefined, queryID is an empty string and getQueryInsights is refused. In a published App and the App Builder preview the record still reports duration, rows and warnings; under astrato dev there is no record for the query.

This is a presentation control, not a security boundary: the query response in the browser still carries the SQL and query ID, visible in browser developer tools.

Render the control only when the statement is present, and fetch insights only when the viewer asks. On every surface, a runtime that was not granted the capability reads no statement and an empty query ID, and getQueryInsights would be refused. Some warehouses do not report insights, so treat a rejected call as “no insights”:

import { createAstratoApp } from '@astrato-io/app-runtime';
const app = await createAstratoApp();
const engine = app.client.services.engine;
const objectId = 'sales-by-manufacturer'; // the visualization's dataBinding.queryContext.objectId
// Run each of that visualization's queries through this, so only the latest query updates the
// control: a slower, older query that settles afterwards changes nothing.
let latestQuery = 0;
async function trackQuery(chartElement: HTMLElement, query: Promise<unknown>) {
const generation = ++latestQuery;
const succeeded = await query.then(
() => true,
() => false,
);
if (generation === latestQuery) showQueryInsights(chartElement, succeeded);
}
// Each call replaces the previous control, so a refreshed visualization never offers the SQL or
// insights of an earlier query; a failed query keeps the last successful record, so it only clears
// the control.
function showQueryInsights(chartElement: HTMLElement, succeeded: boolean) {
chartElement.querySelector('.query-insights')?.remove();
if (!succeeded) return;
const { statement, queryID, startTime } = engine.getLastQueryMetadata?.(objectId) ?? {};
if (!statement) return;
const control = document.createElement('div');
control.className = 'query-insights';
const button = document.createElement('button');
button.type = 'button';
button.textContent = 'Show SQL';
const panel = document.createElement('div');
panel.hidden = true;
const sql = document.createElement('pre');
sql.textContent = statement;
panel.append(sql);
const details = document.createElement('dl');
panel.append(details);
let fetched = false;
button.addEventListener('click', async () => {
panel.hidden = !panel.hidden;
button.textContent = panel.hidden ? 'Show SQL' : 'Hide SQL';
if (panel.hidden || fetched) return;
fetched = true;
details.textContent = '';
try {
if (!queryID || !engine.getQueryInsights) throw new Error('No insights');
const insights = await engine.getQueryInsights({
semanticLayerId: app.dataBinding.semanticLayerId,
queryID,
...(startTime === undefined ? {} : { startTime }),
});
if (!control.isConnected) return; // a newer query replaced this control
const rows: [string, string][] = [
['Answered from cache', insights.cacheUsed ? 'Yes' : 'No'],
['Metadata-only query', insights.metaDataQuery ? 'Yes' : 'No'],
['Join issue', insights.joinIssue ? 'Yes' : 'No'],
['Union issue', insights.unionIssue ? 'Yes' : 'No'],
['Memory issue', insights.memoryIssue ? 'Yes' : 'No'],
['Pruning issue', insights.pruningIssue ? 'Yes' : 'No'],
['Credits (best case)', insights.creditsBestCase.toLocaleString()],
['Credits (worst case)', insights.creditsWorstCase.toLocaleString()],
['Credits (fair value)', insights.creditsFairValue.toLocaleString()],
['Rows scanned', insights.rowsScanned.toLocaleString()],
];
for (const [label, value] of rows) {
const term = document.createElement('dt');
term.textContent = label;
const definition = document.createElement('dd');
definition.textContent = value;
details.append(term, definition);
}
} catch {
if (!control.isConnected) return;
details.textContent = 'Insights unavailable';
fetched = false; // try again the next time the panel opens
}
});
control.append(button, panel);
chartElement.append(control); // the element that holds the visualization
}

Pass each of the visualization’s queries to trackQuery. New App Builder Apps include a QueryInsights React component in src/astrato.ts that does this for you. The starter kit’s Chart shows it as an SQL button in the visualization’s top-right corner once the visualization has drawn or is slow to, unless you pass queryInsights={false}. Pressed, the SQL and query insights take the visualization’s place in its box; pressed again, the visualization comes back. Beside a visualization you mount yourself, render it with engine, objectId and semanticLayerId props. It renders nothing until the SQL statement is available, and fetches insights once per query, the first time it opens; after a failed fetch it tries again the next time it opens.

client.services.selections is an in-memory, group-scoped selection store. It is the basis for cross-chart interactivity — see the Interactivity guide for the full picture with charts.

A selection group is a plain string. Groups are created lazily the first time you name one. The default group ID is the literal string 'default'.

interface AstratoSelection {
field: { fieldKey: string };
values: (string | number | boolean | null)[];
operator?: AstratoSelectionOperator; // 'equals' | 'inList' | 'gt' | 'between' | ...
kind?: 'VALUE' | 'ASSOCIATIVE'; // defaults to 'VALUE'
sourceInstanceId?: string;
}

Core API:

const selections = client.services.selections;
const field = { fieldKey: 'your-engine-field-key' };
selections.setSelectionMode('manual', 'dashboard'); // 'auto' | 'manual', per group
selections.getSelectionMode('dashboard'); // which list selectValues will write to
selections.selectValues('dashboard', { field, operator: 'inList', values: ['Sweden', 'Norway'] });
selections.applyPendingSelections('dashboard'); // pending -> applied
selections.clearPendingSelections('dashboard'); // discard pending, keep applied
selections.getSelection('dashboard'); // { applied, pending }
selections.getActiveSelection('VALUE', 'dashboard');
selections.removeSelection('dashboard', field); // from applied
selections.declineSelection('dashboard', field); // from pending
selections.setNewSelectionState('dashboard', { applied: [], pending: [] }); // replaces both lists
selections.clearAllSelections('dashboard');
selections.setFieldLocked(field, true, 'dashboard');
selections.isFieldLocked(field, 'dashboard'); // true
selections.toggleFieldLocked(field, 'dashboard');
const subscription = selections
.onActiveSelectionChangeApplied('dashboard')
.subscribe((state) => console.log(state.applied, state.pending));
const lockSubscription = selections.isFieldLocked$(field, 'dashboard').subscribe((locked) => console.log(locked));

Behaviors to know:

  • Modes. In 'auto' mode (the default) selectValues writes directly to applied. In 'manual' mode it writes to pending until applyPendingSelections runs.
  • Identity is kind + fieldKey. A second selectValues on the same field and kind replaces the first. It does not append.
  • Three observables, one payload. onActiveSelectionChange, onActiveSelectionChangeApplied, and onActiveSelectionChangePending all emit the full { applied, pending } state. They differ only in when they fire. All replay the current value on subscribe.
  • The selectionMode property setter affects only the 'default' group. Use setSelectionMode(mode, groupId) for named groups.
  • removeSelection with a bare { fieldKey } removes across both kinds. Pass a full selection to remove one kind only.
  • A lock guards selectValues and nothing else. setFieldLocked(field, locked, groupId?) makes selectValues return without writing to either list and without emitting — no throw and nothing to inspect — but declineSelection, removeSelection, setNewSelectionState, clearPendingSelections, clearAllSelections and applyPendingSelections carry no lock check at all, so a locked field’s existing selection can still be removed, replaced or wiped.
  • A lock is keyed on the group and the fieldKey. The lock methods take a bare { fieldKey }, and selectValues tests only the selection’s field, so one lock blocks both the VALUE and the ASSOCIATIVE selection on that field. An omitted groupId means the group literally named 'default', so a lock set without a group is invisible to a named group.
  • isFieldLocked(field, groupId?) reads the state and isFieldLocked$(field, groupId?) observes it, replaying the current value on subscribe — which is what drives a disabled filter control. It emits on every setFieldLocked call, including one that sets the value already held, so de-duplicate if repeated emissions matter. toggleFieldLocked is setFieldLocked with the value inverted.
  • getSelectionMode(groupId?) is the only way to learn whether a selectValues without options.state will land in pending or in applied. It returns the group’s explicit mode and otherwise the mode the service was constructed with — always 'auto' on client.services.selections, because createAstratoClient passes none. A different fallback means supplying hostServices: { selections: createMemorySelectionService('manual') }.
  • Selections and lock state are per client and in memory: neither survives a page reload, and neither is persisted, shared between clients or sent to Astrato.
  • These are not the workbook filter bar’s locks. Locked Filters is a report-designer setting, gated by the Can manage filter locks permission and carried out with the Lock Filter, Unlock Filter and Toggle Filter Lock actions; a Data App’s locks are its own client-side state, and neither reads the other.
  • singleSelectFields$ always emits [] and has no supported use.

client.services.variables is read-only, with two methods:

getVariables(contextId?: string): Promise<AstratoVariable[]>;
resolveValue(nameOrId: string, contextId?: string): Promise<unknown>;
interface AstratoVariable { id: string; name: string; value: unknown; contextId?: string }

The default service is empty. Seed values at client creation:

import { createAstratoClient, createMemoryVariableService } from '@astrato-io/sdk';
const client = createAstratoClient({
baseUrl: window.location.origin,
tenantId: 'your-tenant-id',
hostServices: {
variables: createMemoryVariableService([{ id: 'region', name: 'Region', value: 'EMEA' }]),
},
});
await client.services.variables.resolveValue('Region'); // 'EMEA'

There is no variable write API and no server backing. To change variables at runtime, supply your own AstratoVariableService implementation.

client.events is an RxJS-based event bus. Events are not replayed, so subscribe before triggering the operation you want to observe.

const unsubscribe = client.events.on('transport:error', (event) => {
console.error(event.payload.method, event.payload.url, event.payload.error);
});
client.events.emit('my-app:refresh', { reason: 'user' }); // custom events type-check

Built-in events, all emitted by the client:

Event Payload When
transport:request { method, url } Before every request.
transport:response { method, url } After a successful response.
transport:error { error, method, url } After a failed request, before the rejection reaches you.
sdk:error { context, error } When client.reportError() is called.

There are no client events for selections, variables, or engine loads. Selection changes are available through the three RxJS observables described above. Variables and engine loads expose no event or observable stream.

The default transport wraps fetch:

createFetchTransport({ baseUrl?, defaultHeaders?, fetch? })
  • URL joining uses new URL(url, baseUrl + '/'). The SDK’s own endpoint paths are relative without a leading slash, so a baseUrl with a path prefix is preserved.
  • Bodies: null and undefined produce no body. string, FormData, and URLSearchParams pass through. Other values are JSON-encoded with content-type: application/json.
  • fetch option: supply your own fetch (for CSRF handling, logging, or testing).

The following features are not supported:

  • Authentication flows such as login, refresh, expiry checks, or retry after a 401 response.
  • Cross-origin browser requests.
  • Persistence. model.saveProperties() and model.saveLocalVSpec() are no-ops.
  • Listing or discovery APIs. Semantic layers and datasources are fetched by ID.
  • Business questions.
  • Cancellation on query() or getData(), or built-in timeout, retry, pagination, or streaming.
  • Node.js. The SDK is browser-only.