ew.dev/Docs/Reference/Sdk Recipes

SDK Recipes

Common patterns and ready-to-use code snippets for the DA SDK.

These recipes cover the two DA SDK tasks that come up in almost every integration: writing a page programmatically, and walking a large folder tree without pulling every file into memory at once. Both use the SDK helpers rather than hand-rolled fetch calls, so they pick up auth handling and error conventions for free.

Recipe: create a page

To create a page, build an HTML blob and send it to the admin source endpoint as multipart form data, authenticated with a bearer token. This works the same whether you're running the snippet in a browser context or in Node.js 20+; the only difference in Node is that you have to supply your own token, since there's no session to piggyback on.

import { getToken } from 'https://da.live/nx/utils/sdk.js';

const token = await getToken();
const blob = new Blob([MOCK_PAGE], { type: 'text/html' });
const body = new FormData();
body.append('data', blob);

const opts = {
  headers: { Authorization: `Bearer ${token}` },
  method: 'POST',
  body,
};

await fetch(
  `https://admin.da.live/source/${org}/${repo}/drafts/${user}/${filename}.html`,
  opts,
);

Run the Node.js version the same way you'd run any script: node index.js.

Recipe: stream a list of pages under a path

For large trees, use the crawl helper instead of listing a folder and recursing manually. It walks the tree for you, invoking a callback as each item is discovered so you can process results incrementally rather than waiting for the whole crawl to finish. It also accepts a throttle setting so you don't hammer the admin API on big sites.

import { crawl } from 'https://da.live/nx/public/utils/tree.js';

const { results, getDuration, cancelCrawl } = crawl({
  path: '/myorg/myrepo/drafts',
  callback: (item) => console.log('found:', item.path),
  throttle: 25,
});

const files = await results;
console.log(`crawled ${files.length} items in ${getDuration()}ms`);

Call cancelCrawl() if you need to abort early — useful when wiring this into a UI with a "stop" button.

See more SDK recipes on docs.da.live