TypeScript SDK
@teroai/sdk is a typed client of the REST API, for Node.js 20 or later.
Its types are generated from the API’s OpenAPI document.
npm install @teroai/sdkAPI keys are secrets. Use the SDK on a server or your own machine, never in browser code.
Usage
import { createTeroClient } from "@teroai/sdk";
const tero = createTeroClient({ api_key: process.env.TERO_API_KEY! });
const answer = await tero.questions.ask(
{
question: "Which 5 congressional districts in Texas have the highest median household income?",
},
{
stream: true,
on_progress: ({ elapsed_ms }) => console.log(`Still working… ${Math.round(elapsed_ms / 1000)} s`),
},
);
console.log(answer.answer);
console.log(answer.sql);
console.table(answer.rows);
const followUp = await tero.questions.ask({
question: "And which have the lowest?",
conversation_id: answer.conversation_id,
});
console.log(followUp.answer);
const usage = await tero.account.usage();
console.log(`${usage.percent_used}% of this month's tokens used`);stream: true receives the answer over an event stream, so
answers that take over a minute arrive. The promise still resolves with the whole answer, and
on_progress is called every 15 seconds while it waits.
Functions
| Group | Functions |
|---|---|
account | me(), usage() |
questions | ask(input, { stream?, on_progress? }), listSchemas() |
conversations | list({ limit?, offset?, source? }), get(id, query?), listMessages(id, query?), rename(id, title), delete(id), branch(id, { up_to_message_id?, title? }) |
messages | get(id), rows(id, query?), rate(id, rating), chart(id), map(id, { format? }), report(id, { page_count? }), explainSql(id) |
Each function matches an endpoint in the API reference, with the same
fields. Every function takes a last argument { signal?, timeout_ms? } to cancel it or change
its timeout.
Options
| Option | Default |
|---|---|
api_key | Required |
base_url | https://platform.teroatlas.ai/v1 |
timeout_ms | 120 000 per request; streamed answers wait at least 180 000 |
fetch | The global fetch |
Errors
Every failure rejects with a TeroApiError, an Error with status, code, message,
request_id and details. code is one of the API’s error codes, or,
when no response arrived, timeout, aborted, network_error or invalid_response.
import { createTeroClient, isTeroApiError } from "@teroai/sdk";
const tero = createTeroClient({ api_key: process.env.TERO_API_KEY! });
try {
await tero.questions.ask({ question: "How many congressional districts does Ohio have?" });
} catch (error) {
if (isTeroApiError(error) && error.code === "rate_limited") {
console.log(`Retry in ${error.details.retry_after_seconds} s`);
} else if (isTeroApiError(error)) {
console.error(`${error.code}: ${error.message} [${error.request_id}]`);
} else {
throw error;
}
}Cancelling
import { createTeroClient } from "@teroai/sdk";
const tero = createTeroClient({ api_key: process.env.TERO_API_KEY! });
const controller = new AbortController();
setTimeout(() => controller.abort(), 30_000);
const answer = await tero.questions.ask(
{ question: "How many congressional districts does Ohio have?" },
{ signal: controller.signal },
);
console.log(answer.answer);Cancelling also stops the question on the server.
Version 1
Version 1.1 replaces the 1.0 package, which called endpoints that no longer exist. Keys made for 1.0 were revoked; create a new key and use the functions above.
Other languages can call the REST API directly; its OpenAPI document works with OpenAPI code generators.