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?
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):
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!
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