Kolaybase + Express: Backend Data Access in Node.js APIs
Running an Express API? Use kolaybase-js server-side to read and write PostgreSQL data, with a service key for trusted operations that bypass row-level security.
Install
npm install kolaybase-jsCreate a server client
// kolaybase.js
import { createClient } from "kolaybase-js";
export const kb = createClient({
projectId: process.env.KOLAYBASE_PROJECT_ID,
apiKey: process.env.KOLAYBASE_SERVICE_KEY, // server-only
});Keep the service key on the server only. Use it for trusted operations; for anything reachable by the browser, use the anon key with row-level security.
Use it in a route handler
import express from "express";
import { kb } from "./kolaybase.js";
const app = express();
app.get("/api/posts", async (_req, res) => {
const { data, error } = await kb.from("posts").select("id, title");
if (error) return res.status(500).json({ error });
res.json(data);
});
app.listen(3000);Trusted server access
Use a service key for operations that need to bypass row-level security.
Add a custom API surface
Expose your own endpoints with Express while Kolaybase handles the data layer.
Raw SQL available
Use kb.sql for reports and complex queries beyond the query builder.
Frequently asked questions
- Can I build a custom REST API over Kolaybase with Express?
- Yes. Use Express for bespoke endpoints and business logic, and kolaybase-js with a service key for data access.
- Should I use the anon or service key in Express?
- Use the service key for trusted server operations. If you forward requests on behalf of end users, prefer the anon key so row-level security still applies.
Other integrations
Build your Express backend on Kolaybase
PostgreSQL, auth, storage, and a REST API — running in minutes.