Cannot add a new variable to a variable set using Rest API

I’m trying to add a new variable to a variable set in Octopus using the Rest API.
The first time, I ran:
curl -X PUT my-octo/api/{spaceid}/variables/${varID} -H X-Octopus-ApiKey:**** -H Content-Type:application/json -d '{"Name":"something", "Value":"something", "Type":"String", "IsSensitive" : false}'

I go a json back. Did not seem to be an error. However, when I look in the UI or when I using the API to GET my-octo/api/{spaceid}/variables/${varID} I do not see any variable.

Now, if I try to run the PUT request, I get the following error:
“Changes to these variables could not be saved, because another user has made changes to the variables between when you started editing and when you saved your changes. Please reload or open a new tab to make your changes.”

Any help would be appreciated

Hi @TestQA,

Thanks for reaching out on our support forums, and welcome to the community!

We have several scripts in our GitHub that might help you add new variable values.

You’ll want to review these options for the one that best fits your use case.

Alternatively, you can use the Swagger page for your Octopus instance to help structure your API call

I hope one of those options works out for you. Let me know if you have any additional questions.

Regards,
Brent

Hi Brent,

Thank you, I was able to make some progress. I was able to push a variable using the following json:

                "Version": 2
                "Variables":{"Name":"something", "Value":"something", "Type":"String", "IsSensitive" : false}

I was also able to add a variable using a Scope by using the following json:

                "Version": 2
                "Variables":{"Name":"something", "Value":"something", "Scope": { "Environment": [ "EnvId"]}, "Type":"String", "IsSensitive" : false}

Since I added the variable with a Scope, I cannot add anymore variables, I keep getting the error:
{ "ErrorMessage": "Object reference not set to an instance of an object." }

When I look at the audit on Octopus UI, it seems that it updated the variable “RelatedDocumentIds”: [] which I did not have in my script. It updated it with my environment ID.
Even if I run my PUT request with “RelatedDocumentIds”: [] it throws the Object error. Any help would be appreciated. Thank you

Hi @TestQA,

I’m not sure what exactly would be causing that error, but you might have better luck trying this script:

It should allow you to create new and update existing variables with or without scoping and succeed without getting that error again. I know it looks complicated, but there is a section towards the bottom to enter the desired values.

I hope that helps.

Regards,