Unable to update variables via REST API

I am unable to update variables with POSTMAN via Rest API. Following are the details :

  • HTTP Request :- POST
  • URL :- {octopus_url}/api/Spaces-1/variables/variableset-LibraryVariableSets-661
  • Body :-
    {“Value”:“World”,“Name”:“Hello”}
  • Error :-

Hi @bidhanbst,

Thanks for getting in touch!

When updating variables via the API you need to retrieve the entire variable set object, add or edit the relevant parts and then use a PUT to upload the entire object.

We have an example Powershell script for modifying values within a variable set that might be useful:

Regards,
Paul

The script successfully ran but there is no any update on the variableset.
Can you please provide required json structure to update variables on project and link for POST request.

You can find the required JSON for all API endpoints within our API documentation.

Everything you do within the Octopus web portal uses the REST API. You can also use your browser dev tools (F12) to capture the API call and JSON body used when performing the relevant change to a library variable set.

The script I provided is intended for modifying existing variables. If the variable doesn’t exist, you must modify the script to create the variable instead.

I have tried to use swagger UI, but there is no body section on PUT resources on Variables section.

Are you running an older version of Octopus perhaps?

The full body appears for me:

{
  "Id": "string",
  "LastModifiedBy": "string",
  "LastModifiedOn": "2023-04-04T10:07:46.783Z",
  "Links": {
    "additionalProp1": "string",
    "additionalProp2": "string",
    "additionalProp3": "string"
  },
  "OwnerId": "string",
  "ScopeValues": {
    "Actions": [
      {
        "Id": "string",
        "Name": "string"
      }
    ],
    "Channels": [
      {
        "Id": "string",
        "Name": "string"
      }
    ],
    "Environments": [
      {
        "Id": "string",
        "Name": "string"
      }
    ],
    "Machines": [
      {
        "Id": "string",
        "Name": "string"
      }
    ],
    "Processes": [
      {
        "Id": "string",
        "Name": "string",
        "ProcessType": "Deployment"
      }
    ],
    "Roles": [
      {
        "Id": "string",
        "Name": "string"
      }
    ],
    "TenantTags": [
      {
        "Id": "string",
        "Name": "string"
      }
    ]
  },
  "SpaceId": "string",
  "Variables": [
    {
      "Description": "string",
      "Id": "string",
      "IsEditable": true,
      "IsSensitive": true,
      "Name": "string",
      "Prompt": {
        "Description": "string",
        "DisplaySettings": {
          "additionalProp1": "string",
          "additionalProp2": "string",
          "additionalProp3": "string"
        },
        "Label": "string",
        "Required": true
      },
      "Scope": {
        "Project": [
          "string"
        ],
        "Environment": [
          "string"
        ],
        "Machine": [
          "string"
        ],
        "Role": [
          "string"
        ],
        "TargetRole": [
          "string"
        ],
        "Action": [
          "string"
        ],
        "User": [
          "string"
        ],
        "Trigger": [
          "string"
        ],
        "ParentDeployment": [
          "string"
        ],
        "Private": [
          "string"
        ],
        "Channel": [
          "string"
        ],
        "TenantTag": [
          "string"
        ],
        "Tenant": [
          "string"
        ],
        "ProcessOwner": [
          "string"
        ]
      },
      "Type": "string",
      "Value": "string"
    }
  ],
  "Version": 0
}

This is just an example body; not all of those values will be required. Using the browser dev tools to examine the body used when performing the relevant actions within the web portal will give a better idea of the JSON structure you need.

I have executed a PUT request with the above json field with POSTMAN. The following error occurs :-

As I initially mentioned, you will need to retrieve the entire variable set with a GET, make the required changes, and perform the PUT request. This is also shown within the sample script I provided and within your browser, if you use dev tools to capture the requests made when editing the variable set.

e.g. in the sample script we GET the variable set

$LibraryVariableSetVariables = (Invoke-RestMethod -Method Get -Uri "$OctopusURL/api/$($Space.Id)/variables/$($LibraryVariableSet.VariableSetId)" -Headers $Header) 

Modify the variable set

for($i=0; $i -lt $LibraryVariableSetVariables.Variables.Length; $i++) {
    $existingVariable = $LibraryVariableSetVariables.Variables[$i];
    if($existingVariable.Name -eq $VariableName) {
        Write-Host "Found existing variable, updating its value"
        $existingVariable.Value = $VariableValue
    }
}

$existingVariable = $LibraryVariableSetVariables.Variables  | Where-Object {$_.name -eq $VariableName} | Select-Object -First 1 

Then perform the PUT

$UpdatedLibraryVariableSet = Invoke-RestMethod -Method Put -Uri "$OctopusURL/api/$($Space.Id)/variables/$($LibraryVariableSetVariables.Id)" -Headers $Header -Body ($LibraryVariableSetVariables | ConvertTo-Json -Depth 10)

If you’re having problems getting this to work in Postman I would suggest attempting this in Powershell as we have a number of sample scripts that will help you understand the necessary steps for working with the API.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.