OriginTrail
  • Get started with OriginTrail
  • OriginTrail Ecosystem — Call for Papers (Coming Soon)
  • Key Concepts
    • Decentralized Knowledge Graph (DKG)
    • DKG key concepts
  • Build with DKG
    • Quickstart (test drive the DKG in 5 mins)
      • Quickstart with Node.js
      • Quickstart with Python
    • ChatDKG builder toolkit
      • DKG SDK
        • Development environment setup
        • DKG Javascript SDK (dkg.js)
          • Interact with DKG paranets
          • Knowledge submission & curation
          • Paranet's incentives pool implementation
        • DKG Python SDK (dkg.py)
      • DKG paranets
        • Deploying a DKG paranet
        • Building with DKG paranets
        • Syncing a DKG Paranet
        • Initial Paranet Offerings (IPOs)
          • IPO specification
          • Launching your IPO
          • Paranet's incentives pool
          • IPO voting
      • AI agents
        • ElizaOS DKG agent
        • Custom DKG Python agent
        • Custom DKG JavaScript agent
    • DKG Edge Node
      • DKG Edge Node architecture
      • Get started with the Edge Node boilerplate
        • Automated setup with the installer
        • Manual setup
        • Usage example
      • Customize & build with the Edge Node
      • Knowledge Mining and dRAG examples
      • DKG Edge Node inception program
      • DKG Edge Node API documentation
    • DKG Core Node
      • Run a V8 Core Node on testnet
        • Preparation for V8 DKG Core Node deployment
        • V8 DKG Core Node installation
      • Run a V8 Core Node on mainnet
        • Preparation for V8 DKG Core Node deployment
        • V8 DKG Core Node installation
  • Delegated staking
    • Delegated staking—Introduction
      • Step-by-step staking
      • Redelegating stake
  • Integrated Blockchains
    • Base blockchain
      • Connect to Base
    • Gnosis chain
      • Connect to Gnosis
    • NeuroWeb
    • Teleport instructions - NeuroWeb
    • Bridging to Moonbeam
    • Deployed smart contracts
  • Bounties & rewards
    • General bug bounty
    • Code contributions & V8 bug bounty
  • Whitepapers & RFCs
    • OriginTrail whitepaper
    • OriginTrail RFCs
  • Useful Resources
    • What's new with OriginTrail V8
    • DKG V8 guidebook
      • Protocol updates
      • Feature roadmap
      • How to upgrade to V8?
    • Public nodes
    • Tutorials
    • Test token faucet
    • Development principles
    • Community created resources
    • Linked data & knowledge graphs
    • Available networks, network details and RPCs
    • OT Node Engine implementation details
      • Modules
      • Command Executor
    • Contribution guidelines
      • Guidelines for automated test contributions
    • Explore the OriginTrail ecosystem
Powered by GitBook
On this page
  • 🚀 Step 1: Set up your project and install the SDK
  • 1.1 Create your project folder
  • 1.2 Open a terminal in your project folder
  • 1.3 Initialize project and install SDK
  • 1.4 Set up your private key
  • 📝 Step 2: Publish your first knowledge asset
  • Example Knowledge Asset
  • JavaScript implementation
  • 🔍 Step 3: Query & Retrieve Your Knowledge Asset
  • 🎉 Congratulations!
  • 🚀 What's Next?

Was this helpful?

Edit on GitHub
  1. Build with DKG
  2. Quickstart (test drive the DKG in 5 mins)

Quickstart with Node.js

Create your first Knowledge Asset on OriginTrail Decentralized Knowledge Graph using the DKG JavaScript SDK

🚀 Step 1: Set up your project and install the SDK

1.1 Create your project folder

  1. Create a new folder on your computer named OT-DKG-Quickstart

  2. Open the folder in your code editor:

    • VS Code: File → Open Folder → Select OT-DKG-Quickstart

    • Cursor: File → Open → Select OT-DKG-Quickstart

1.2 Open a terminal in your project folder

  • VS Code: Right-click the folder in Explorer → "Open in Integrated Terminal"

  • Cursor: Click the folder → "Open Terminal Here"

1.3 Initialize project and install SDK

# Initialize your project
npm init -y

# Install the DKG SDK
npm install dkg.js@latest

1.4 Set up your private key

  1. Create a new file named .env in your project root

  2. Add your wallet's private key:

PRIVATE_KEY="your_private_key_here"

Note: Replace your_private_key_here with your actual private key. Never share this file or commit it to version control!

How to get your private key from MetaMask:

  1. Open MetaMask

  2. Click the three dots menu

  3. Select "Account Details"

  4. Click "Export Private Key"

  5. Enter your password and copy the key

📝 Step 2: Publish your first knowledge asset

A Knowledge Asset (KA) is structured data stored on the DKG. Let's create a simple one!

Example Knowledge Asset

Here's the JSON-LD KA we'll publish

{
  "@context": "https://schema.org/",
  "@type": "CreativeWork",
  "@id": "urn:first-dkg-ka:info:hello-dkg",
  "name": "Hello DKG",
  "description": "My first Knowledge Asset on the Decentralized Knowledge Graph!"
}

💡 Tip: You can customize the name and description fields with your own content! Make sure to also change the last part of the @id (e.g., from "hello-dkg" to "my-asset-name") to keep your asset unique for querying later!

JavaScript implementation

  1. Create a new file named publish_ka.mjs in your project folder

  2. Copy and paste this code, replacing the content part with your custom values if you'd like (as described in the Example Knowledge Asset section above):

import DKG from 'dkg.js';
import { BLOCKCHAIN_IDS } from 'dkg.js/constants';
import 'dotenv/config';

const OT_NODE_HOSTNAME = 'https://v6-pegasus-node-02.origin-trail.network';
const OT_NODE_PORT = '8900';

const DkgClient = new DKG({
    endpoint: OT_NODE_HOSTNAME,
    port: OT_NODE_PORT,
    blockchain: {
        name: BLOCKCHAIN_IDS.NEUROWEB_TESTNET,
        privateKey: process.env.PRIVATE_KEY,
    },
    maxNumberOfRetries: 300,
    frequency: 2,
    contentType: 'all',
    nodeApiVersion: '/v1',
});

(async () => {
    const content = {
        public: {
            '@context': 'https://www.schema.org',
            '@id': 'urn:first-dkg-ka:info:hello-dkg',
            '@type': 'CreativeWork',
            'name': 'Hello DKG',  // 🎯 Remember this name for querying!
            'description': 'My first Knowledge Asset on the Decentralized Knowledge Graph!'
        },
    };

    console.log('Publishing Knowledge Asset...');
    
    const create_result = await DkgClient.asset.create(content, {
        epochsNum: 2,
        minimumNumberOfFinalizationConfirmations: 3,
        minimumNumberOfNodeReplications: 1,
    });

    console.log('Success! Your Knowledge Asset has been published:');
    console.log(JSON.stringify(create_result, null, 2));
})();
  1. Run the script by entering the following into the terminal:

node publish_ka.mjs

⏳ Note: Publishing might take 30-60 seconds. You'll see a success message with details about your KA!

🔍 Step 3: Query & Retrieve Your Knowledge Asset

Now let's retrieve your published Knowledge Asset using a SPARQL query.

  1. Create a new file in your project folder named query_ka.mjs , paste in the code below, and update the query name if you changed the name of the KA when creating it:

import DKG from 'dkg.js';
import { BLOCKCHAIN_IDS } from 'dkg.js/constants';
import 'dotenv/config';

const OT_NODE_HOSTNAME = 'https://v6-pegasus-node-02.origin-trail.network';
const OT_NODE_PORT = '8900';

const DkgClient = new DKG({
    endpoint: OT_NODE_HOSTNAME,
    port: OT_NODE_PORT,
    blockchain: {
        name: BLOCKCHAIN_IDS.NEUROWEB_TESTNET,
        privateKey: process.env.PRIVATE_KEY,
    },
    maxNumberOfRetries: 300,
    frequency: 2,
    contentType: 'all',
    nodeApiVersion: '/v1',
});

// ⚠️ IMPORTANT: Update "hello dkg" if you changed the name when publishing!
const query = `
    PREFIX schema: <http://schema.org/>
    SELECT ?s ?name ?description
    WHERE {
        ?s schema:name ?name ;
           schema:description ?description .
        FILTER(LCASE(?name) = "hello dkg")
    }
`;

(async () => {
    console.log('Querying for your Knowledge Asset...');
    
    try {
        const queryResult = await DkgClient.graph.query(query,"SELECT");
        console.log('Found your Knowledge Asset!');
        console.log(JSON.stringify(queryResult, null, 2));
    } catch (error) {
        console.error("Error querying the asset:", error);
        console.log("Tip: Make sure the name in your query matches the name you used when publishing!");
    }
})();
  1. Run the query by entering the following into your terminal:

node query_ka.mjs

⚠️ Query Troubleshooting

If your query returns no results:

  1. Check the name: Ensure the name in your SPARQL query exactly matches what you published

  2. Wait a moment: Sometimes it takes a few seconds for the KA to be fully indexed

  3. Verify publication: Check that your publish script completed successfully

Example: If you changed the name to "My Custom KA", update your query:

FILTER(LCASE(?name) = "my custom ka")

🎉 Congratulations!

You've successfully:

  • Published your first Knowledge Asset to the DKG

  • Created a verifiable, ownable asset that you control

  • Retrieved it using a SPARQL query

  • Learned the basics of the OriginTrail SDK

Your Knowledge Asset now lives on the DKG, where it's:

  • Owned by you - secured by your wallet

  • Verifiable - anyone can cryptographically verify you as the publisher

  • Discoverable - can be found by anyone while you maintain ownership

🚀 What's Next?

Now that you've mastered the basics, here's what comes next.

Everything you need to build with the DKG: publish and query knowledge, deploy autonomous AI agents, and launch decentralized knowledge ecosystems with paranets—all from a unified toolkit.

Take control of your stack: deploy agents, paranets, and private Knowledge Assets on your own infrastructure—fully customizable and privacy-first.

You’re now on the path to building a semantic, decentralized, and AI-ready knowledge ecosystem!

PreviousQuickstart (test drive the DKG in 5 mins)NextQuickstart with Python

Last updated 9 hours ago

Was this helpful?

Explore the ChatDKG builder toolkit
Build with your Edge Node