Accessing Device Names, locations, and other device specific variables

Hi all, I’d like to understand how to access the name of each of my devices within my code.

As I explained in my other post, my project involves my Balena devices collecting data and publishing the data to amazon’s DynamoDB using IOT hub. With this, I’d like to be able to track which device published data by getting the device name and/or location.

Can anyone help me understand how to access these types of variables?

Thanks

Hello @Mattssmith94

did you try to request the device’s name to the balena supervisor? check here for more information → Interacting with the balena Supervisor - Balena Documentation

For the location, you will need to check to the balena API the custom_latitude and custom_longitude. Check here → Resources - Balena Documentation

Actually you also can get the device name from the latest API call.

Let us know if that works.

Hey @mpous,
I have not tried to request the device name via the supervisor.
I’m relatively new to this, so I’m having trouble understanding how the “curl” commands (which I believe must be done manually in an app container’s terminal?) translate into javascript so that I can access those variables and information.
What I’d like my app to do is write these variables into each transaction that it publishes, or even tags like the ones in this example response from balena documentation:
{
“status”: “success”,
“tags”: [
{
“id”: 188303,
“name”: “DeviceLocation”,
“value”: “warehouse #3
}
]
}

Can you help me to understand how to use these curl commands?

@Mattssmith94 You will need to use Fetch API to request device name and other information from the Supervisor as @mpous suggested. There are a lot of tutorial you can follow to do the GET request.

Alternatively, some information you can get from the environment variables (process.env.env_variable):

# env|grep DEVICE
BALENA_DEVICE_ARCH=amd64
BALENA_DEVICE_NAME_AT_INIT=XXXXXX
BALENA_DEVICE_UUID=XXXXXX
BALENA_DEVICE_TYPE=genericx86-64-ext
1 Like

@Mattssmith94 did you solve this? Did you try this?


curl -X GET \
"https://api.balena-cloud.com/v6/device?\$filter=uuid%20eq%20'<UUID>'" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <AUTH_TOKEN>" 

Thanks @Mikhail for helping here!

1 Like

Hey Mpous,
I haven’t solved this yet.
Can these calls be run programmatically so that my Javascript code can use these variables?

If so, I’m not understanding how. I’m pretty new to this, so a step-by-step explanation would be appreciated if you can offer one.

Thanks

Hi @mattssmith94,

As Marc mentioned initially, using the Supervisor API’s GET /v2/device/name endpoint [1] would be the best way to get the name of the device. You can certainly use the curl command pasted above to https://api.balena-cloud.com, however, it will consume an additional amount of bandwidth, whereas the Supervisor API command is entirely within the device so would both be faster and less resource intensive.

First, to access the Supervisor API, you should add the supervisor-api label to your docker-compose.yml file [2]. You can find an example of io.balena.features.supervisor-api in context at [3].

After you’ve configured your docker-compose with the Supervisor API label, as described at [2], you’ll have access to some additional container environment variables. The ones you’ll need are BALENA_SUPERVISOR_ADDRESS and BALENA_SUPERVISOR_API_KEY. In your Node.js process, these env vars are accessible under the process.env object.

With access to these variables, you can query the Supervisor API’s GET /v2/device/name REST endpoint using a variety of npm packages, such as node-fetch [4] or axios [5]. If you’re running Node version 17 or newer, node-fetch has been incorporated into native Node [6], so you won’t need to npm i node-fetch. I would recommend Fetch, as it’s seen wider adoption due to its incorporation into newer Node versions, and thus you should be able to find plenty of resources on using it.

For more info about how to query endpoints with Fetch or Axios, I’d recommend reading their docs, found at [4] and [5]. Always a valuable skill to cultivate!

Let us know if this help!

Regards,
Christina

[1] Interacting with the balena Supervisor - Balena Documentation
[2] docker-compose.yml fields - Balena Documentation
[3] inkycal/docker-compose.yml at f360da5065cffa2e16d24db79529d5579d972533 · balena-io-playground/inkycal · GitHub
[4] node-fetch - npm
[5] axios - npm
[6] The Fetch API is finally coming to Node.js - LogRocket Blog

Additionally, if you are running JavaScript in the browser and not in Node, the Fetch API is available in your window object. Here is a guide on using Fetch client-side: Using the Fetch API - Web APIs | MDN

Thank you for the updates here!

I’ll get back to you if I need further assistance tomorrow.

Best,
Matt