Balena API: create/update a device environment variable in one request

Hi,
Our node sdk already offers a models.device.envVar.set() method which look similar to what you are trying to achieve:

On the other hand I realize that you are probably more interested at using the OData API directly on your PHP project.
You can achieve this by using a PUT request where you use the composite natural key of the resource as id of the request.

curl $'https://api.balena-cloud.com/v6/application_environment_variable(application=<app_id>,name=\'testname\')' \
  -X 'PUT' \
  -H 'authorization: Bearer <TOKEN>' \
  --data-raw '{"value":"test value"}'

Be aware though that updates of records with this approach, currently work like a DELETE and then re-INSERT, which means that you will be getting a fresh date on the created_at field whenever you so a PUT like the above. This might change it the future though to work like an POST/PATCH, but that shouldn’t be a breaking change probably.

The OData specification refers to this using the “Alternate Key” term. Let me also point you to that part of the specification:
https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#_Toc31360936

Kind regards,
Thodoris

1 Like