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
  • Fetch Documents
  • Document Properties
  • List Document Versions
  • Fetch a Specific Document Version
  • Create a Document
  • Update a Document
  • Adding Metadata to a Document

Was this helpful?

  1. DEVELOPERS
  2. Typescript SDK

Managing Documents

Public Data

Remember, all data stored on Context is public. Avoid storing any secrets or private information without additional measures to protect confidentiality.

Fetch Documents

Fetch a specific document or template or asset, from any domain:

// Fetch a specific document
const document = await ctx.document("context.startup/link3");  // "domain/path/to/file"

Document Properties

Access and display properties of a document:

console.log(document.data.path);
console.log(document.data.versionNumber);
console.log(document.data.data);
console.log(document.data.metadata);
console.log(document.data.templates);
console.log(document.data.type); // Document | Template | Asset
console.log(document.data.txId);
console.log(document.data.createdAt);
console.log(document.data.updatedAt);
console.log(JSON.stringify(document.data));

List Document Versions

Fetch a list of all versions of a document:

const documentVersions = await document.data.versions();

Fetch a Specific Document Version

You can fetch a specific version of a document in two different ways:

// By using the version method of the document:
const document = await ctx.document("document_path");
const documentVersion = await document.data.getVersion("X.Y.Z");

// By specifying the version directly the document path:
const documentInVersionXYZ = await ctx.document("document_path?v=X.Y.Z");

Create a Document

Steps to create a new document within a domain:

const data = YOUR_JSON_DATA;  // JSON data for the document
const templates = ["template_path"];  // Optional array of template paths
const metadata = { name: "Document Name", description: "Document Description", readme: "ctx:domain/files/my_markdown" };  // Optional metadata
const documentPath = "mypath/files/document";

const newDocument = await ctx.createDocument(documentPath, data, templates, metadata);

Update a Document

Update an existing document:

const updatedData = YOUR_UPDATED_JSON_DATA;  // Updated JSON data
const document_path = "mypath/files/document";

const document = await ctx.document(documentPath);
if (!document.success) {
    // Handle error
}
const result = await document.data.update(updatedData);

Adding Metadata to a Document

You can add metadata to a document using the addMetadata method. The metadata object should contain the following (optional) fields: name, description, and readme as shown below:

const metadata = { name: "Document Name", description: "Document Description", readme: "ctx:domain/files/my_markdown" };
await document.data.addMetadata(metadata);
PreviousWorking with DomainsNextCreating Templates

Last updated 6 months ago

Was this helpful?

🏗️