Konnektr Logo
Reference

Native SDK Reference

Note: This SDK talks to PostgreSQL directly, so it needs database access. For HTTP integrations use the Python SDK (konnektr-graph, the ADT-compatible client with search and pgvector support) or the Azure Digital Twins SDK.

Overview

The AgeDigitalTwins SDK provides a .NET client for direct database access to your Konnektr Graph instance. It connects directly to PostgreSQL with Apache AGE, bypassing the HTTP API layer for maximum performance.

When to Use This SDK

✅ Use the Native SDK when:

  • You have a Konnektr Graph deployment and access to its PostgreSQL database
  • You need maximum performance with direct database access
  • You want to perform bulk operations efficiently
  • You need advanced Cypher query capabilities
  • You require transaction support

❌ Don't use this SDK if:

  • You're integrating over HTTP without database access
  • You want to migrate to/from Azure Digital Twins easily
  • You prefer standard REST API patterns

Native SDK vs REST API Comparison

FeatureREST API (ADT / Python SDK)Native SDK
ConnectionHTTPS REST APIDirect PostgreSQL
AuthenticationOAuth 2.1 JWTDatabase credentials
PerformanceGoodExcellent
Bulk OperationsStandardOptimized
TransactionsNot supportedFull support
MigrationEasy to/from ADTCustom required

Installation

To install the SDK, use the following NuGet command:

Install-Package AgeDigitalTwins

Usage

Initialization

using AgeDigitalTwins;
using Npgsql;

var connectionString = "Host=localhost;Username=postgres;Password=yourpassword;Database=agedb";
var dataSource = NpgsqlDataSource.Create(connectionString);
var client = new AgeDigitalTwinsClient(dataSource, "digitaltwins");

Managing Digital Twins

Create or Replace a Digital Twin

var twin = new
{
    $dtId = "room1",
    name = "Room 1",
    $metadata = new { $model = "dtmi:com:adt:dtsample:room;1" }
};
await client.CreateOrReplaceDigitalTwinAsync("room1", twin);

Get a Digital Twin

var twin = await client.GetDigitalTwinAsync<object>("room1");
Console.WriteLine(twin);

Delete a Digital Twin

await client.DeleteDigitalTwinAsync("room1");

Querying the Graph

Execute a Query

var query = "SELECT * FROM DIGITALTWINS";
await foreach (var result in client.QueryAsync<object>(query))
{
    Console.WriteLine(result);
}

Managing Models

Create Models

var models = new[]
{
    "{ \"@id\": \"dtmi:com:adt:dtsample:room;1\", \"@type\": \"Interface\", \"displayName\": \"Room\" }"
};
await client.CreateModelsAsync(models);

List Models

await foreach (var model in client.GetModelsAsync<object>())
{
    Console.WriteLine(model);
}

Delete a Model

await client.DeleteModelAsync("dtmi:com:adt:dtsample:room;1");

Advanced Query Examples

Complex Graph Traversal

var query = @"
    MATCH (room:DigitalTwin {$dtId: 'room1'})
    MATCH (room)-[:contains]->(sensor:DigitalTwin)
    WHERE sensor.Temperature > 25.0
    RETURN room.$dtId, sensor.$dtId, sensor.Temperature
";

await foreach (var result in client.QueryAsync<object>(query))
{
    Console.WriteLine(result);
}

Bulk Insert with Transaction

using (var transaction = dataSource.CreateTransaction())
{
    var client = new AgeDigitalTwinsClient(transaction, "digitaltwins");
    
    // Insert multiple twins in a transaction
    for (int i = 0; i < 1000; i++)
    {
        var twin = new { $dtId = $"sensor{i}", Temperature = 20.0 + i };
        await client.CreateOrReplaceDigitalTwinAsync($"sensor{i}", twin);
    }
    
    await transaction.CommitAsync();
}

Performance Benefits

The native SDK provides significant performance advantages:

  • Direct Database Access: No HTTP serialization overhead
  • Connection Pooling: Efficient PostgreSQL connection management
  • Bulk Operations: Optimized batch inserts and updates
  • Transactions: ACID compliance with rollback support
  • Streaming: Memory-efficient query result streaming

Error Handling

try
{
    await client.CreateOrReplaceDigitalTwinAsync("twin1", twinData);
}
catch (AgeDigitalTwinsException ex) when (ex.ErrorCode == "ModelNotFound")
{
    Console.WriteLine($"Model not found: {ex.Message}");
}
catch (NpgsqlException ex)
{
    Console.WriteLine($"Database error: {ex.Message}");
}

Compatibility

  • .NET 6.0+: Full support for modern .NET
  • Async/Await: All operations are asynchronous
  • PostgreSQL 13+: Requires Apache AGE extension
  • Connection Pooling: Built-in Npgsql connection pooling
  • TrackLastUpdatedBy: Fully supported. The native SDK's custom JSON converters handle $lastUpdatedBy at both the twin-level ($metadata.$lastUpdatedBy) and per-property ($metadata.<property>.lastUpdatedBy) metadata. See Security for details.

Migration Considerations

From Azure Digital Twins SDK to Native SDK

// Before (Azure Digital Twins SDK)
var client = new DigitalTwinsClient(endpoint, credential);
var twin = await client.GetDigitalTwinAsync<BasicDigitalTwin>("twin1");

// After (Native SDK)
var client = new AgeDigitalTwinsClient(dataSource, "digitaltwins");  
var twin = await client.GetDigitalTwinAsync<object>("twin1");

From Native SDK to Azure Digital Twins SDK

For migration to the HTTP API (Konnektr Graph or Azure Digital Twins), you'll need to:

  1. Replace database connections with HTTP endpoints
  2. Implement OAuth 2.1 or Azure AD authentication
  3. Update query syntax to ADT query language
  4. Handle API rate limits and pagination

On this page