Variable Group Creation using API in Azure Devops

Varun S Kumar 50 Reputation points
2023-08-31T09:47:36.1033333+00:00

I am trying to create a variable group in an Azure DevOps project using the REST APIs and Python scripting. The API used is:

POST https://dev.azure.com/{organization}/_apis/distributedtask/variablegroups?api-version=7.0

The payload in use is attached. I am receiving an error: Failed to create variable group. Status code: 500

Response: {"$id":"1","innerException":null,"message":"Atleast one variable group project reference is required.","typeName":"Microsoft.TeamFoundation.DistributedTask.Server.Exceptions.InvalidRequestException, Microsoft.TeamFoundation.DistributedTask.Server","typeKey":"InvalidRequestException","errorCode":0,"eventId":3000}

What might be the problem here?

vargroup_data = {
            "name": "MyVariableGroup",
            "description": "This is my variable group",
            "type": "Vsts",
            "variables": {
                "myVariable": {
                "value": "myValue",
                "isSecret": False
                },
                "myPublicVariable": {
                "value": "myPublicValue",
                "isSecret": False
                }
            },
            "variableGroupProjectReferences": {
                "projectReference": {
                    "id": project_id,
                    "name": project
                }
            }
        }
Community Center Not monitored
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Tony Dis 16 Reputation points
    2023-09-28T10:43:41.7933333+00:00

    The correct json (tested using postman) is

    {
        "name": "MyVariableGroup",
        "description": "This is my variable group",
        "type": "Vsts",
        "variables": {
            "myVariable": {
            "value": "myValue"
            },
            "myPublicVariable": {
            "value": "myPublicValue"
            }
        },
        "variableGroupProjectReferences": [
            {
                "name" : "MyVariableGroup",
                "description" : "This is my variable group",
                "projectReference": {
                    "id":"project_id",
                    "name": "project_name"
                }
            }
        ]
    }
    
    2 people found this answer helpful.
    0 comments No comments

  2. navba-MSFT 27,540 Reputation points Microsoft Employee Moderator
    2023-08-31T10:36:08.08+00:00

    @Varun S Kumar Welcome to Microsoft Q&A! Thanks for posting the question.

    DevOps is currently not supported in the Q&A forums, the supported products are listed over here:

    https://learn.microsoft.com/en-us/answers/products (more to be added later on).

    You can ask the experts in the dedicated forum over here:
    https://stackoverflow.com/questions/tagged/devops https://developercommunity.visualstudio.com/spaces/21/index.html

    **
    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.