Getting started

Welcome to Context Protocol.

Introduction


The Context SDK is a robust and flexible toolkit designed for developers to interact programmatically with Context Protocol. It allows for efficient management of your Documents through Context's API, streamlining operations such as creation, modification, and fetching data.

Installation


Install the Context SDK to your TypeScript project using npm:

npm install @contextprotocol/sdk

Quick Start


This guide provides the basic steps to establish a connection and perform common operations using the Context SDK.

Setting Up Your Connection

To use the Context SDK, you first need to obtain an API key. You can get your API key by creating an account at app.ctx.xyz.

Initialize the SDK with your API key to start interacting with Context services:

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

const ctx = new Context({ apiKey: "your_api_key_here" }); // Replace with your API key

Working with Domains


Fetch Domain Information

Fetch details of a specific domain or the default domain associated with your API key:

// Fetch the default domain
const yourDomain = await ctx.domain();

// Fetch a specific domain
const domain = await ctx.domain("domain_name");  // Returns null if not found

Domain properties

console.log(domain.name);
console.log(domain.documents);
console.log(domain.owner);
console.log(domain.createdAt);
console.log(domain.updatedAt);

Managing Documents


Fetch Documents

Fetch a specific document, from any domain:

// Fetch a specific document
const document = await ctx.document("document_path");  // Returns null if not found

// Fetch a specific version of a document
const document = await ctx.document("document_path");
const documentInVersionXYZ = await ctx.document("document_path?v=X.Y.Z"); // Get a specific version of a document

Document Properties

Access and display properties of a document:

console.log(document.path);
console.log(document.versionNumber);
console.log(document.data);
console.log(document.createdAt);
console.log(document.updatedAt);
console.log(JSON.stringify(document));

List Document Versions

Fetch a list of all versions of a document:

const documentVersions = await document.versions();

Fetch a Specific Document Version

Fetch a specific version of a document:

const documentVersion = await document.version("X.Y.Z");

Create a Document

Steps to create a new document within your domain:

const data = YOUR_AWESOME_JSON_DATA;  // JSON data for the document
const templates = ["template_path"];  // Optional array of template paths

const newDocument = await ctx.createDocument("document_path", data, templates, false);

Update a Document

Update an existing document:

const updatedData = YOUR_UPDATED_AWESOME_JSON_DATA;  // Updated JSON data
const templatesToInstall = ["template_path"];  // Optional array of templates
const versionNumber = "X.Y.Z";  // Optional specific version

await document.update(updatedData, templatesToInstall, versionNumber);

Creating Templates

  1. Define a JSON Schema for a Template

Create a JSON schema directly or from a TypeScript interface:

// Direct JSON Schema definition
const schema = {
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "name": {"type": "string", "description": "The name of the organization."},
    "description": {"type": "string", "description": "A brief description of the organization."},
    "website": {"type": "string", "description": "The URL of the organization's website.", "format": "uri"}
  },
  "required": ["name"],
};

// Generate JSON schema from a TypeScript interface
const dataName = 'User';
const myDataType = `interface ${dataName} {
  name: string;
  age: number;
}`;
const schema = generateJsonSchema(dataName, myDataType);
  1. Create a new Template

Use the defined schema to create a new template:

const template = await ctx.createDocument("template_path", schema, [], true);

Uploading Assets


If you want to upload an asset to Contexts, you have to upload it as follows:

const ctxDocumentPath = "document/path";
const localFilePath = "file/path.jpg";
const asset = await ctx.createAsset(ctxDocumentPath, localFilePath, metadata /* optional */);

Create your first Domain


We are scheduled to launch on Mainnet in the coming weeks. If you are interested in becoming one of our early users, we encourage you to create your initial domain on our Beta using the WebApp.

For inquiries about creating a premium and verified domain, please contact us at support@ctx.xyz

Last updated