Resolution
Avoid writing tenant variables through the legacy /tenants/{tenantId}/variables endpoint and switch to the scoped endpoints, /tenants/{tenantId}/projectvariables and /tenants/{tenantId}/commonvariables. These endpoints can represent unscoped and scoped values. If you use a client library rather than raw HTTP, upgrade it to a version that calls the scoped endpoints. If the Go client is being used, ensure it's version 2.64.4 or later.
When you move to the newer scoped endpoints in Octopus Server 2025.2.11370 or later, fetch the current variables, give every entry a valid Scope object (an empty EnvironmentIds array means unscoped), and remove duplicate or overlapping entries. To set a value for a template that has only a default value, add a new entry for it. Then send the complete set back in one request.
Symptom
A script, integration, or older client fails when it saves tenant variables through the Octopus REST API. The request returns a 400 Bad Request with this error:
There are existing project variables that have no scope. The API you are using to modify them does not support unscoped project variables, so the request has been denied to keep the existing variables intact. Use an API that supports unscoped project variables.
When the tenant has common template values scoped to environments, the same request returns a related error instead:
There are existing common variables that have scopes. The API you are using to modify them does not support scoped common variables, so the request has been denied to keep the existing variables intact. Use an API that supports scoped common variables.
Other things you might notice:
- The same script works for some tenants and fails for others. It fails only for tenants that hold unscoped project values or scoped common values.
- The script worked before, then started failing after someone edited the tenant's variables in the Web Portal, or after an Octopus Cloud update. No change to the script was needed to trigger it.
- Scoping the variable your script updates doesn't clear the error. Every other value on the tenant is checked too.
- You can't find the scoped endpoints in your instance's Swagger UI. Swagger coverage for these endpoints may not align exactly with the available endpoints (Octopus Server versions 2025.2.11370 to early 2025.3), but they should still be available even if they don't show in Swagger UI.
Deployments and the tenant Variables page in the Octopus Web Portal keep working. Only the API write fails.
Scope: when this is the right article
This article applies when all of these are true:
- You're on Octopus Cloud, or a self-hosted Octopus Server on version 2025.2.11370 or later, where scoped tenant variables are available.
- The error comes from an API call, Octopus CLI command, client library, or script that saves tenant variables. It doesn't come from a deployment task log.
- The caller uses the legacy
/tenants/{tenantId}/variablesendpoint, or an older client library or tool that calls it. This includes the Go client (go-octopusdeploy) before version 2.64.4, and custom scripts written before scoped tenant variables were released. - The tenant has project template values that aren't scoped to any environment, project templates with only a default value, or common template values that are scoped to environments.
This isn't the right article if:
- A deployment fails with a missing tenant variable error.
- The error mentions an environment the tenant isn't connected to, or a permissions problem.
- You're on a self-hosted version earlier than 2025.2.11370.
Cause
The legacy /tenants/{tenantId}/variables endpoint was built before tenant variables could be scoped to environments. It can't represent two states:
- A project template value with no environment scope, including a template that has only its default value.
- A common template value that's scoped to one or more environments.
If the tenant has either, Octopus rejects the write rather than delete those values. This is by design. The check covers every value on the tenant, not only the one you're changing, so the error appears even when your request updates a different variable. Editing variables in the Web Portal often creates these states, which is why a working script can suddenly start failing for one tenant.
The scoped endpoints, /projectvariables and /commonvariables, support every state. They do have their own rules:
| State for one template | Valid on the scoped endpoints? |
|---|---|
One unscoped value (EnvironmentIds is empty) |
Yes, at most one per template |
| Scoped values, each with a different set of environments | Yes |
| An unscoped value plus scoped values | Yes. Scoped values win for their environments. |
| A default value only, with no tenant value | Yes. Add an entry to set an explicit value. |
| The same environment in two values | No. An environment can be in only one scope per template. |
| An environment the tenant isn't connected to | No |
Both scoped endpoints also replace the tenant's whole set of values with what you send. Any value you leave out of the request is deleted.
Diagnosis and fix
These steps use PowerShell. The scripts in step 3 use the Octopus .NET client (Octopus.Server.Client.dll), which you can get from the Octopus.Server.Client NuGet package. The API key needs permission to view and edit the tenant.
-
Check how your integration writes tenant variables.
-
Custom script or raw HTTP calls: search it for
/tenants/. If the path after the tenant ID is/variables, the script uses the legacy endpoint. Replace those writes with one of the scripts in step 3, or adapt your script to match them. -
Go client (
go-octopusdeploy): rungo list -m github.com/OctopusDeploy/go-octopusdeploy/v2. Versions earlier than 2.64.4 use the legacy endpoint. Upgrade withgo get github.com/OctopusDeploy/go-octopusdeploy/v2@latest, then use the tenant project variable and tenant common variable services. - .NET client, TypeScript client, or Octopus CLI: upgrade to the latest version, then use the tenant variable requests and commands shown in step 3.
If you only needed to upgrade a client, run your integration again. If it still fails, continue with step 2.
- Inspect the tenant's current variables. The following PowerShell script lists what's on the tenant and flags any problems the scoped endpoints would reject. It only reads data, so it's safe to run at any time:
$OctopusUrl = "<Your server URL>"
$ApiKey = "<Your API key>"
$SpaceId = "Spaces-1"
$TenantId = "Tenants-1"
$headers = @{ "X-Octopus-ApiKey" = $ApiKey }
$base = "$OctopusUrl/api/$SpaceId/tenants/$TenantId"
$project = Invoke-RestMethod -Headers $headers -Uri "$base/projectvariables?includeMissingVariables=true"
$common = Invoke-RestMethod -Headers $headers -Uri "$base/commonvariables?includeMissingVariables=true"
$tenant = Invoke-RestMethod -Headers $headers -Uri $base
"--- Project template values ---"
$project.Variables | Select-Object Id, ProjectName,
@{ n = "Template"; e = { $_.Template.Name } },
@{ n = "Scope"; e = { if ($_.Scope.EnvironmentIds) { $_.Scope.EnvironmentIds -join ", " } else { "Unscoped" } } } |
Format-Table -AutoSize
"--- Project templates with only a default value ---"
$project.MissingVariables | Select-Object ProjectId, ProjectName,
@{ n = "Template"; e = { $_.Template.Name } } | Format-Table -AutoSize
"--- Common template values ---"
$common.Variables | Select-Object Id, LibraryVariableSetName,
@{ n = "Template"; e = { $_.Template.Name } },
@{ n = "Scope"; e = { if ($_.Scope.EnvironmentIds) { $_.Scope.EnvironmentIds -join ", " } else { "Unscoped" } } } |
Format-Table -AutoSize
"--- Problems ---"
foreach ($set in @(
@{ Vars = $project.Variables; Owner = "ProjectId" },
@{ Vars = $common.Variables; Owner = "LibraryVariableSetId" })) {
$set.Vars | Group-Object { "$($_.($set.Owner))/$($_.TemplateId)" } | ForEach-Object {
$unscoped = @($_.Group | Where-Object { -not $_.Scope.EnvironmentIds })
if ($unscoped.Count -gt 1) { "More than one unscoped value for $($_.Name)" }
$dupes = $_.Group.Scope.EnvironmentIds | Group-Object | Where-Object Count -gt 1
foreach ($d in $dupes) { "Environment $($d.Name) is in more than one scope for $($_.Name)" }
}
}
foreach ($v in $project.Variables) {
$connected = @($tenant.ProjectEnvironments.($v.ProjectId))
foreach ($e in $v.Scope.EnvironmentIds) {
if ($connected -notcontains $e) { "$($v.Id) is scoped to $e, which isn't connected to the tenant for $($v.ProjectName)" }
}
}
Unscoped project values, default-only templates, and scoped common values are what block the legacy endpoint. They're valid on the scoped endpoints, so you don't need to change them. Fix anything listed under Problems in the Web Portal before you continue: go to Tenants, select the tenant, open Variables, and adjust the Scope column.
The scoped endpoints might not appear in your instance's Swagger UI (Octopus Server versions 2025.2.11370 to early 2025.3), but they should be available even if Swagger doesn't list them.
- Update the value with the scoped endpoints. Use the script that matches the kind of variable you're setting. Each script reads every existing value on the tenant, changes only the one that matches your template and scope, and sends the full set back so nothing is deleted. If no value matches, which includes templates with only a default value, the script adds a new one.
Set -EnvironmentIds to the exact scope you want. Leave it empty (@()) for an unscoped value. To update an existing value, use the same environments shown for it in step 2. To add a new scoped value, use only environments that aren't already in another scope for that template, or the request fails.
Project template value:
param(
[string]$OctopusUrl = "<Your server URL>",
[string]$ApiKey = "<Your API key>",
[string]$TenantName = "tenant 1",
[string]$ProjectName = "variable examples",
[string]$VariableName = "project template",
[string]$VariableValue = "test value",
[string[]]$EnvironmentIds = @("Environments-3") # Empty for unscoped
)
Add-Type -Path "<Path to Client dll>\Octopus.Server.Client.dll"
# Function to compare environment scopes
function Test-ScopeMatch($scope1, $scope2) {
$env1 = @($scope1.EnvironmentIds | Sort-Object)
$env2 = @($scope2.EnvironmentIds | Sort-Object)
if ($env1.Count -ne $env2.Count) { return $false }
for ($i = 0; $i -lt $env1.Count; $i++) {
if ($env1[$i] -ne $env2[$i]) { return $false }
}
return $true
}
try {
$repo = [Octopus.Client.OctopusRepository]::new([Octopus.Client.OctopusServerEndpoint]::new($OctopusUrl, $ApiKey))
# Get resources
$tenant = $repo.Tenants.FindByName($TenantName)
$project = $repo.Projects.FindByName($ProjectName)
$template = $project.Templates | Where-Object { $_.Name -eq $VariableName }
# Get ALL existing project variables for this tenant
$getRequest = [Octopus.Client.Model.TenantVariables.GetProjectVariablesByTenantIdRequest]::new($tenant.Id, $tenant.SpaceId)
$allExistingVariables = $repo.TenantVariables.Get($getRequest).Variables
# Create the target scope we want to match
$targetEnvIds = [Octopus.Client.Model.ReferenceCollection]::new()
$EnvironmentIds | ForEach-Object { $targetEnvIds.Add($_) }
$targetScope = [Octopus.Client.Model.TenantVariables.ProjectVariableScope]::new($targetEnvIds)
# Find the specific variable we want to update (match project, template, and scope)
$targetVariable = $allExistingVariables | Where-Object {
$_.ProjectId -eq $project.Id -and
$_.TemplateId -eq $template.Id -and
(Test-ScopeMatch $_.Scope $targetScope)
}
# Create payloads for all existing variables
$payloads = @()
# Add all existing variables (unchanged) to preserve them
foreach ($existingVar in $allExistingVariables) {
if ($existingVar -eq $targetVariable) {
# This is the variable we want to update - create new payload with our values
$payload = [Octopus.Client.Model.TenantVariables.TenantProjectVariablePayload]::new(
$project.Id,
$template.Id,
[Octopus.Client.Model.PropertyValueResource]::new($VariableValue, $false),
$targetScope
)
$payload.Id = $existingVar.Id
} else {
# This is a different variable - preserve it as-is
$payload = [Octopus.Client.Model.TenantVariables.TenantProjectVariablePayload]::new(
$existingVar.ProjectId,
$existingVar.TemplateId,
$existingVar.Value,
$existingVar.Scope
)
$payload.Id = $existingVar.Id
}
$payloads += $payload
}
# If the variable doesn't exist yet, add it as a new variable
if (-not $targetVariable) {
$newPayload = [Octopus.Client.Model.TenantVariables.TenantProjectVariablePayload]::new(
$project.Id,
$template.Id,
[Octopus.Client.Model.PropertyValueResource]::new($VariableValue, $false),
$targetScope
)
$newPayload.Id = [string]::Empty
$payloads += $newPayload
}
# Execute update using new API with all variables - any excluded variables are deleted
$command = [Octopus.Client.Model.TenantVariables.ModifyProjectVariablesByTenantIdCommand]::new($tenant.Id, $tenant.SpaceId, $payloads)
$repo.TenantVariables.Modify($command) | Out-Null
$scope = if ($EnvironmentIds.Count -eq 0) { "unscoped" } else { "scoped to: $($EnvironmentIds -join ', ')" }
$action = if ($targetVariable) { "updated ID: $($targetVariable.Id)" } else { "created new" }
Write-Host "Successfully updated '$VariableName' = '$VariableValue' for tenant '$TenantName' ($scope, $action)" -ForegroundColor Green
} catch {
Write-Error "Failed: $($_.Exception.Message)"
}
Common template value:
param(
[string]$OctopusUrl = "<Your server URL>",
[string]$ApiKey = "<Your API key>",
[string]$TenantName = "tenant 1",
[string]$LibrarySetName = "common set",
[string]$VariableName = "common template",
[string]$VariableValue = "test value",
[string[]]$EnvironmentIds = @("Environments-2","Environments-3") # Empty for unscoped
)
Add-Type -Path "<Path to Client dll>\Octopus.Server.Client.dll"
# Function to compare environment scopes
function Test-ScopeMatch($scope1, $scope2) {
$env1 = @($scope1.EnvironmentIds | Sort-Object)
$env2 = @($scope2.EnvironmentIds | Sort-Object)
if ($env1.Count -ne $env2.Count) { return $false }
for ($i = 0; $i -lt $env1.Count; $i++) {
if ($env1[$i] -ne $env2[$i]) { return $false }
}
return $true
}
try {
$repo = [Octopus.Client.OctopusRepository]::new([Octopus.Client.OctopusServerEndpoint]::new($OctopusUrl, $ApiKey))
# Get resources
$tenant = $repo.Tenants.FindByName($TenantName)
$librarySet = $repo.LibraryVariableSets.FindByName($LibrarySetName)
$template = $librarySet.Templates | Where-Object { $_.Name -eq $VariableName }
# Get ALL existing common variables for this tenant
$getRequest = [Octopus.Client.Model.TenantVariables.GetCommonVariablesByTenantIdRequest]::new($tenant.Id, $tenant.SpaceId)
$allExistingVariables = $repo.TenantVariables.Get($getRequest).Variables
# Create the target scope we want to match
$targetEnvIds = [Octopus.Client.Model.ReferenceCollection]::new()
$EnvironmentIds | ForEach-Object { $targetEnvIds.Add($_) }
$targetScope = [Octopus.Client.Model.TenantVariables.CommonVariableScope]::new($targetEnvIds)
# Find the specific variable we want to update (match library set, template, and scope)
$targetVariable = $allExistingVariables | Where-Object {
$_.LibraryVariableSetId -eq $librarySet.Id -and
$_.TemplateId -eq $template.Id -and
(Test-ScopeMatch $_.Scope $targetScope)
}
# Create payloads for all existing variables
$payloads = @()
# Add all existing variables (unchanged) to preserve them
foreach ($existingVar in $allExistingVariables) {
if ($existingVar -eq $targetVariable) {
# This is the variable we want to update - create new payload with our values
$payload = [Octopus.Client.Model.TenantVariables.TenantCommonVariablePayload]::new(
$librarySet.Id,
$template.Id,
[Octopus.Client.Model.PropertyValueResource]::new($VariableValue, $false),
$targetScope
)
$payload.Id = $existingVar.Id
} else {
# This is a different variable - preserve it as-is
$payload = [Octopus.Client.Model.TenantVariables.TenantCommonVariablePayload]::new(
$existingVar.LibraryVariableSetId,
$existingVar.TemplateId,
$existingVar.Value,
$existingVar.Scope
)
$payload.Id = $existingVar.Id
}
$payloads += $payload
}
# If the variable doesn't exist yet, add it as a new variable
if (-not $targetVariable) {
$newPayload = [Octopus.Client.Model.TenantVariables.TenantCommonVariablePayload]::new(
$librarySet.Id,
$template.Id,
[Octopus.Client.Model.PropertyValueResource]::new($VariableValue, $false),
$targetScope
)
$newPayload.Id = [string]::Empty
$payloads += $newPayload
}
# Execute update using new API with all variables - any excluded variables are deleted
$command = [Octopus.Client.Model.TenantVariables.ModifyCommonVariablesByTenantIdCommand]::new($tenant.Id, $tenant.SpaceId, $payloads)
$repo.TenantVariables.Modify($command) | Out-Null
$scope = if ($EnvironmentIds.Count -eq 0) { "unscoped" } else { "scoped to: $($EnvironmentIds -join ', ')" }
$action = if ($targetVariable) { "updated ID: $($targetVariable.Id)" } else { "created new" }
Write-Host "Successfully updated '$VariableName' = '$VariableValue' for tenant '$TenantName' ($scope, $action)" -ForegroundColor Green
} catch {
Write-Error "Failed: $($_.Exception.Message)"
}
Both scripts set the value as plain text. Existing sensitive values are passed back unchanged, so they keep their stored secret.
Verify
-
The script in step 3 prints a
Successfully updatedmessage instead of the error. -
Run the inspection script in step 2 again. Check that the value appears with the scope you set, that every other value is still listed, and that nothing appears under Problems.
-
In the Web Portal, go to Tenants, select the tenant, and open Variables. Check that the value and its Scope column match what you set.
-
Run your original integration again. It should finish without the error.
If the error comes back later, another script or integration is likely still writing through the legacy /variables endpoint. Check each tool that writes tenant variables, using the checks in step 1 of Diagnosis and fix.
Related
- Tenant variables documentation explains project templates, common templates, and how unscoped and scoped values apply to environments.
- Scoped tenant variables blog post covers the release of tenant variable scoping, the new API endpoints, migration behavior, and the legacy endpoint deprecation.
- Octopus REST API documentation covers authentication, API keys, and how to find resource IDs.
- Octopus.Server.Client NuGet package is the .NET client the step 3 scripts use.
- Go client variables package shows the request and response shapes for the scoped tenant variable endpoints.