Get started

TypeScript SDK

@bytecontent/sdk is a small, typed client for the REST API. It works in Node.js 18+, Deno, Bun, edge runtimes and browsers.

Create a client

ts
import { createClient } from '@bytecontent/sdk';

const cms = createClient({
  baseUrl: 'https://cms.example.com', // /api/v1 is added automatically
  apiKey: process.env.CMS_API_KEY,
});
Keep API keys on the server

Use the SDK with an API key in server code (API routes, server components, scripts). For browsers, use the anonymous public API.

Typed records

Generate interfaces for your entities with the CLI, or write them yourself:

ts
interface Product {
  name: string;
  price: string;          // decimals are strings
  isActive: boolean | null;
  tags: Array<'new' | 'sale'> | null;
}

const products = cms.records<Product>('product');

const page = await products.list({
  filter: { isActive: true, price: { gte: 10 }, tags: { contains: 'sale' } },
  sort: ['-price', 'name'],
  pageSize: 50,
});
page.data[0].data.price; // typed as string

const created = await products.create({ name: 'Desk lamp', price: '19.90' });
const updated = await products.update(created.id, { price: '24.50' }, { version: created.version });
await products.delete(created.id);
await products.restore(created.id); // admins
const one = await products.get(created.id, { expand: ['category'] });

Iterate over everything

ts
for await (const product of cms.iterate<Product>('product', { filter: { isActive: true } })) {
  console.log(product.data.name);
}

Errors

Failed requests throw PlatformError with the HTTP status, the stable code and optional details:

ts
import { PlatformError } from '@bytecontent/sdk';

try {
  await products.update(id, { price: '1.00' }, { version: 1 });
} catch (error) {
  if (error instanceof PlatformError && error.code === 'RECORD_VERSION_CONFLICT') {
    // reload and retry
  } else throw error;
}

Network failures raise PlatformError with code NETWORK_ERROR.

Schema and configuration

ts
const entities = await cms.entities.list();
await cms.entities.create({ key: 'event', name: 'Event', fields: [{ key: 'title', type: 'text', label: 'Title' }] });

const doc = await cms.config.export();             // portable schema document
const plan = await cms.config.diff(doc);           // read-only plan
await cms.config.apply(doc, { prune: false });     // atomic apply

const spec = await cms.openapi.tenant();           // OpenAPI incl. your entities

Reference

MethodDescription
records<T>(key)list, get, create, update, delete, restore
iterate<T>(key, options)Async iterator over all pages
entitieslist, get, create, update, remove
configexport, diff, apply
openapiplatform(), tenant()
tenant()Current workspace
request(method, path, opts)Any other endpoint

List options: page, pageSize, sort, fields, search, expand, filter, deleted.

ByteContent by ByteRings is open source under the MIT License. Edit on GitHub