The Client API
The SDK client provides a type-safe wrapper around the Decoupla API. It leverages TypeScript's "const generics" to ensure that your queries and results are validated at compile-time.
Initialization
import { createClient } from '@decoupla/sdk';
const client = createClient({
workspace: process.env.DECOUPLA_WORKSPACE!,
apiToken: process.env.DECOUPLA_API_TOKEN!,
});
Reading Content (getEntry & getEntries)
Queries support powerful filtering and preloading. Filters are restricted based on field type (e.g., you cannot use a contains operator on an int field).
const posts = await client.getEntries(BlogPost, {
filters: {
ViewCount: { gte: 100 },
Title: { starts_with: 'Release' }
},
preload: ['Author'], // Automatically populates the Author relation
contentView: 'live' // Use 'preview' to fetch draft/unpublished data
});
Writing Content (createEntry & updateEntry)
Both methods support partial updates (patching) and can return either simple metadata or a fully preloaded object.
// Create a draft
const draft = await client.createEntry(BlogPost, { Title: 'Draft Post' }, false);
// Update, publish, and return the full object with relations
const { data } = await client.updateEntry(
BlogPost,
draft.id,
{ ViewCount: 1 },
{ published: true, preload: ['Author'] }
);
console.log(data.author.name); // Type-safe access to preloaded data