Azure Digital Twins APIs and SDKs
This article gives an overview of the Azure Digital Twins 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.
Azure Digital Twins comes equipped with control plane APIs, data plane APIs, and SDKs 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.
- The SDKs take advantage of the existing APIs to allow for ease of development of custom applications making use of Azure Digital Twins. The control plane SDKs are available in .NET (C#) and Java, and the data plane SDKs are available in .NET (C#), Java, JavaScript, and Python.
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 call the APIs directly by referencing the latest Swagger folder in the control plane Swagger repo. This folder also includes a folder of examples that show the usage.
- You can currently access SDKs for control APIs in...
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 call the APIs directly, by...
- Referencing the latest Swagger folder in the data plane Swagger repo. This folder also includes a folder of examples that show the usage.
- Viewing the API reference documentation.
- You can use the .NET (C#) SDK. To use the .NET SDK...
- You can view and add the package from NuGet: Azure.DigitalTwins.Core.
- You can view the SDK reference documentation.
- You can find the SDK source, including a folder of samples, in GitHub: Azure IoT Digital Twins client library for .NET.
- You can see detailed information and usage examples by continuing to the .NET (C#) SDK (data plane) section of this article.
- You can use the Java SDK. To use the Java SDK...
- You can view and install the package from Maven:
com.azure:azure-digitaltwins-core - You can view the SDK reference documentation
- You can find the SDK source in GitHub: Azure IoT Digital Twins client library for Java
- You can view and install the package from Maven:
- You can use the JavaScript SDK. To use the JavaScript SDK...
- You can view and install the package from npm: Azure Azure Digital Twins Core client library for JavaScript.
- You can view the SDK reference documentation.
- You can find the SDK source in GitHub: Azure Azure Digital Twins Core client library for JavaScript
- You can use the Python SDK. To use the Python SDK...
- You can view and install the package from PyPi: Azure Azure Digital Twins Core client library for Python.
- You can view the SDK reference documentation.
- You can find the SDK source in GitHub: Azure Azure Digital Twins Core client library for Python
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 packages to your project:
- Azure.DigitalTwins.Core: The package for the Azure Digital Twins SDK for .NET.
- Azure.Identity: The library that provides tools to help with authentication against Azure.
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 twinBasicDigitalTwinComponent: Generically represents a component in theContentsproperties of aBasicDigitalTwinBasicRelationship: Generically represents the core data of a relationshipDigitalTwinsJsonPropertyName: 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
DigitalTwinsClientclass. The constructor requires credentials that can be obtained with different kinds of authentication methods in theAzure.Identitypackage. For more onAzure.Identity, see its namespace documentation. - You may find the
InteractiveBrowserCredentialuseful 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 aboutInteractiveBrowserCredential, 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
DigitalTwinsClientclass. - 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
trysection, and catch at leastRequestFailedExceptions. 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), whereTis the class of return object for the service call. The Response class encapsulates the service return and presents return values in itsValuefield. - Service methods with paged results return
Pageable<T>orAsyncPageable<T>as results. For more about thePageable<T>class, see its reference documentation; for more aboutAsyncPageable<T>, see its reference documentation. - You can iterate over paged results using an
await foreachloop. 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.
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: