Adding device using API on js

const grpc = require("@grpc/grpc-js");

const { DeviceServiceClient} = require("@chirpstack/chirpstack-api/api/device_grpc_pb");
const {CreateDeviceRequest, CreateDeviceKeysRequest ,DeviceQueueItem } = require("@chirpstack/chirpstack-api/api/device_pb");
const {Device , DeviceKeys} = require("@chirpstack/chirpstack-api/api/device_pb");


async function addingDevice(server, apiToken, deviceList, applicationId, deviceProfileId) {
    console.log ("SERVER :",server);
        server= "http://"+server ;
        // console.log ("SERVER :",typeof(server));

    try {
        const channel = new DeviceServiceClient(server, grpc.credentials.createInsecure());
        // console.log("Channel:", channel);
        const metadata = new grpc.Metadata();
        metadata.set("authorization", "Bearer " + apiToken);

        // const client = new DeviceServiceClient(channel);

        for (const deviceData of deviceList) {
            try {
                const req = new CreateDeviceRequest();
                const device = new Device();

                // Use the setter methods provided by the Device class to populate the device object
                device.setDevEui(deviceData.devEUI);
                device.setName(deviceData.name);
                device.setApplicationId(applicationId);
                device.setDeviceProfileId(deviceProfileId);
                device.setDescription("Registering device via API");
                device.setIsDisabled(false);

                req.setDevice(device); // Set the device in the request

                const resp = await channel.create(req, metadata);
                console.log("Device added:", resp.toObject());

                const keysReq = new CreateDeviceKeysRequest();
                const keys = new DeviceKeys();
                keys.setDevEui(device.devEUI);
                keys.setAppKey(device.appkey);

                keysReq.setDeviceKeys(keys);

                const keysResp = await channel.createKeys(keysReq, metadata);
                console.log("Device key added:", keysResp.toObject());
            } catch (e) {
                console.log("Error at adding device:", e);
                throw e;
            }
        }

        return true; // Indicates successful device additions after all devices are processed
    } catch (e) {
        console.log("Error:", e);
        throw e;
    }
}

module.exports = {
    addingDevice: addingDevice
};

Im trying to add device using the node @chirpstack/chirpstack-api, but i got

Error at adding device: Error: Incorrect arguments passed

Anything that i might miss? Thanks in advance!

Did you try creating the device without adding the keys?

It seems setNwkKey (use AppKey to set the field) is mandatory, at least with Python. I’ve stopped with JS as it seems not possible to add tags.

async function addingDevice(server,apiToken,deviceList,applicationId,deviceProfileId){

    //check if device is not an array
    if (!Array.isArray(deviceList)) {
        deviceList = [deviceList];
    }

    for (const deviceData of deviceList) {
        try {
            //setting up the device
            const req = new CreateDeviceRequest();
            const device = new Device();

            device.setDevEui(deviceData.devEUI);
            device.setName(deviceData.name);
            device.setApplicationId(applicationId);
            device.setDeviceProfileId(deviceProfileId);
            device.setDescription("Registering device via API");
            device.setIsDisabled(false);
            device.setSkipFcntCheck(false);
            device.setIsDisabled(false);
            req.setDevice(device);

            //setting up the device key
            const keysReq = new CreateDeviceKeysRequest();
            const keys = new DeviceKeys();

            keys.setDevEui(deviceData.devEUI);
            keys.setNwkKey(deviceData.appKey);
            keys.setAppKey(deviceData.appKey);
            keysReq.setDeviceKeys(keys);

            //adding the device and the device key
            const channel = await new DeviceServiceClient(server, grpc.credentials.createInsecure());
            const metadata = await new grpc.Metadata();
            await metadata.set("authorization", "Bearer " + apiToken);
            await channel.create(req, metadata, (err, resp) => {
                if (err !== null) {
                    console.log("Create err:", err);
                    return;
                } else {
                    console.log("Create Device success: " + deviceData.name);
                }
                channel.createKeys(keysReq, metadata, (err, resp) => {
                    if (err !== null) {
                        console.log("CreateKeys err:", err);
                        return;
                    } else {
                        console.log(
                            "Create DeviceKey success: " + deviceData.name
                        );
                    }
                });
            });
        } catch (e) {
            console.log("Error at adding device key", e);
            return response.status(500).send(`An error occurred: ${e}`);
        }
    }
    return true;
}

Yes, I forgot the nwk key and how there is still an error occurring. But somehow the code up here works perfectly. I hope this piece of code can help anyone who’s going through this.