OS env values how to set up to config.json

Hello to all!

I need to bring somehow values from config.json to env values inside device.
Something like add key=value during config.json creation and use this value in python application.
After device got booted to be able read it like:

os.getenv('API_VISO', "NO VALUE")

i’m using openbalena so dashboard is not accessible.
And I need env values to be ready on first boot , yes I know about cli command to add env value.

let me summarize question. How to add custom values to config.json and made this values accessible inside of devise container ???

Hi @mexekanez,

Would you mind describing a little more about what you’re trying to accomplish? I do not believe adding custom key value pairs is something that we support at the moment, as editing this file carries a lot of risk.

Looking forward to hearing what that is!

hi @xginn8
I would like to prepopulate image with some values before it get flashed. So when device will first time booted I want to send device info {data} to 3rd party service.

Hi @mexekanez ,

So, from your description it sounds like you want to be able to immediately be able to start using a service, would that be correct? You can actually preload an application into an image file before this is used to flash a device, which means the device doesn’t need to download the application image before starting the application.

This sounds like it would work for your usecase if all you need to do is contact a 3rd party service as soon as the device boots. If not, could you please list some steps that you’d like to carry out on boot for your application, as it’s still a little unclear to us. As Matthew mentioned, we don’t support the addition of application specific keyvalue pair reading from the config.json file.

If you think preloading sounds like it could help, there are details on how to carry this out here: https://www.balena.io/docs/reference/cli/#preload-image

Best regards,


@hedss Thanks

Idea was about the next…
We have 3rd party application named portal… each user has own workspace with own ID. Idea was to add this ID during image creation (generate on fly) . So when device will boot first time just post this ID with internal BALENA_ information to this web app.


OK, that makes sense! Then what you could do is pre-register a device before it’s actually activated, and configure an image with those device details. Because you now have a device image you can flash to a device with pre-known UUIDs, you could use the balenaCloud API endpoint to set named environment variables for that unique device UUID to set values. This means that on first bootup, the device would use the preset environment variable values for that device.

By doing this, for example, you could pre-register a device for a user, say ‘Bob User’, set the ‘USER_NAME’ environment variable via our API endpoint (or balena-cli, or the SDK) to ‘Bob User’ for that specific device UUID, flash the device image with the set device UUID onto a physical device then send that specific device to ‘Bob User’. When the device first boots, the UUID matches the pre-registered and pre-configured envvars, which can be used by your application (which will see a ‘USER_NAME’ of ‘Bob User’).

Hopefully that makes sense, and sounds like the kind of thing you want to do. Of course, it still relies on connecting to our backend to get the right value for the envvar, but it sounds like your devices will be Internet connected.

Please let us know if we can expand on this explanation if it’s not clear!

Best regards,


hi @hedss

your solution sounds like a trick but it isn’t so, for your solution we need to keep device or app where we can set env var and then download image and the same for each iteration. if we will have 2000 user which will works with images and different solution for them do you imagine what architecture do we need to support this. I can tell you that the “trick” also can be found using in docker.template by using ENV WORKSPACE_ID=7777 but the same issue -> keep a ton of docker files and created containers.

I think the case is to use sdk for associate create namespace with balena application

Sorry, I don’t think I really understand the use case then. Yes, you can pass an environment variable into a Dockerfile or docker-compose.yml but this won’t be traceable to a specific device.

I’m also not sure about your comment on the architecture you’d need to support this, if you have 2000 users why are they working with different images and solutions. Are you suggesting a different application for each user?

I think part of the issue here is we don’t actually have a clear set of steps you want to carry out or what the actual issue is. Could you please break the issue into a series of atomic steps please? At the moment we just know you use a third party service which will have a different ID for each user, we’re still not sure how you’re trying to solve your issue or exactly what it is. Having an atomic breakdown would make a huge difference.

Thanks and best regards,


Hi, @mexekanez!

Do I understand your case correctly: you have a multi-tenant portal with ~2000 tenants. Also, all of the many tenants can deploy different balenaOS based devices into their own environments by downloading base images from your portal.
And the problem is – how to allow the downloaded image automatically register for correct tenant?

From one of your comments, I assume that you already considered application-per-tenant approach and discarded that due to high count of applications (alghough, if docker images are identical, the apps should not have that big storage requirements).

Another option - the config.json supports hostname key, which would then be used as the hostname, and hostname is available throughout the system. You could set hostname according to some pattern, such as WRK7777-1234 and in your container, decode this hostname to workspace ID and random device ID which then can be used to register this device.

One point though: our situation is quite similar, only that we have small number of tenants, with large number of devices per tenant. One of the first requirements is how to identify the physical devices in the portal? We use the ENV['BALENA_DEVICE_NAME_AT_INIT'] when device registers itself to the portal, so that we can map to it in balena devices list. If you change the hostname from the default UUID, you probably still need a way to refer to this device so that you can issue commands to it.

Hope this helps.