Azure Digital Twins APIs and SDKs

Azure Digital Twins comes equipped with both control plane APIs and data plane APIs for managing your instance and its elements.

  • The control plane APIs are Azure Resource Manager (ARM) APIs, and cover resource management operations like creating and deleting your instance.
  • The data plane APIs are Azure Digital Twins APIs, and are used for data management operations like managing models, twins, and the graph.

This article gives an overview of the APIs available, and the methods for interacting with them. You can either use the REST APIs directly with their associated Swaggers (through a tool like Postman), or through an SDK.

Overview: control plane APIs

The control plane APIs are ARM APIs used to manage your Azure Digital Twins instance as a whole, so they cover operations like creating or deleting your entire instance. You'll also use these APIs to create and delete endpoints.

The most current control plane API version is 2020-12-01.

To use the control plane APIs:

You can also exercise control plane APIs by interacting with Azure Digital Twins through the Azure portal and CLI.

Overview: data plane APIs

The data plane APIs are the Azure Digital Twins APIs used to manage the elements within your Azure Digital Twins instance. They include operations like creating routes, uploading models, creating relationships, and managing twins, and can be broadly divided into the following categories:

  • DigitalTwinModels - The DigitalTwinModels category contains APIs to manage the models in an Azure Digital Twins instance. Management activities include upload, validation, retrieval, and deletion of models authored in DTDL.
  • DigitalTwins - The DigitalTwins category contains the APIs that let developers create, modify, and delete digital twins and their relationships in an Azure Digital Twins instance.
  • Query - The Query category lets developers find sets of digital twins in the twin graph across relationships.
  • Event Routes - The Event Routes category contains APIs to route data, through the system and to downstream services.

The most current data plane API version is 2020-10-31.

To use the data plane APIs:

You can also exercise date plane APIs by interacting with Azure Digital Twins through the CLI.

.NET (C#) SDK (data plane)

The Azure Digital Twins .NET (C#) SDK is part of the Azure SDK for .NET. It's open source, and is based on the Azure Digital Twins data plane APIs.

Note

For more information on SDK design, see the general design principles for Azure SDKs and the specific .NET design guidelines.

To use the SDK, include the NuGet package Azure.DigitalTwins.Core with your project. You'll also need the latest version of the Azure.Identity package. In Visual Studio, you can add these packages using the NuGet Package Manager (accessed through Tools > NuGet Package Manager > Manage NuGet Packages for Solution). You can also use the .NET command-line tool with the commands found in the NuGet package links below to add these to your project:

For a detailed walk-through of using the APIs in practice, see Code a client app.

Serialization helpers

Serialization helpers are helper functions available within the SDK for quickly creating or deserializing twin data for access to basic information. Since the core SDK methods return twin data as JSON by default, it can be helpful to use these helper classes to break down the twin data further.

The available helper classes are:

  • BasicDigitalTwin: Generically represents the core data of a digital twin
  • BasicDigitalTwinComponent: Generically represents a component in the Contents properties of a BasicDigitalTwin
  • BasicRelationship: Generically represents the core data of a relationship
  • DigitalTwinsJsonPropertyName: Contains the string constants for use in JSON serialization and deserialization for custom digital twin types

General API/SDK usage notes

Note

Please note that Azure Digital Twins doesn't currently support Cross-Origin Resource Sharing (CORS). For more info about the impact and resolution strategies, see the Cross-Origin Resource Sharing (CORS) section of Concepts: Security for Azure Digital Twins solutions.

The following list provides more detail and general guidelines for using the APIs and SDKs.

  • You can use an HTTP REST-testing tool like Postman to make direct calls to the Azure Digital Twins APIs. For more information about this process, see Make API requests with Postman.
  • To use the SDK, instantiate the DigitalTwinsClient class. The constructor requires credentials that can be obtained with different kinds of authentication methods in the Azure.Identity package. For more on Azure.Identity, see its namespace documentation.
  • You may find the InteractiveBrowserCredential useful while getting started, but there are several other options, including credentials for managed identity, which you'll likely use to authenticate Azure functions set up with MSI against Azure Digital Twins. For more about InteractiveBrowserCredential, see its class documentation.
  • Requests to the Azure Digital Twins APIs require a user or service principal that is a part of the same Azure Active Directory (Azure AD) tenant where the Azure Digital Twins instance exists. To prevent malicious scanning of Azure Digital Twins endpoints, requests with access tokens from outside the originating tenant will be returned a "404 Sub-Domain not found" error message. This error will be returned even if the user or service principal was given an Azure Digital Twins Data Owner or Azure Digital Twins Data Reader role through Azure AD B2B collaboration. For information on how to achieve access across multiple tenants, see Write app authentication code.
  • All service API calls are exposed as member functions on the DigitalTwinsClient class.
  • All service functions exist in synchronous and asynchronous versions.
  • All service functions throw an exception for any return status of 400 or above. Make sure you wrap calls into a try section, and catch at least RequestFailedExceptions. For more about this type of exception, see its reference documentation.
  • Most service methods return Response<T> or (Task<Response<T>> for the asynchronous calls), where T is the class of return object for the service call. The Response class encapsulates the service return and presents return values in its Value field.
  • Service methods with paged results return Pageable<T> or AsyncPageable<T> as results. For more about the Pageable<T> class, see its reference documentation; for more about AsyncPageable<T>, see its reference documentation.
  • You can iterate over paged results using an await foreach loop. For more about this process, see the relevant documentation.
  • The underlying SDK is Azure.Core. See the Azure namespace documentation for reference on the SDK infrastructure and types.

Service methods return strongly typed objects wherever possible. However, because Azure Digital Twins is based on models custom-configured by the user at runtime (via DTDL models uploaded to the service), many service APIs take and return twin data in JSON format.

Monitor API metrics

API metrics such as requests, latency, and failure rate can be viewed in the Azure portal.

From the portal homepage, search for your Azure Digital Twins instance to pull up its details. Select the Metrics option from the Azure Digital Twins instance's menu to bring up the Metrics page.

Screenshot showing the metrics page for Azure Digital Twins.

From here, you can view the metrics for your instance and create custom views.

Next steps

See how to make direct requests to the APIs using Postman:

Or, practice using the .NET SDK by creating a client app with this tutorial: