1.0.0
11/06/2024 08:11:25
The Context SDK is a robust and flexible toolkit designed for developers to interact programmatically with Context Protocol.
Context is an abstraction layer designed to integrate Web3 storage technologies, providing distinctive features that enhance data management. Dive into creation with our SDK, which helps users to organize, read and share public and private data in a sovereign, trusted and verified way. Saying goodbye to hashes, and embracing domains and documents within the blockchain ecosystem. We empower developers to innovate, providing the tools you need to build groundbreaking applications on top of our platform.
Every data on Context is publicly available, free, and forever for other developers to use everywhere through our public gateway - i.e. rpc.ctx.xyz/contextprotocol
Poolside, Context and Alex as Domains, with Documents inside that contain relations between them. The Template "web3/token" used on the "context/token" Document.
Context Protocol is a Web3 Semantic Layer that brings your data:
Install the Context SDK to your TypeScript project using npm:
npm install @contextprotocol/sdk
To use Context, you always need a domain. This domain acts as your namespace within Context, where all your documents will be stored. Then, you'll need to obtain an API key for your domain.
You can claim your domain and generate the API key by creating an account at app.ctx.xyz.
Initialize the SDK:
import { Context } from '@contextprotocol/sdk';
const ctx = new Context({ apiKey: "your_api_key_here" }); // Replace with your API key
<br />
Domains represent verified and curated entities, such as companies, projects, or individuals.
Fetch details of a specific domain or the default domain associated with your API key:
// Fetch your domain
const yourDomain = await ctx.domain();
// Fetch a specific domain
const domain = await ctx.domain("domain_name");
Access and display properties of a domain:
console.log(domain.data.name);
console.log(domain.data.documents);
console.log(domain.data.status);
console.log(domain.data.createdAt);
console.log(domain.data.updatedAt);
<br />
Fetch a specific document or template or asset, from any domain:
// Fetch a specific document
const document = await ctx.document("document_path"); // "domain/path/to/file"
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));
Fetch a list of all versions of a document:
const documentVersions = await document.data.versions();
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");
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 metadata = { name: "Document Name", description: "Document Description", readme: "ctx:domain/files/my_markdown" }; // Optional metadata
const newDocument = await ctx.createDocument("document_path", data, templates, metadata);
Update an existing document:
const updatedData = YOUR_UPDATED_AWESOME_JSON_DATA; // Updated JSON data
const document = await ctx.document("document_path");
if (!document.success) {
// Handle error
}
const result = await document.data.update(updatedData);
You can add/update metadata to a document anytime using the addMetadata
method. The metadata object should contain the following (optional) fields: name
, description
, and readme
(link to a Markdown document) as shown below:
const metadata = { name: "Document Name", description: "Document Description", readme: "ctx:domain/files/my_markdown" };
await document.data.addMetadata(metadata);
<br />
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);
Use the defined schema to create a new template:
const template = await ctx.createTemplate("template_path", schema);
Once we have the template, we can install it in a document by using the install
method:
const document = await ctx.document("document_path");
if (!document.success) {
// Handle error
}
const templateArrayToInstall = ["template_path"];
const newDoc = await document.data.install(templateArrayToInstall);
To uninstall a template from a document, we can use the uninstall
method:
const document = await ctx.document("document_path");
if (!document.success) {
// Handle error
}
const templateArrayToUninstall = ["template_path"];
const newDoc = await document.data.uninstall(templateArrayToUninstall);
<br />
As a user, you can upload assets to your domain. When uploading an asset, you can specify the document path where the asset will be stored.
const ctxDocumentPath = "document/path";
const localFilePath = "file/path.jpg";
const asset = await ctx.createAsset(ctxDocumentPath, localFilePath, metadata /* optional */);
You can update an existing asset by providing the document path and the local file path of the updated asset. It returns a document with a new version.
const localFilePath = "file/path.jpg";
const asset = await document.data.updateAsset(localFilePath, metadata /* optional */);
When calling a function, you can check if an error occurred by checking the error
property in the returned object:
const document = await ctx.document("document_path");
if(document.success === false){
console.error(document.error.error); // Error message
console.error(document.error.message); // Detailed error message
console.error(document.error.statusCode); // HTTP status code
}
If you have any questions or just want to brainstorm about how to integrate Context into your project, reach out to us on Telegram or by email.
For more detailed information and examples, visit the official Context SDK documentation.
This project is licensed under the MIT License. See the LICENSE file for details.
Key | Value |
---|---|
data | data |
Size: 186.00 bytes
Last update: 11/06/2024 08:11:25
Tx: