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
  • Python 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 Python

Create your first Knowledge Asset on OriginTrail Decentralized Knowledge Graph using the DKG Python 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

# Create a virtual environment
python -m venv env

# Activate virtual environment
# On Mac/Linux:
source env/bin/activate
# On Windows:
env\Scripts\activate

# Install the DKG SDK
pip install dkg

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!

Python implementation

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

  2. Copy and paste this code, replacing the content part with your custom values if you'd like:

import json
import time

from dkg import DKG
from dkg.providers import BlockchainProvider, NodeHTTPProvider
from dkg.constants import BlockchainIds

node_provider = NodeHTTPProvider(
    endpoint_uri="https://v6-pegasus-node-02.origin-trail.network:8900",
    api_version="v1",
)

blockchain_provider = BlockchainProvider(
    BlockchainIds.NEUROWEB_TESTNET.value,
)

config = {
    "max_number_of_retries": 300,
    "frequency": 2,
}
dkg = DKG(node_provider, blockchain_provider, config)

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!"
    }
}

print("Publishing Knowledge Asset...")

create_asset_result = dkg.asset.create(content, {
    "epochs_num": 2,
    "minimum_number_of_finalization_confirmations": 3,
    "minimum_number_of_node_replications": 1
})

print("Success! Your Knowledge Asset has been published:")
print(json.dumps(create_asset_result, indent=4))
  1. Run the script by entering the following into the terminal:

python publish_ka.py

Note: On some systems, like Mac and Linux, you may need to use python3 instead of python , depending on your setup

⏳ 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.py , paste in the code below, and update the query name if you changed the name of the KA when creating it:

import json
import os
from dkg import DKG
from dkg.providers import BlockchainProvider, NodeHTTPProvider
from dkg.constants import BlockchainIds

node_provider = NodeHTTPProvider(
    endpoint_uri="https://v6-pegasus-node-02.origin-trail.network:8900",
    api_version="v1",
)

blockchain_provider = BlockchainProvider(
    BlockchainIds.NEUROWEB_TESTNET.value,
)

config = {
    "max_number_of_retries": 300,
    "frequency": 2,
}

dkg = DKG(node_provider, blockchain_provider, config)

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

print("Querying for your Knowledge Asset...")

try:
    query_result = dkg.graph.query(query)
    print("Found your Knowledge Asset!")
    print(json.dumps(query_result, indent=4))
except Exception as e:
    print(f"Error querying the asset: {e}")
    print("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:

python query_ka.py

Note: On some systems, like Mac and Linux, you may need to use python3 instead of python , depending on your setup

⚠️ 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 with Node.jsNextChatDKG builder toolkit

Last updated 11 hours ago

Was this helpful?

Explore the ChatDKG builder toolkit
Build with your Edge Node