Quickstart
-
Open a database in the app (or create one from the Client CRM template, which the examples in these docs use).
-
Create a connection string. Click Connect at the top of the database (on phones: ⋯ → Connect an app (API)), give it a name such as My script, choose Read & write, and click Create.
Copy the connection string now: it is shown only once. It looks like this:
drivedb://kid_7f3k2m9qx4b8w1zt:sk_Q2x…@your-app-domain/f32bf3kwsrxt9dpgKeep it secret, like a password. Put it in an environment variable:
.env DRIVEDB_URL=drivedb://kid_…:sk_…@your-app-domain/f32bf3kwsrxt9dpg -
Install the SDK.
Terminal window npm install @faeizfurqan/drivedbTerminal window pnpm add @faeizfurqan/drivedbTerminal window bun add @faeizfurqan/drivedb -
Read and write.
index.ts import { DriveDB } from '@faeizfurqan/drivedb';// The connection string from "Connect an app", kept out of your code.const client = new DriveDB(process.env.DRIVEDB_URL!);const db = client.db();const clients = db.collection('clients');// Createconst { insertedId } = await clients.insertOne({name: 'Ada Lovelace',company: 'Analytical Engines',status: 'Lead',dealValue: 4800,});// Readconst leads = await clients.find({ status: 'Lead' }).sort({ dealValue: -1 }).toArray();console.log(`${leads.length} leads; the biggest is ${leads[0]?.name}`);// Updateawait clients.updateOne({ _id: insertedId }, { $set: { status: 'Won' }, $inc: { dealValue: 200 } });// Deleteawait clients.deleteOne({ _id: insertedId });Run it with Node.js 22.18 or newer, which runs TypeScript files directly and reads
.envfiles itself:Terminal window node --env-file=.env index.ts -
Look at the app. The records your code writes appear in the grid within seconds.
Without the SDK
Section titled “Without the SDK”Every method is a POST with a JSON body, so any language works. Use the key part of the
connection string (kid_…:sk_…) as a bearer token:
curl https://your-app-domain/v1/db/f32bf3kwsrxt9dpg/find \ -H "Authorization: Bearer $DRIVEDB_KEY" \ -H "Content-Type: application/json" \ -d '{"collection":"clients","filter":{"status":"Lead"},"limit":10}'import osimport requests
API = "https://your-app-domain/v1/db/f32bf3kwsrxt9dpg"HEADERS = {"Authorization": f"Bearer {os.environ['DRIVEDB_KEY']}"}
def call(action, **body): response = requests.post(f"{API}/{action}", json=body, headers=HEADERS, timeout=30) response.raise_for_status() return response.json()
call("insertOne", collection="clients", document={"name": "Ada", "status": "Lead"})print(call("find", collection="clients", filter={"status": "Lead"})["documents"])// No SDK needed: every method is a POST with a JSON body.const API = process.env.DRIVEDB_API!; // https://<host>/v1/db/<dbId>const KEY = process.env.DRIVEDB_KEY!; // kid_…:sk_…
async function call(action: string, body: Record<string, unknown>) { const response = await fetch(`${API}/${action}`, { method: 'POST', headers: { Authorization: `Bearer ${KEY}`, 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); const data = await response.json(); if (!response.ok) throw new Error(`${data.error.code}: ${data.error.message}`); return data;}
const { insertedId } = await call('insertOne', { collection: 'clients', document: { name: 'From fetch', status: 'Lead' },});const { documents } = await call('find', { collection: 'clients', filter: { status: 'Lead' }, sort: { dealValue: -1 }, limit: 10,});console.log(insertedId, documents.length);Next steps
Section titled “Next steps”- How your data maps to documents: collections, fields and value types.
- Querying and updating documents.
- Relations between collections.
- Keeping connection strings safe.
Made by Faeiz Furqan