Automated Variables using RestAPI on Octopus Version 3.3.18

I am unable to do the following:

$variable = (@{“Name” = “MyVariable”;
“Value” = “Value”;
“Scope” = “”;
“IsSensitive” = $false;
“Prompt” = “”} | ConvertTo-JSON)

$headers = @{ “X-Octopus-ApiKey” = $Key}
Invoke-RestMethod -Method Post -Uri "http://localhost/api/variables/variableset-Projects-2 -Headers $headers -ContentType “application/json” -Body $variable

#I’m getting 405 Method not allowed. Is there a better way to do this using the RestAPI?
#Also the VariableSet section of the RestAPI documentation is a little sparse with broken links. Can anyone point me in the right direction?

Hi,

Thanks for reaching out.

That API call doesn’t quite work like that. In a nutshell, what we POST to /api/variables is the entire variable set, and not just a single variable. So first you’ll have to GET the entire variable set from api/variables/[ID], then append the variable and finally post the entire set back.

There’s a script I wrote for Octoposh that might be able to give you a hand with this: https://github.com/Dalmirog/OctoPosh/wiki/Modifying-Variable-Sets

Please be super careful when using it. Test it first on a dummy project until you are sure you understand how it works.

Regarding our lack of documentation for the API, it is (sadly) very true, and its something we are aware of and that we’re gonna work on. Our plan is sometime after 3.4 to re-write the API in a different tecnology so we can take advantage of tools that can automate the doc generating process.

Best regards,
Dalmiro