APIs and SDKs
JavaScript and TypeScript SDK
The official JavaScript client works in browsers, Node, Bun, and edge runtimes, and covers records, authentication, files, realtime, batch, functions, webhooks, and API keys.
Install
Install the RustaBase 1.0 package. It is a fresh client with its own calls; code written for the older client needs updating.
npm install rustabaseCreate a client
import { createClient } from "rustabase";
const rb = createClient("https://my-app.rustabase.net");
// sign in a user
const auth = await rb.auth("users").signInWithPassword(
"[email protected]",
"a-strong-password",
);
console.log(rb.session.isValid, auth.record.id);Read and write records
interface Post {
id: string;
title: string;
published: boolean;
author: string;
}
const page = await rb.from<Post>("posts").list({
page: 1,
perPage: 20,
filter: 'published = true',
sort: "-created",
expand: "author",
});
const created = await rb.from<Post>("posts").create({
title: "Shipping the new API",
published: false,
});
await rb.from<Post>("posts").update(created.id, { published: true });
await rb.from("posts").remove(created.id);Keep the session
In the browser the SDK persists the session in local storage by default. On the server, or in React Native, provide your own async store so each request uses the right session.
import { createClient, AsyncSession } from "rustabase";
const session = new AsyncSession({
save: async (serialized) => saveToStorage(serialized),
initial: await loadFromStorage(),
});
const rb = createClient("https://my-app.rustabase.net", { session });Never create a server-side client once and share it across users. Create a client per request, or call rb.signOut() between users.
Handle errors
import { RustaBaseError } from "rustabase";
try {
await rb.from("posts").get("missing-id");
} catch (err) {
if (err instanceof RustaBaseError) {
console.error(err.status, err.message, err.fieldErrors);
}
}Fix it yourself. The link below opens this file in GitHub's editor and forks the repository for you if you need one, and your change becomes a pull request without leaving the browser.