Example Workflow

Example workflow to start creating and updating your Documents with Context Protocol.

1. Create a Document

First, create a document within your Domain using some initial data.

import { Context } from '@contextprotocol/sdk';

const ctx = new Context({ apiKey: 'your_api_key_here' });

async function createDocument() {
    const data = {
        title: "Initial Document",
        content: "This is the initial content of the document."
    };

    const pathToFile = "myfiles/path/firstfile";
    const document = await ctx.createDocument(pathToFile, data, [], false);
    console.log(`Document created on: ${document.path}`);

    return document;
}

2. Define and create a Template

Next, define a JSON schema and use it to create a new Template on your domain.

async function createTemplate() {
    const schema = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "object",
        "properties": {
            "title": {
                "type": "string",
                "description": "The title of the document."
            },
            "content": {
                "type": "string",
                "description": "The content of the document."
            }
        },
        "required": ["title", "content"]
    };
    
    const templatePath = "templates/newtemplate";
    const isTemplate = true;
    const template = await ctx.createDocument(templatePath, schema, [], isTemplate);
    console.log(`Template created on: ${template.path}`);

    return template;
}

3. Update the Document

Finally, update the previously created document with new data and apply the newly created template.

async function updateDocument(documentPath, templatePath) {
    const updatedData = {
        title: "Updated Document",
        content: "This is the updated content of the document."
    };
    const templatesToInstall = [templatePath];  // Use the path of the newly created template

    const doc = await ctx.document(documentPath)
    const updatedDoc = await doc.update(updatedData, templatesToInstall);
    console.log("Document updated successfully.");
}

Last updated