Initializing and Configuring the DMK
In this guide, you’ll learn how to initialize and configure the Device Management Kit (DMK) for your application. By the end, you’ll have a working DMK instance that’s ready to interface with hardware devices.
What you’ll learn
- How to set up the DMK in your project
- How to configure loggers and transports
- How to maintain the DMK instance as a singleton
Step 1: Install the required packages
First, you’ll need to install the Device Management Kit and related packages:
npm install @ledgerhq/device-management-kit @ledgerhq/device-transport-kit-web-hid
Step 2: Create the DMK configuration
Important: Make sure the DMK instance is properly initialized with at least one transport. The transport is what enables communication with the device. Without it, none of the data exchange commands in this example will work.
In this example, to initialize the DMK, we’ll create a new file called dmk.ts
:
import {
ConsoleLogger,
DeviceManagementKitBuilder,
} from "@ledgerhq/device-management-kit";
import { webHidTransportFactory } from "@ledgerhq/device-transport-kit-web-hid";
// Initialize the DMK with a console logger and WebHID transport
export const dmk = new DeviceManagementKitBuilder()
.addLogger(new ConsoleLogger())
.addTransport(webHidTransportFactory) // Transport is required!
.build();
This code sets up the DMK with:
- A console logger that will output information to your browser’s console
- The WebHID transport, which allows communication with hardware devices via the browser’s WebHID API
Step 3: Use the DMK instance
Import the DMK instance in any file where you need to interact with devices:
import { dmk } from "./dmk";
// Now you can use the DMK for device operations
async function connectToDevice() {
const devices = await dmk.listDevices();
console.log("Available devices:", devices);
}
Important: The DMK should be initialized only once in your application. Always import the same instance rather than creating multiple instances.
What you’ve learned
Congratulations! You’ve now:
- Set up a working DMK instance
- Configured it with a logger and transport
- Learned how to maintain it as a singleton
- Know how to import and use it in your application