{"id":17167,"library":"azure-iot-device-http","title":"Azure IoT Device HTTP Transport","description":"This package provides the HTTP 1.1 transport layer for the Azure IoT Device SDK for Node.js. It enables IoT devices to communicate with Azure IoT Hub by sending device-to-cloud messages and receiving cloud-to-device commands over the HTTP protocol. This transport is particularly useful in environments where persistent connections (like MQTT or AMQP) are impractical or undesirable, such as highly constrained networks or when devices infrequently send data. The current stable version is 1.14.4. It is part of the larger Azure IoT SDK for Node.js monorepo, with releases typically bundled across multiple related packages, occurring quarterly or as needed for bug fixes and security updates. It differentiates itself by offering the HTTP protocol option, contrasting with `azure-iot-device-mqtt` and `azure-iot-device-amqp` which provide alternative transports.","status":"active","version":"1.14.4","language":"javascript","source_language":"en","source_url":"https://github.com/Azure/azure-iot-sdk-node","tags":["javascript","typescript"],"install":[{"cmd":"npm install azure-iot-device-http","lang":"bash","label":"npm"},{"cmd":"yarn add azure-iot-device-http","lang":"bash","label":"yarn"},{"cmd":"pnpm add azure-iot-device-http","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This package acts as a transport for the core `azure-iot-device` SDK, and relies on its client abstraction for device identity and messaging patterns.","package":"azure-iot-device","optional":false},{"reason":"Provides the foundational HTTP client functionalities and authentication mechanisms used by this transport.","package":"azure-iot-http-base","optional":false}],"imports":[{"note":"The primary client interface for all Azure IoT transports is `Client` from the `azure-iot-device` package. `clientFromConnectionString` and `clientFromSharedAccessSignature` are static methods on this `Client` class.","wrong":"const Client = require('azure-iot-device').Client;","symbol":"Client","correct":"import { Client } from 'azure-iot-device';"},{"note":"The `Message` class is fundamental for creating device-to-cloud messages, regardless of the transport chosen. It's imported from the core `azure-iot-device` package.","wrong":"const Message = require('azure-iot-device').Message;","symbol":"Message","correct":"import { Message } from 'azure-iot-device';"},{"note":"This `Http` class represents the specific HTTP transport implementation and is passed as an argument to `Client.fromConnectionString` or `Client.fromSharedAccessSignature` from `azure-iot-device`.","wrong":"const Http = require('azure-iot-device-http').Http;","symbol":"Http","correct":"import { Http } from 'azure-iot-device-http';"},{"note":"While this method is re-exported directly from `azure-iot-device-http` for backward compatibility, the recommended modern approach for better type safety and consistency is to import `Client` from `azure-iot-device` and use `Client.fromConnectionString(connectionString, Http)`.","wrong":"const clientFromConnectionString = require('azure-iot-device-http').clientFromConnectionString;","symbol":"clientFromConnectionString","correct":"import { clientFromConnectionString } from 'azure-iot-device-http';"}],"quickstart":{"code":"import { Client, Message } from 'azure-iot-device';\nimport { Http } from 'azure-iot-device-http';\n\n// Replace with your actual IoT Hub device connection string.\n// You can find this in the Azure portal under your IoT Hub -> Devices -> [Your Device] -> Connection string (primary key).\nconst connectionString: string = process.env.IOTHUB_DEVICE_CONNECTION_STRING || 'HostName=YOUR_HUB.azure-devices.net;DeviceId=YOUR_DEVICE_ID;SharedAccessKey=YOUR_KEY';\n\nif (connectionString.includes('YOUR_HUB')) {\n  console.warn('Please replace the placeholder connection string with your actual IoT Hub device connection string.');\n  process.exit(1);\n}\n\nconst client = Client.fromConnectionString(connectionString, Http);\n\nconst connectCallback = (err?: Error): void => {\n  if (err) {\n    console.error(`Could not connect: ${err.message}`);\n  } else {\n    console.log('Client connected');\n    const message = new Message('some data from my device via HTTP');\n    console.log('Sending message:', message.getData());\n    client.sendEvent(message, (sendErr?: Error) => {\n      if (sendErr) {\n        console.error(`Error sending message: ${sendErr.toString()}`);\n      } else {\n        console.log('Message sent successfully');\n      }\n    });\n\n    client.on('message', (msg: Message) => {\n      console.log('Received message from IoT Hub:', msg.getData().toString());\n      client.complete(msg, (completeErr?: Error) => {\n        if (completeErr) {\n          console.error(`Error completing message: ${completeErr.toString()}`);\n        } else {\n          console.log('Message completed');\n        }\n      });\n    });\n\n    client.on('error', (error: Error) => {\n        console.error(`Client error: ${error.message}`);\n    });\n\n    client.on('disconnect', (transportError: Error) => {\n        console.warn(`Client disconnected: ${transportError?.message || 'Unknown reason'}`);\n    });\n  }\n};\n\nclient.open(connectCallback);\n\n// Keep the process alive for a bit to allow messages to be received\nsetInterval(() => {}, 1000 * 60 * 60); // Keep alive for 1 hour or until process is killed","lang":"typescript","description":"Demonstrates how to connect an Azure IoT device using an HTTP connection string, send a device-to-cloud message, and handle incoming cloud-to-device messages. It uses the modern `Client.fromConnectionString` pattern with the `Http` transport."},"warnings":[{"fix":"Upgrade your Node.js runtime environment to version 14.0.0 or higher. The `engines` field in `package.json` specifies `node: \">= 14.0.0\"`.","message":"Node.js 12 reached end-of-life and is no longer supported. Azure IoT SDKs moved to Node.js 14 as the minimum requirement.","severity":"breaking","affected_versions":">=1.18.0"},{"fix":"For modern Node.js projects using ES Modules (e.g., `\"type\": \"module\"` in `package.json`), use `import { Client, Message } from 'azure-iot-device';` and `import { Http } from 'azure-iot-device-http';`. For CommonJS projects, use `const { Client, Message } = require('azure-iot-device');` and `const { Http } = require('azure-iot-device-http');`.","message":"The Azure IoT SDK for Node.js supports both CommonJS (`require`) and ES Modules (`import`). Using `require` in an ESM context or vice-versa can lead to runtime errors or incorrect symbol resolution.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your project's dependency resolution correctly handles `@azure/core-auth`. If you are directly using functionalities from `azure-iot-http-base`, consult its documentation for updated API surfaces or type definitions.","message":"The underlying `azure-iot-http-base` package changed its dependency from `@azure/core-http` to `@azure/core-auth` in a previous release. While `azure-iot-device-http` abstracts this, direct interactions with `azure-iot-http-base` or type conflicts could arise.","severity":"breaking","affected_versions":">=1.12.2"},{"fix":"Regularly update all `azure-iot-*` packages to their latest stable versions. Utilize tools like `npm audit` or `yarn audit` to identify and resolve known vulnerabilities.","message":"The Azure IoT SDKs, including `azure-iot-device-http`, are regularly updated to address dependency vulnerabilities. Running older versions can expose your application to known security risks.","severity":"gotcha","affected_versions":"<1.14.4 (any older version)"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure you are importing `Client` from `azure-iot-device` as a named import: `import { Client } from 'azure-iot-device';`. Then, pass the `Http` transport when creating the client: `const client = Client.fromConnectionString(connectionString, Http);`.","cause":"This error typically occurs when `Client` is not correctly imported as a class with static methods, or when attempting to call `Client.fromConnectionString` directly on the `azure-iot-device-http` package (which is a re-export, but less common in modern usage).","error":"TypeError: Client.fromConnectionString is not a function"},{"fix":"Verify the device connection string in the Azure portal for your specific device. Ensure it's copied correctly, includes `HostName`, `DeviceId`, and `SharedAccessKey` (or `SharedAccessSignature`), and that the device is enabled in IoT Hub.","cause":"The device connection string provided is invalid, expired, or does not correspond to an active device in your Azure IoT Hub, preventing successful authentication.","error":"Error: Could not connect: {\"error\":\"Unauthorized\"}"},{"fix":"Ensure event listeners are attached only once per `Client` instance. If clients are frequently re-initialized, consider reusing an existing client or explicitly removing old listeners with `client.removeListener(eventName, listenerFunction)` before attaching new ones. For rare cases where many listeners are genuinely needed, `client.setMaxListeners(N)` can suppress the warning, but it's often a symptom of a bug.","cause":"This warning indicates that too many event listeners (e.g., for `message`, `error`, `disconnect` events) are being attached to a `Client` instance, often due to creating new client instances or re-attaching listeners in a loop without proper cleanup.","error":"(node:xyz) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. N Listener added to [Client]. Use emitter.setMaxListeners() to increase limit"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}