Python API - Create DeviceKeys issue

Hello ChirpStack Community,

I’m trying to create a device (and provision its keys) from my Python application. I am successfully able to use the API to create a device (as well as perform various other tasks like creating a device profile, creating a multicast group, etc.).

However, when it comes to creating and adding the DeviceKeys, I’m encountering an error message. I’ve seen several posts with the same issue, usually related to a missing field. However, in my case, I can’t seem to identify which field is missing.

Here is my code (In fact i’m not creating a device from scratch, i copy an existing device):

        server = self.api_server + ":8080"
        channel = self.__get_channel(server)
        client = api.DeviceServiceStub(channel)
        auth_token = [("authorization", "Bearer %s" % user_conftest.chirpstack_api_key)]
        request = api.GetDeviceRequest()
        request.dev_eui = eui
        resp= client.Get(request, metadata=auth_token)

        device = resp.device
        device.dev_eui = dev_eui
        device.join_eui = join_eui
        device.device_profile_id = device_profile_id
        request = api.CreateDeviceRequest()
        resp = client.Create(request, metadata=auth_token)

        device_keys = api.DeviceKeys()
        device_keys.dev_eui = dev_eui
        device_keys.app_key = nwk_key
        device_keys.nwk_key =nwk_key
        request2 = api.CreateDeviceKeysRequest()
        resp2= client.Create(request2, metadata=auth_token)

Running this results in:

>           raise _InactiveRpcError(state)
E           grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
E               status = StatusCode.INVALID_ARGUMENT
E               details = "invalid length: expected length 32 for simple format, found 0"
E               debug_error_string = "{"created":"@1706796136.953902249","description":"Error received from peer ipv4:","file":"src/core/lib/surface/","file_line":903,"grpc_message":"invalid length: expected length 32 for simple format, found 0","grpc_status":3}"
E           >

../.local/lib/python3.10/site-packages/grpc/ _InactiveRpcError

I would like to clarify that the device has indeed been successfully created.
As I mentioned, other topics mentioning this error were resolved because there was a missing field. However, after looking at the protofile, I can’t see what information is missing, and I’m sure that dev_eui , nwk_key , and app_key are the correct size (16 and 32 characters respectively, which translates to 8 and 16 bytes).

If anyone in the community has any idea, I would greatly appreciate your assistance in identifying the root cause of this error and any potential solutions or suggestions to resolve it.

Thank you in advance for your help, and please let me know if you need any additional information or code snippets to assist with troubleshooting.

I don’t fully understand why you’re doing api.DeviceKeys() and then copying them, and I don’t feel like getting into the nitty gritty of your code, but I figured I should share my python code for creating device keys and hopefully you can figure something out from that:

def activateDevice(client,api_token,dev_eui,app_key):
  print("Activating device with EUI: " + dev_eui)

  # Define the API key meta-data.
  auth_token = [("authorization", "Bearer %s" % api_token)]

    req = api.CreateDeviceKeysRequest()
    req.device_keys.dev_eui = dev_eui
    req.device_keys.nwk_key = app_key
    req.device_keys.app_key = app_key

    # Send request
    client.CreateKeys(req, metadata=auth_token)

  except grpc.RpcError as e:
        print('Ensure API token is proper, devEUI is correct, app_key is 32 characters')

Works great for me.

Looking at your code again, I’m guessing its because you’re using client.Create() and not client.CreateKeys()

Thanks a lot !
You solved my issue, I should have called CreateKeys and not Create

Thanks again !