Universal Document Universal Document

cSDK Developer Documentation

The Cloudflare Worker SDK (cSDK) provides serverless libraries for compiling, sealing, signing, and serving Universal Documents directly at the Edge.

1. Installation

Install the SDK via npm into your Cloudflare Worker project:

npm install @universaldocument/csdk

2. Packaging a UDS Document

Generate and seal a UDS payload dynamically in your Worker request router:

import { sealDocument } from '@universaldocument/csdk';

export default {
  async fetch(request, env) {
    const blocks = [
      { id: '1', type: 'paragraph', base_content: { text: 'Paid Invoice' } }
    ];

    // Compute hashes and generate the sealed document payload
    const udsDoc = await sealDocument({
      title: 'Invoice #1004',
      blocks,
      signerKey: env.SIGNING_KEY
    });

    return new Response(JSON.stringify(udsDoc), {
      headers: { 'Content-Type': 'application/uds+json' }
    });
  }
}

3. Wrapping as Self-Opening HTML

Convert any raw UDS JSON payload to a self-opening HTML envelope before returning to the client:

import { wrapUDSInHTML } from '@universaldocument/csdk';

const htmlEnvelope = wrapUDSInHTML(udsDoc);
return new Response(htmlEnvelope, {
  headers: { 'Content-Type': 'text/html' }
});