Context Protocol Docs
Start building
  • PROTOCOL OVERVIEW
    • ✖️About Context Protocol
    • 👾Why Context Protocol?
      • Verified AI-Ready Domains (VARD)
      • Training AI with verified data
      • SaaS Solution
    • 🤖Technology behind Context
    • 📈$TEX Token
      • Multi-chain token
      • Token Utility
      • Governance
      • Allocation
    • 🧐Use Cases and Applications
      • 📢Telegram bot for updates
      • 🔵Basenames integration
      • 🌎dWeb: Descentralized Website
      • 📌Decentralized Link3
  • DEVELOPERS
    • 👨‍💻Why build on Context
    • 🏗️Typescript SDK
      • Initialization
      • Working with Domains
      • Managing Documents
      • Creating Templates
      • Assets
      • Error Handling
    • 📑Example Workflow
    • ✨Create your first Domain
  • COMMUNITY
    • 🪂Community Program
    • 🫂Social Media
  • RESOURCES
    • 🛟Support
    • 🖥️WebApp
Powered by GitBook
On this page
  • 1. Create a Document
  • 2. Define and create a Template
  • 3. Update the Document
  • 3. Install the Template

Was this helpful?

  1. DEVELOPERS

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/file_name";
    const document = await ctx.createDocument(pathToFile, data);
    if (!document.success) {
        // Handle error
    }
    console.log(`Document created on: ${document.data.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 template = await ctx.createTemplate(templatePath, schema);
    if (!template.success) {
        // Handle error
    }
    console.log(`Template created on: ${template.data.path}`);

    return template;
}

3. Update the Document

Then, update the previously created document to be adapted to the template.

async function updateDocument(documentPath) {
    const updatedData = {
        title: "Updated Document",
        content: "This is the updated content of the document."
    };

    const doc = await ctx.document(documentPath)
    const updatedDoc = await doc.data.update(updatedData);
    if (!updatedDoc.success) {
        // Handle error
    }
    console.log(`Document updated with new data: ${updatedDoc.data.data}`);

    return updatedDoc;
}

3. Install the Template

Finally, install the created template.

async function installTemplate(documentPath) {

    const doc = await ctx.document(documentPath)
    const updatedDoc = await doc.data.install(["your_domain/templates/newtemplate"]);
    if (!updatedDoc.success) {
        // Handle error
    }
    console.log(`Document updated with this templates: ${updatedDoc.data.templates}`);

    return updatedDoc;
}

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

PreviousError HandlingNextCreate your first Domain

Last updated 11 months ago

Was this helpful?

📑