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';constctx=newContext({ apiKey:'your_api_key_here' });asyncfunctioncreateDocument() {constdata= { title:"Initial Document", content:"This is the initial content of the document." };constpathToFile="myfiles/path/file_name";constdocument=awaitctx.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.
asyncfunctioncreateTemplate() {constschema= {"$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"] };consttemplatePath="templates/newtemplate";consttemplate=awaitctx.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.
asyncfunctionupdateDocument(documentPath) {constupdatedData= { title:"Updated Document", content:"This is the updated content of the document." };constdoc=awaitctx.document(documentPath)constupdatedDoc=awaitdoc.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.
asyncfunctioninstallTemplate(documentPath) {constdoc=awaitctx.document(documentPath)constupdatedDoc=awaitdoc.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.