Setup
Connect your app to its Buildspace-managed Turso database.
Environment variables
When your app is created (or when you provision a database from the Data tab), two environment variables are automatically injected into your Railway service:
| Variable | Description |
|---|---|
BUILDSPACE_DB_URL | libSQL connection URL (e.g. libsql://bs-a1b2c3d4-dev-yourorg.turso.io) |
BUILDSPACE_DB_TOKEN | Turso auth token (full-access JWT) |
These are set per environment — your dev and prod services each receive their own URL and token.
You do not need to configure these manually. They are synced to Railway by the platform whenever a database is provisioned or a token is rotated.
Install dependencies
Install the libSQL client and Drizzle ORM:
npm install @libsql/client drizzle-ormOr with Bun:
bun add @libsql/client drizzle-ormFor migrations, also add drizzle-kit as a dev dependency:
npm install -D drizzle-kitCreate the database client
Create a db.ts (or lib/db.ts) file in your project:
import { createClient } from "@libsql/client";
import { drizzle } from "drizzle-orm/libsql";
const client = createClient({
url: process.env.BUILDSPACE_DB_URL!,
authToken: process.env.BUILDSPACE_DB_TOKEN!,
});
export const db = drizzle(client);That's it — db is a Drizzle instance ready for queries.
Local development
For local development you can use a local SQLite file instead of connecting to the remote Turso database:
import { createClient } from "@libsql/client";
import { drizzle } from "drizzle-orm/libsql";
const client = createClient({
url: process.env.BUILDSPACE_DB_URL ?? "file:local.db",
authToken: process.env.BUILDSPACE_DB_TOKEN,
});
export const db = drizzle(client);This falls back to a local local.db file when the env vars are not set, so you can develop without connecting to Turso.
Token rotation
You can rotate database tokens from the Data tab in Creator Studio. When you rotate a token:
- All existing tokens for that database are invalidated.
- A new token is generated and stored.
- The
BUILDSPACE_DB_TOKENenv var is updated and synced to Railway. - Your service will pick up the new token on its next deploy or restart.