跳到主要内容

Five minute quickstart

One page, the whole journey: install, connect a database, mint a token, render the chat, ask the first question.

What you need

  • A Dataira account with an API key (Platform, API keys)
  • A Postgres database your Dataira org can reach, with a read-only user (create one in two minutes)
  • A React app with a backend route (Next.js shown; any server works)

1. Install the packages (one minute)

npm install @dataira/react @dataira/node

@dataira/react renders in the browser. @dataira/node stays on your server and talks to the Dataira API.

2. Connect the database (two minutes)

In the Dataira app, connect a datasource with the read-only credential. Dataira tests the connection, checks that the credential really is read-only, reads the table structure, and prepares a data context for the assistant. You confirm what it learned, and the datasource goes live.

All your customers live in one database? That is the normal case: keep a customer_id style column (or any column that marks whose row is whose), you will scope by it in step 3.

3. Mint a token on your backend (one minute)

app/api/dataira-token/route.ts
import { Dataira } from "@dataira/node";

const dataira = new Dataira({
apiKey: process.env.DATAIRA_API_KEY!,
baseUrl: "https://api.dataira.ai",
});

export async function POST(req: Request) {
const user = await yourAuth(req); // your session, your user
const { token } = await dataira.createToken({
endUserId: user.id,
endUserOrgId: user.orgId,
datasourceId: process.env.DATAIRA_DATASOURCE_ID!,
scopeColumn: "customer_id",
scopeValue: user.customerId,
});
return Response.json({ token });
}

Two rules that keep your data safe, enforced by us:

  • The token is minted on your server, from your authenticated session. The browser never chooses who it is.
  • scopeColumn and scopeValue pin every question to one customer's rows. Even a crafted query cannot read another customer's data.

Answers show the generated SQL and the assistant's analysis by default. To hide SQL from a non-technical audience, add allowSqlEvidence: false to the mint call (details in the Backend SDK).

4. Render the chat (one minute)

app/dashboard/Analytics.tsx
"use client";
import { useState, useEffect } from "react";
import { DatairaInsights, DatairaProvider } from "@dataira/react";

export function Analytics() {
const [token, setToken] = useState<string>();
useEffect(() => {
fetch("/api/dataira-token", { method: "POST" })
.then((r) => r.json())
.then(({ token }) => setToken(token));
}, []);
if (!token) return null;
return (
<DatairaProvider
baseUrl="https://api.dataira.ai"
getToken={() => token}
>
<DatairaInsights />
</DatairaProvider>
);
}

5. Ask a question

Type: how much did my customer spend last month by product?

The assistant writes a query with the customer filter attached by the server, runs it read-only, and answers with a chart. The SQL is visible in the interface so anyone can check it.

Where to go next