Get variables for environment per API


is there a way to get the variables for a release and environment over the API?

I want to get the variables from the TEST environment and from the PRODUCTION environment and diff them.
This way I want to find variables that are expected to be set per environment, but have not been.


Hi Georg,

Thanks for getting in touch! There sure is, if you use the .NET Client library, you can get them via:

var project = repository.Projects.FindByName("My Project");
var set = repository.VariableSets.Get(project.Id);
var variables = set.Variables

You can also use Octoposh from powershell.

Using the raw api, first find the project in and find the Variables element under Links. That will give you the url of the variable set.

Alternatively open the dev tools in the browser and capture the request when you open the variables page in the web UI.

Hope that helps!

Robert W

But these are not per environment, are they?

Hi Georg,

I’m not sure what you mean “per environment”, could you expand on what you mean by that? The returned variables should contain the scope of each variable, so you can filter out those variables that apply to other environments.

Robert W

And how does one use the scope object? I have 0 idea what key/value pair i’m looking for and so far the ‘documentation’ is 0 help.

Hi Georg,

The Scope property contains an Environment property with an array of environments that a variable applies to. If the Environment property does not exist, it applies to all environments.

The following script will find the names of variables that are scoped to Test, and all the names of variables scoped to Prod. It then finds those in the test set, but not prod set. Also included is a list of all variables scoped to Prod or all environments that may fit what you require better.

var project = repository.Projects.FindByName("My Project");
var set = repository.VariableSets.Get(project.VariableSetId);

var testEnvironment = set.ScopeValues.Environments.First(e => e.Name == "Test").Id;
var prodEnvironment = set.ScopeValues.Environments.First(e => e.Name == "Prod").Id;
var testScopedVariables = set.Variables
        .Where(v => v.Scope.ContainsKey(ScopeField.Environment) && v.Scope[ScopeField.Environment].Any(e => e == testEnvironment))
        .Select(v => v.Name);
var prodScopedVariables = set.Variables
        .Where(v => v.Scope.ContainsKey(ScopeField.Environment) && v.Scope[ScopeField.Environment].Any(e => e == prodEnvironment))
        .Select(v => v.Name);

var prodAndUnscopedVariables = set.Variables
        .Where(v => !v.Scope.ContainsKey(ScopeField.Environment) || v.Scope[ScopeField.Environment].Any(e => e == prodEnvironment))
        .Select(v => v.Name);

var unset = testScopedVariables.Except(prodScopedVariables, StringComparer.InvariantCultureIgnoreCase);

Hope that helps

Robert W

1 Like

Thanks Robert,

I didn’t have time to wait for an answer so I found out most of what you sent via brute force. This would’ve saved me a good deal of time (not your fault on the timing). This isn’t the first time some api examples have been created for your forum questions that would be useful for others. Why aren’t y’all adding these to your documentation. A simple copy/paste of this to an ‘examples’ section would be a big help for all of us banging away at the api. I love octopus and the api – once you can find the right bits – has really helped us automate our environment creation/deployment; but the api is an absolute nightmare to navigate and much of it makes little sense – even after you find out how to use it.

Take care,

1 Like

Hi Greg,

Thank you for the feedback. We do have a API documentation and samples site that covers most of the API, but I do see that Variables are not properly documented. We are planning on creating better API documentation and discover-ability this year.

Robert W