Azure IoT Device Provisioning Service HTTP Transport

1.9.1 · active · verified Wed Apr 22

The `azure-iot-provisioning-device-http` package provides the HTTP transport layer for interacting with the Azure IoT Hub Device Provisioning Service (DPS) in Node.js applications. It is a crucial component for devices that need to register with DPS using the HTTP protocol, supporting both X.509 certificate and Trusted Platform Module (TPM) authentication methods. As of its latest release, `1.9.2`, this package is part of the broader Azure IoT SDK for Node.js, which typically sees new releases quarterly or bi-annually, bundling updates across various related packages. This transport package is designed to be used in conjunction with the `azure-iot-provisioning-device` client package and a security client package (e.g., `azure-iot-security-x509` or `azure-iot-security-tpm`) to form a complete device provisioning solution, offering a flexible choice of underlying communication protocols.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to register a device with Azure IoT Hub Device Provisioning Service (DPS) using HTTP transport and X.509 certificate authentication.

import { ProvisioningDeviceClient } from 'azure-iot-provisioning-device';
import { Http } from 'azure-iot-provisioning-device-http';
import { X509Security } from 'azure-iot-security-x509';
import { readFileSync } from 'fs';

// Replace with your DPS ID Scope and device certificate paths
const idScope = process.env.IOTHUB_DPS_ID_SCOPE ?? '';
const registrationId = process.env.IOTHUB_DPS_REGISTRATION_ID ?? 'my-x509-device';
const certificateFile = process.env.X509_CERT_FILE ?? 'path/to/device_cert.pem';
const keyFile = process.env.X509_KEY_FILE ?? 'path/to/device_key.pem';

async function main() {
  if (!idScope || !certificateFile || !keyFile) {
    console.error("Please set IOTHUB_DPS_ID_SCOPE, X509_CERT_FILE, and X509_KEY_FILE environment variables.");
    process.exit(1);
  }

  try {
    const certificate = readFileSync(certificateFile).toString();
    const privateKey = readFileSync(keyFile).toString();

    // Create a new X509 security client
    const securityClient = new X509Security(registrationId, certificate, privateKey);

    // Create the provisioning client with HTTP transport and X509 security
    const provisioningClient = ProvisioningDeviceClient.create(
      idScope,
      new Http(), // The HTTP transport from this package
      securityClient
    );

    console.log(`Attempting to register device '${registrationId}' with DPS...`);

    // Register the device
    const result = await provisioningClient.register();

    console.log('Registration result:');
    console.log(JSON.stringify(result, null, 2));
    console.log(`Device successfully registered with IoT Hub: ${result.assignedHub}`);
    console.log(`Device ID: ${result.deviceId}`);
  } catch (err: any) {
    console.error('Device registration failed:', err.message);
    if (err.responseBody) {
      console.error('DPS Response:', err.responseBody);
    }
    process.exit(1);
  }
}

main();

view raw JSON →