# Set Service Variable from device container

**URL:** <https://forums.balena.io/t/set-service-variable-from-device-container/283955>\
**Category:** balenaOS\
**Tags:** docker\
**Created:** [April 6, 2021, 10:20pm UTC](https://forums.balena.io/t/set-service-variable-from-device-container/283955 "2021-04-06T22:20:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JasonLiu](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@JasonLiu](https://forums.balena.io/u/JasonLiu)\
**Post date:** [April 6, 2021, 10:20pm UTC](https://forums.balena.io/t/set-service-variable-from-device-container/283955/1 "2021-04-06T22:20:34Z")

</div>

My question is similar to this:

> [@Set device variable from device container](https://forums.balena.io/t/set-device-variable-from-device-container/47210):
>
> 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

but he is trying to set the device variable, while I’m trying to set the device service variable in python.  
I tried using

```auto
from balena import Balena
import os
balena = Balena()
auth_token = os.environ['BALENA_API_KEY']
app_id = os.environ['BALENA_APP_ID']
balena.auth.login_with_token(auth_token)
balena.models.environment_variables.service_environment_variable.create(app_id, 'main', 'TEST', 'test value')

```

to do that in the device container, but I got an error complains it’s unauthrized. Could you please tell me what’s wrong? Do I need to give it the account credential in order to do that? The API doc didn’t give an example: [Balena Python SDK - Balena Documentation](https://www.balena.io/docs/reference/sdk/python-sdk/#serviceenvvariable).

Thank you.

---

<div class="post-metadata">

**Author:** ![srlowe](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/srlowe/32/2638_2.png) [@srlowe](https://forums.balena.io/u/srlowe)\
**Post date:** [April 7, 2021, 9:37am UTC](https://forums.balena.io/t/set-service-variable-from-device-container/283955/3 "2021-04-07T09:37:29Z")

</div>

Hello, That should be possible as you have described. Have you verified that the env var `BALENA_API_KEY` is set as expected? This is not available by default - you need to add the `io.balena.features.balena-api` label in docker-compose. See : [Multiple containers - Balena Documentation](https://www.balena.io/docs/learn/develop/multicontainer/#labels)

---

<div class="post-metadata">

**Author:** ![JasonLiu](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@JasonLiu](https://forums.balena.io/u/JasonLiu)\
**Post date:** [April 7, 2021, 3:46pm UTC](https://forums.balena.io/t/set-service-variable-from-device-container/283955/5 "2021-04-07T15:46:48Z")

</div>

Hi, thank you for replying. I believe I have the `BALENA_API_KEY` defined as I can update/create the device variable with the example in the first link. But I can not set the service environment variable.

---

<div class="post-metadata">

**Author:** ![pipex](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pipex/32/4840_2.png) [@pipex](https://forums.balena.io/u/pipex)\
**Post date:** [April 7, 2021, 7:52pm UTC](https://forums.balena.io/t/set-service-variable-from-device-container/283955/6 "2021-04-07T19:52:45Z")

</div>

Hi @jasonliu, I believe what is going on is that the `service_environment_variable`method creates variables for the whole application while the `BALENA_API_KEY` is scoped for a single device.

If you just want to create a service variable for a single device you can use the [device\_service\_environment\_variable](https://www.balena.io/docs/reference/sdk/python-sdk/#deviceserviceenvvariable) method.

Otherwise if you want to create a service variable for the whole application you can create a new API\_KEY (under Preferences \> Access Tokens) and pass that key as a variable to the device to allow it to change settings at the fleet level.

Could you tell us a bit more about your use case? What are you trying to achieve with service variables?

Thank you!

---

<div class="post-metadata">

**Author:** ![JasonLiu](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@JasonLiu](https://forums.balena.io/u/JasonLiu)\
**Post date:** [April 7, 2021, 10:08pm UTC](https://forums.balena.io/t/set-service-variable-from-device-container/283955/9 "2021-04-07T22:08:50Z")

</div>

Thank you, I will try that out later.

Basically we (our company) is trying to run a program in balena container on different RPIs and that program will have a socket connection to a server.  
We would like to have someone, who doesn’t have the balena cloud access, to change some of the global environment variables for devices through the socket.

This is our use case. Let me know if it make sense.

---

<div class="post-metadata">

**Author:** ![pipex](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pipex/32/4840_2.png) [@pipex](https://forums.balena.io/u/pipex)\
**Post date:** [April 7, 2021, 10:46pm UTC](https://forums.balena.io/t/set-service-variable-from-device-container/283955/10 "2021-04-07T22:46:46Z")

</div>

Hi again,

Thanks for the further context. Yes, what you are saying makes sense.

Beware though than when creating an API key as I explained before, that API key has the same level of the permissions as the key owner, so you should be careful to limit the permissions of that user to a single application and be sure that the third party cannot get access to that key otherwise they will be able to modify more than environment variables.

You can see more documentation about permissions here [Organization management - Balena Documentation](https://www.balena.io/docs/learn/manage/organizations/#granting-access-to-organization-applications)

---

<div class="post-metadata">

**Author:** ![JasonLiu](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@JasonLiu](https://forums.balena.io/u/JasonLiu)\
**Post date:** [April 7, 2021, 11:05pm UTC](https://forums.balena.io/t/set-service-variable-from-device-container/283955/13 "2021-04-07T23:05:42Z")

</div>

Hi Pipex,

Thank you for pointing out the permission issue and for the provided reference.

Best,  
Jason
