Before I dive into the code and show you guys how to create an Azure IoT Central application programmatically, I would like to give an introduction of what an Azure IoT Central is and why do we need it?

Azure IoT Central is a

Connect fast with a hassle-free, hosted IoT platform

Easily connect, monitor, and manage your Internet of Things (IoT) assets at scale. Azure IoT Central is a hosted, extensible software as a service (SaaS) platform that simplifies setup of your IoT solution and helps reduce the burden and costs of IoT management, operations, and development. Provide customers superior products and service while expanding your business possibilities.

Looks like this platform is a perfect fit for business who already have tons of devices and wish to have them connected to the cloud and able to manage them easily and effortless. Wish to connect your MXChip IoT DevKit, Raspberry Pi, Windows IoT Core device, SensorTile.box device and RuuviTag sensor to Azure IoT Central? Follow this tutorial to do it. Wish to have more information on this great product? Check it out here.

This should gives you some idea of what an Azure IoT Central is and how it can be useful for people who just want to try it out as well as enterprise customers who already have tons of devices that are waiting for it to be connected and started to send some telemetry data.

Let’s get to the coding part, shall we?

Before you can create any resource in Azure Portal, you would need to have an account with them. Create your Azure free account over here today to get started.

Next, you would also need a subscription id, don’t have it? Follow this short tutorial to get one.

You would also need a resource group before you can create any application within the Azure IoT Central, create one over at the Azure Portal or create one through the Azure CLI.

Okay, now that you have an account with Azure Portal, subscription id, and resource group. You should now have everything you need to create an Azure IoT Central application programmatically.

For simplicity, we are going to use TypeScript as our primary programming language.

Let’s start with importing the necessary node modules,

  import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
  import { IotCentralClient } from "@azure/arm-iotcentral";
  import { App, OperationInputs } from "@azure/arm-iotcentral/lib/models/index";

source code hosted on GitHub

@azure/ms-rest-nodeauth are needed for authentication in Azure. @azure/arm-iotcentral are needed for creating/updating/deleting resources in Azure IoT Central.

Next, you would also need to hard-code your sensitive information in the code so that Azure IoT Central can use it to create resource for you. (It is recommended that you use environment variable to store your secrets instead of putting it in the code)

  const SUBSCRIPTIONID: string = "FILL IN SUB ID";
  const RESOURCEGROUPNAME: string = "myResourceGroup";
  const RESOURCENAME: string = "my-app-name";

  const NAME: OperationInputs = {
    name: RESOURCENAME
  }
  const NEWAPP: App = {
    subdomain: RESOURCENAME,
    sku: {
        name: 'S1'
    },
    location: 'West US',
    displayName: RESOURCENAME
  };

source code hosted on GitHub

The code usually begins with a login, (we write it as an async function since we are trying to avoid callback hells)

  async function login(): Promise<msRestNodeAuth.DeviceTokenCredentials> {
    const creds = await msRestNodeAuth.interactiveLogin();
    return new Promise<msRestNodeAuth.DeviceTokenCredentials>(resolve => resolve(creds));
  }

source code hosted on GitHub

Once you are logged-in, let’s check if your app name is available since at the end, your url will look like my-app-name.azureiotcentral.com

  async function checkIfNameExist(creds): Promise<IotCentralClient> {
    const client = new IotCentralClient(creds, SUBSCRIPTIONID);
    const result = await client.apps.checkNameAvailability(NAME);
    console.log(result);
    return new Promise<IotCentralClient>(resolve => resolve(client));
  }

source code hosted on GitHub

Next, you would also need to create an application within the Azure IoT Central in order to provision and monitor your devices.

  async function createOrUpdateApp(client): Promise<IotCentralClient> {
    const result = await client.apps.createOrUpdate(RESOURCEGROUPNAME, RESOURCENAME, NEWAPP);
    console.log(result);
    return new Promise<IotCentralClient>(resolve => resolve(client));
  }

source code hosted on GitHub

Once the Azure IoT Central’s application is created. You can access it through my-app-name.azureiotcentral.com

Ever want to see information about your application? do the following,

  async function retrieveAppInfo(client): Promise<IotCentralClient> {
    const result = await client.apps.get(RESOURCEGROUPNAME, RESOURCENAME)
    console.log(result);
    return new Promise<IotCentralClient>(resolve => resolve(client));
  }

source code hosted on GitHub

You can also delete application programmatically by doing this,

  async function deleteApp(client): Promise<IotCentralClient> {
    const result = await client.apps.deleteMethod(RESOURCEGROUPNAME, RESOURCENAME);
    console.log(result);
    return new Promise<IotCentralClient>(resolve => resolve(client));
  }

source code hosted on GitHub

Finally, call the async function like this,

  login()
    .then(checkIfNameExist)
    .then(createOrUpdateApp)
    .then(retrieveAppInfo)
    // .then(deleteApp)
    .then(() => {
        console.log("done");
    })
    .catch(err => {
        console.log('An error occurred:');
        console.dir(err, {
            depth: null,
            colors: true
        });
    });

source code hosted on GitHub

Now, you have learned to login, check if app name is available, create or update app, retrieve app info, delete app within Azure IoT Central.

Okay, do let me know in the comments below if you have any questions/concerns and I would be happy to help in any way. Happy using Azure IoT Central!

Wrapping Up

Hopefully you enjoyed this article and will inspire you to try out Azure IoT Central if you have not done so. Let me know if this helps you. Thank you for reading!

Resources

I’ll try to keep this list current and up to date. If you know of a great resource you’d like to share or notice a broken link, please let us know.

Getting started