Unable to set environment variable

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 ?

  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
  }
}

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

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

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/
  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
  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

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