Quickstart
This guide takes you from zero to a working Lark connection in under five minutes. You’ll connect to a database, write data, read it back, and subscribe to real-time updates.
1. Install the SDK
npm install @lark-sh/client
2. Connect to your database
import { LarkDatabase } from "@lark-sh/client";
const db = new LarkDatabase("my-project/my-database", { anonymous: true });
That’s it. Operations queue automatically and run once the connection is established. No need to await anything before you start using the database.
You can find your project and database IDs in the Lark dashboard. The format is always projectId/databaseId.
3. Write data
// Set data at a specific path
await db.ref("users/alice").set({
name: "Alice",
score: 42,
online: true,
});
4. Read data once
// Read the data you just wrote
const snapshot = await db.ref("users/alice").once("value");
console.log(snapshot.val());
// { name: "Alice", score: 42, online: true }
5. Subscribe to real-time updates
// Listen for changes — on() returns an unsubscribe function
const unsubscribe = db.ref("users/alice/score").on("value", (snapshot) => {
console.log("Score changed:", snapshot.val());
});
// Update the score from somewhere else — your listener fires automatically
await db.ref("users/alice/score").set(99);
// Console: "Score changed: 99"
6. Clean up
// Stop listening
unsubscribe();
// Disconnect when you're done
db.disconnect();
Full working example
Here’s everything in one file:
import { LarkDatabase } from "@lark-sh/client";
async function main() {
const db = new LarkDatabase("my-project/my-database", { anonymous: true });
// Write
await db.ref("users/alice").set({
name: "Alice",
score: 42,
online: true,
});
// Read once
const snapshot = await db.ref("users/alice").once("value");
console.log("Read:", snapshot.val());
// Subscribe to real-time updates
const unsubscribe = db.ref("users/alice/score").on("value", (snap) => {
console.log("Score is now:", snap.val());
});
// Make a change — the subscription fires
await db.ref("users/alice/score").set(100);
// Clean up
unsubscribe();
db.disconnect();
}
main();
Next steps
- Connecting: connection options, transport selection, and lifecycle events
- Writing data: set, update, remove, push, and multi-path writes
- Subscriptions: listen for real-time changes with different event types
- Authentication: move beyond anonymous auth with tokens