Skip to content

Node.js / Backend Quick Start

Varity Team Core Contributors Updated March 2026

Build a REST API with Express and Varity’s zero-config database. No database setup, no connection strings, no ORM configuration.

  • Node.js 18 or later
  • npm or pnpm
  • Python 3.8+ (for the CLI, needed at deploy time)
  1. Create a new project

    Terminal window
    mkdir my-api && cd my-api
    npm init -y
  2. Install dependencies

    Terminal window
    npm install @varity-labs/sdk express
  3. Install the CLI (for deployment)

    Terminal window
    pip install varitykit

Create a file to set up your database collections:

database.js
const { db } = require('@varity-labs/sdk');
// Create collection helpers
// Each collection is like a database table
const products = () => db.collection('products');
const orders = () => db.collection('orders');
module.exports = { products, orders };
server.js
const express = require('express');
const { products, orders } = require('./database');
const app = express();
app.use(express.json());
// ===== PRODUCTS =====
// Create a product
app.post('/api/products', async (req, res) => {
try {
const product = await products().add({
name: req.body.name,
price: req.body.price,
stock: req.body.stock || 0,
createdAt: new Date().toISOString(),
});
res.status(201).json(product);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// List all products
app.get('/api/products', async (req, res) => {
try {
const limit = parseInt(req.query.limit) || 50;
const offset = parseInt(req.query.offset) || 0;
const allProducts = await products().get({ limit, offset });
res.json(allProducts);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// Update a product
app.put('/api/products/:id', async (req, res) => {
try {
const updated = await products().update(req.params.id, req.body);
res.json(updated);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// Delete a product
app.delete('/api/products/:id', async (req, res) => {
try {
await products().delete(req.params.id);
res.json({ deleted: true });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// ===== ORDERS =====
// Create an order
app.post('/api/orders', async (req, res) => {
try {
const order = await orders().add({
productId: req.body.productId,
quantity: req.body.quantity,
status: 'pending',
createdAt: new Date().toISOString(),
});
res.status(201).json(order);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// List orders
app.get('/api/orders', async (req, res) => {
try {
const allOrders = await orders().get({
limit: parseInt(req.query.limit) || 50,
orderBy: '-createdAt',
});
res.json(allOrders);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`API running at http://localhost:${PORT}`);
});
Terminal window
node server.js

You should see:

[Varity Database] Using shared development database. Data is stored in an isolated dev schema.
Deploy with `varitykit app deploy` to get your own private database.
API running at http://localhost:3000
Terminal window
# Create a product
curl -X POST http://localhost:3000/api/products \
-H "Content-Type: application/json" \
-d '{"name": "Widget", "price": 29.99, "stock": 100}'
# List products
curl http://localhost:3000/api/products
# Update a product (replace PRODUCT_ID with the id from the create response)
curl -X PUT http://localhost:3000/api/products/PRODUCT_ID \
-H "Content-Type: application/json" \
-d '{"price": 24.99}'
# Delete a product
curl -X DELETE http://localhost:3000/api/products/PRODUCT_ID

The Varity database provides four operations on every collection:

MethodDescriptionReturns
collection.add(data)Insert a new documentThe inserted document with id, created_at
collection.get(options?)Query documentsArray of documents
collection.update(id, data)Update a document by IDThe updated document
collection.delete(id)Delete a document by IDtrue on success

The get() method accepts an options object:

const results = await products().get({
limit: 10, // Max documents to return
offset: 20, // Skip N documents (for pagination)
orderBy: '-price', // Sort field ("-" prefix for descending)
});

If you use TypeScript, collections are fully typed:

database.ts
import { db } from '@varity-labs/sdk';
interface Product {
name: string;
price: number;
stock: number;
createdAt: string;
}
const products = () => db.collection<Product>('products');
// Fully typed - TypeScript knows product.name is a string
const product = await products().add({
name: 'Widget',
price: 29.99,
stock: 100,
createdAt: new Date().toISOString(),
});

Varity manages all database credentials automatically:

EnvironmentHow it works
DevelopmentThe db singleton uses shared dev credentials. Your data is isolated in a dev schema. No configuration needed.
Productionvaritykit app deploy injects production credentials into your build. Each app gets its own private database schema.

You never need to set up a database, configure connection strings, or manage credentials.

  1. Install the CLI

    Terminal window
    pip install varitykit
  2. Deploy your API

    Terminal window
    varitykit app deploy

    Your API is live. Database credentials are injected automatically.

  3. Submit to the App Store (optional)

    Terminal window
    varitykit app deploy --submit-to-store