Skip to content

Node.js Quick Start

Build an Express API powered by Centrali in 5 minutes. You'll create REST endpoints that use Centrali as your data layer.

Prerequisites

  • A Centrali account with a workspace (Account Setup)
  • Service account credentials (client ID and secret)
  • Node.js 18+

1. Set Up the Project

mkdir my-centrali-api && cd my-centrali-api
npm init -y
npm install express @centrali-io/centrali-sdk dotenv

2. Set Up Environment Variables

Create a .env file:

CENTRALI_CLIENT_ID=ci_your_client_id
CENTRALI_CLIENT_SECRET=sk_your_client_secret
CENTRALI_WORKSPACE=your-workspace-slug
PORT=3000

3. Create the Server

Create index.js:

import 'dotenv/config';
import express from 'express';
import { CentraliSDK } from '@centrali-io/centrali-sdk';

const app = express();
app.use(express.json());

// Initialize Centrali SDK
const centrali = new CentraliSDK({
  baseUrl: 'https://api.centrali.io',
  workspaceId: process.env.CENTRALI_WORKSPACE,
  clientId: process.env.CENTRALI_CLIENT_ID,
  clientSecret: process.env.CENTRALI_CLIENT_SECRET
});

// List products
app.get('/products', async (req, res) => {
  const products = await centrali.queryRecords('Product', {
    sort: '-createdAt',
    limit: 20
  });
  res.json(products);
});

// Get a single product
app.get('/products/:id', async (req, res) => {
  const product = await centrali.getRecord(req.params.id);
  if (!product) return res.status(404).json({ error: 'Not found' });
  res.json(product);
});

// Create a product
app.post('/products', async (req, res) => {
  const product = await centrali.createRecord('Product', {
    name: req.body.name,
    price: req.body.price,
    inStock: true
  });
  res.status(201).json(product);
});

// Update a product
app.patch('/products/:id', async (req, res) => {
  const product = await centrali.updateRecord(req.params.id, req.body);
  res.json(product);
});

// Delete a product
app.delete('/products/:id', async (req, res) => {
  await centrali.deleteRecord(req.params.id);
  res.status(204).end();
});

// Search products
app.get('/search', async (req, res) => {
  const results = await centrali.queryRecords('Product', {
    search: req.query.q,
    limit: 10
  });
  res.json(results);
});

app.listen(process.env.PORT, () => {
  console.log(`Server running on http://localhost:${process.env.PORT}`);
});

Add "type": "module" to your package.json for ES module support.

4. Run It

node index.js

5. Test the API

# Create a product
curl -X POST http://localhost:3000/products \
  -H "Content-Type: application/json" \
  -d '{"name": "Wireless Headphones", "price": 99.99}'

# List products
curl http://localhost:3000/products

# Search
curl "http://localhost:3000/search?q=headphones"

What's Next?