Microsoft provides SDKs for both building IoT devices and accessing the IoT Hub (based on several programming languages).
Regarding the IoT Hub, with the SDK you are able to eg. register and control devices.
Still, some actions are possible with the IoT Hub which is not supported by the SDK.
For those actions, Microsoft supports an IoTHub Rest API which extends the capabilities of the SDK.
In the past, I demonstrated how to work with C2D messages using the REST API.
A few days back, I came along with this MS Learn question about IoT Hub message counts. This also has to be tackled by the REST API.
Let’s see how.
Access to an Azure IoT Hub is secured by credentials, just like any other Azure resource.
To make use of the Rest API, we need to provide some credentials in the form of a bearer token.
In the previous IoT Hub Rest API blog, I got a token from some Microsoft app. This time, we will generate one on the fly!
First, create an identity in Azure using these PowerShell commands (see here for more details):
az login
az ad sp create-for-rbac -n "testaccount"
This creates an ‘testaccount’ with its credentials and secrets.
Note: This is a one-time action. You can revoke the user at any time you want. This will stop all code using the credential secrets.
Fill these credential secrets in, in the code below:
internal class Program
{
private static void Main(string[] args)
{
// Take these values from the login credentials of our new user
var appId = "[testaccount user Appid]";
var password = "[testaccount user password]";
var tennant = "[testaccount user tennant]";
var bearer = GetBearer(tennant, appId, password);
Console.WriteLine($"bearer {bearer}");
// Take the following values from the IoT Hub overview page
GetQuota(bearer, "[subscriptionId]", "[resourceGroup]", "[iotHub]");
Console.WriteLine("Press a key to exit");
Console.ReadKey();
}
private static void GetQuota(string bearer, string subscriptionId, string resourceGroup, string iotHub)
{
var url = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Devices/IotHubs/{iotHub}/quotaMetrics?api-version=2018-04-01";
using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearer);
var req = new HttpRequestMessage(HttpMethod.Get, url);
using var res = client.SendAsync(req).Result;
var jsonString = res.Content.ReadAsStringAsync().Result;
Console.WriteLine(jsonString);
}
private static string GetBearer(string tennant, string appId, string password)
{
var nvc = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", appId),
new KeyValuePair<string, string>("client_secret", password),
new KeyValuePair<string, string>("resource", "https://management.azure.com/")
};
var url = $"https://login.microsoftonline.com/{tennant}/oauth2/token";
using var client = new HttpClient();
var req = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = new FormUrlEncodedContent(nvc)
};
using var res = client.SendAsync(req).Result;
var jsonString = res.Content.ReadAsStringAsync().Result;
var json = JsonConvert.DeserializeObject<dynamic>(jsonString);
return json.access_token;
}
}
The IoT Hub related values can be taken from the overview blade of the targetted IoT Hub.
Note: due to the secrets being use, it’s advised to run the logic only in the cloud, not on a local device.
This code first creates the bearer token and uses that bearer token to make the actual call.
Note: The bearer token comes with an expiration DateTime (actually a Unix timestamp) which translates into a one hour expiration time. On multiple API calls, you can buy yourself some time by reusing the token within that one hour.
The result is seen here:

This result shows four Quota values:
- The current total messages for today
- The max number of messages allowed per day for this IoT Hub (based on the tier and number of units)
- The number of registered devices
- The maximum allowed devices for this IoT Hub
Do not expect an actual real-time representation of the number of messages received. There can be experienced a delay.
Conclusion
Using the Rest API of the IoT Hub, you can access many more IoT Hub features compared with the SDK.
So please give it a try.