Register end devices without web ui

Hi, i would like to register end devices without using the web ui, im using chirpstack with docker and the API REST in order to achieve that, but the documentation says that gRPC is recommended, but i dont see any examples and i dont understand why gRPC is preferred over API REST, there is my code btw, thanks

import requests

headers = {"accept": "application/json",
           "Grpc-Metadata-Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJjaGlycHN0YWNrIiwiaXNzIjoiY2hpcnBzdGFjayIsInN1YiI6ImUzY2IzN2QxLTU4YjYtNGU5Zi05NjY3LWE1NDQ4MzhiNDg5NiIsInR5cCI6ImtleSJ9.UVVh96Gao4nnkmfJHPDKNsrxmsTSadn_xJG3qhh3U14",
           "Content-Type": "application/json"}

def get_tenant() -> str:
    res = requests.get(
        "http://localhost:8090/api/tenants?limit=1", headers=headers)
    if res.status_code == 200:
        data = res.json()
        return data['result'][0]['id']
    return ""

TENANT_ID = get_tenant()
GATEWAY_EUI = "f0e910ffab2964e0"

def create_app() -> str:
    data = {"application": {
        "description": "test",
        "id": "test",
        "name": "app",
        "tenantId": TENANT_ID
    }}
    res = requests.post(
        "http://localhost:8090/api/applications", headers=headers, json=data)
    if res.status_code == 200:
        return res.json()['id']
    return ""


APP_ID = create_app()


def create_gateway():
    data = {
        "gateway": {
            "description": "",
            "gatewayId": GATEWAY_EUI,
            "location": {
                "accuracy": 0,
                "altitude": 0,
                "latitude": 0,
                "longitude": 0,
                "source": "UNKNOWN"
            },
            "name": "gateway",
            "properties": {
                "additionalProp1": "string",
                "additionalProp2": "string",
                "additionalProp3": "string"
            },
            "tags": {
                "additionalProp1": "string",
                "additionalProp2": "string",
                "additionalProp3": "string"
            },
            "tenantId": TENANT_ID
        }}
    res = requests.post("http://localhost:8090/api/gateways",
                        headers=headers, json=data)

create_gateway()

def create_device_profile() -> str:
    data = {
        "deviceProfile": {
            "classBTimeout": 0,
            "classCTimeout": 0,
            "description": "",
            "deviceStatusReqInterval": 1,
            "flushQueueOnActivate": True,
            "macVersion": "LORAWAN_1_0_3",
            "name": "profile",
            "payloadCodecRuntime": "CAYENNE_LPP",
            "regParamsRevision": "A",
            "region": "US915",
            "supportsClassB": False,
            "supportsClassC": False,
            "supportsOtaa": True,
            "tenantId": TENANT_ID,
            "uplinkInterval": 3600
        }
    }
    res = requests.post("http://localhost:8090/api/device-profiles",
                        headers=headers, json=data)
    if res.status_code == 200:
        return res.json()['id']
    return ""

DEVICE_PROFILE_ID = create_device_profile()

def register_device(deveui, name):
    data = {
        "device": {
            "applicationId": APP_ID,
            "description": "",
            "devEui": deveui,
            "deviceProfileId": DEVICE_PROFILE_ID,
            "isDisabled": False,
            "name": name,
            "skipFcntCheck": True,
        }
    }
    res = requests.post("http://localhost:8090/api/devices",
                        headers=headers, json=data)
    if res.status_code == 200:
        print("device registered successfully")
    else:
        print(res.json())

register_device("1114b10b0022f78a", "node1")
register_device("1114b10b0022f78b", "node2")
register_device("1114b10b0022f78c", "node3")
1 Like