Set device variable from device container

I want to create or modify a device variable from a Python application running in a device container. My assumption is I have to use the API here, right?

Is there a way for making API calls via Python SDK without providing my personal user/password?

Thanks much.
Bruno

Hi there,
The device exposes a scoped API token as BALENA_API_TOKEN that you can use to update values of the device itself, in this case a device environment variable.
For example, you could set the device environment variable foo=bar with the following code:

from balena import Balena
import os
balena = Balena()
auth_token = os.environ['BALENA_API_KEY']
device_uuid = os.environ['BALENA_DEVICE_UUID']
balena.auth.login_with_token(auth_token)

balena.models.environment_variables.device.create(device_uuid, 'foo', 'bar')

To gain access to the BALENA_API_KEY variable in a multi-container application you will need to ensure that you are using the io.balena.features.balena-api label in your docker-compose.yml as described here https://www.balena.io/docs/learn/develop/multicontainer/#labels

Excellent support. Exactly what I needed.
Cheers
Bruno