# Unable to set environment variable

**URL:** <https://forums.balena.io/t/unable-to-set-environment-variable/280560>\
**Category:** Product support\
**Created:** [March 31, 2021, 1:47pm UTC](https://forums.balena.io/t/unable-to-set-environment-variable/280560 "2021-03-31T13:47:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thmarinnho](https://avatars.discourse-cdn.com/v4/letter/t/b2d939/32.png) [@thmarinnho](https://forums.balena.io/u/thmarinnho)\
**Post date:** [March 31, 2021, 1:47pm UTC](https://forums.balena.io/t/unable-to-set-environment-variable/280560/1 "2021-03-31T13:47:01Z")

</div>

Hello,  
I’m trying to set environment variable using your API and axios.  
Unfortunately, the code above isn’t working, just returning an array of all the variables already set manually.  
I am doing something wrong ?

```auto
  private client: AxiosInstance

  public constructor() {
    this.client = axios.create({
      baseURL: "http://api.balena-cloud.com",
      headers: {
        "Authorization": `Bearer ${process.env.BALENA_API_KEY}`,
        "Content-Type": "Application/json"
      }
    })
  }

  public async setEnvVar(id: string, key: string, value: string) {
    const res = await this.client.post('/v6/device_environment_variable', {
      device: id,
      name: key,
      value
    })
    return res.data.d
  }
}

```

---

<div class="post-metadata">

**Author:** ![anujdeshpande](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/anujdeshpande/32/3155_2.png) [@anujdeshpande](https://forums.balena.io/u/anujdeshpande)\
**Post date:** [April 1, 2021, 5:55am UTC](https://forums.balena.io/t/unable-to-set-environment-variable/280560/4 "2021-04-01T05:55:10Z")

</div>

Hi - do you see this same behavior when you try setting the env using something like curl?

---

<div class="post-metadata">

**Author:** ![thmarinnho](https://avatars.discourse-cdn.com/v4/letter/t/b2d939/32.png) [@thmarinnho](https://forums.balena.io/u/thmarinnho)\
**Post date:** [April 1, 2021, 7:56am UTC](https://forums.balena.io/t/unable-to-set-environment-variable/280560/5 "2021-04-01T07:56:16Z")

</div>

Hey, thanks for the quick reply! Trying this using curl works fine

---

<div class="post-metadata">

**Author:** ![JSReds](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jsreds/32/2929_2.png) [@JSReds](https://forums.balena.io/u/JSReds)\
**Post date:** [April 1, 2021, 4:39pm UTC](https://forums.balena.io/t/unable-to-set-environment-variable/280560/6 "2021-04-01T16:39:19Z")

</div>

Hi there, looking at your code I can see that the request you are making is an http request which should instead use https.  
Also I can see that the first argument of your `setEnvVar` is of type string which is not correct, for this request you need to pass the device id which is of type number.  
I just tried it using the browser console and like this it works fine:

1. open [https://dashboard.balena-cloud.com/](https://dashboard.balena-cloud.com/)
2. open the browser console ( windows: ctrl + shift + i; macOS: cmd + option + i; linux: ctrl + alt + t )
3. change the pieces of code I put between \<\>, of the code below, and paste it into the browser console

```auto
  var axios = document.createElement('script');
  axios.src = "https://unpkg.com/axios/dist/axios.min.js";
  document.getElementsByTagName('head')[0].appendChild(axios);

  var client = axios.create({
    baseURL: "https://api.balena-cloud.com",
    headers: {
      "Authorization": `Bearer <BEARER_TOKEN>`,
      "Content-Type": "application/json"
    }
  })

  const res = await this.client.post('/v6/device_environment_variable', {
    device: <DEVICE_ID>',
    name: 'test',
    value: 'valuetest'
  })

```

BEARER\_TOKEN = can get it by running `await sdk.auth.getToken()` from your browser console and copy the result.  
DEVICE\_ID = this is not the UUID, to get the id you can run `await sdk.models.device.get(<DEVICE_UUID>)`  
DEVICE\_UUID = from the dashboard, enter into a device summary and you can copy it from the summary card or from the url.  
device summry page = login into you dashboard, click on the application, click on the device

---

<div class="post-metadata">

**Author:** ![thmarinnho](https://avatars.discourse-cdn.com/v4/letter/t/b2d939/32.png) [@thmarinnho](https://forums.balena.io/u/thmarinnho)\
**Post date:** [April 1, 2021, 5:10pm UTC](https://forums.balena.io/t/unable-to-set-environment-variable/280560/8 "2021-04-01T17:10:35Z")

</div>

Hello,  
Thank you very much for your answer, the problem was actually due to the fact that my request was made in http and not in https
