Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to create/update a workspace variable via API?

Chris Fouts August 30, 2024

I see this blog, but it refers to a specific repository variable. I however, want to create/update a workspace variable. Will it be the same --data, but the URL will stop at the workspace?

https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-variables-variable-uuid-put

1 answer

1 accepted

0 votes
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 3, 2024

Hey @Chris Fouts ,

thanks for reaching out to Community!

The endpoint to update workspace variables and repository variables is a little bit different:

While the endpoints are different, the request body (--data) is the same amongst those two endpoints.

I hope that helps! Should you have any questions, feel free to ask.

Thank you, @Chris Fouts !

Patrik S

Chris Fouts September 5, 2024

Thanks @Patrik S I was able to create a workspace variable per you answer. However, I can't update it, and get no discernable error. I get the variable_uuid by

GET 
https://api.bitbucket.org/2.0/workspaces/{workspace}/pipelines-config/variables?page=2

and see



      "type": "pipeline_variable",

      "uuid": "{f8278c5e-b42b-4fc7-8a47-d3bbb8f98808}",

      "key": "TEST_VAR",

      "value": "TEST_VALUE",

      "secured": false,

      "system": false,

      "scope": "ACCOUNT"

    },

So I do a

PUT https://api.bitbucket.org/2.0/workspaces/{workspace}/pipelines-config/variables/{f8278c5e-b42b-4fc7-8a47-d3bbb8f98808} \
--data '{
"key": "TEST_VAR",
"value: "NEW_VALUE",
"secured: false
}'
Chris Fouts September 5, 2024

I opened a Bitbucket support ticket too where I give more information that I can't put here.

Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 5, 2024

Hey @Chris Fouts ,

I noticed you opened a support ticket but set it as resolved already.

I'll be sharing the same response here so it may be useful for other users that come across the same issue.

To update a workspace, you can use a request similar to the below:

curl --location --globoff --request PUT 'https://api.bitbucket.org/2.0/workspaces/WORKSPACE/pipelines-config/variables/{VARIABLE UUID}' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <TOKEN>' \
--data '{
    "key": "TEST_VAR",
    "value": "TEST_VALUE2",
    "secured": false
}'

This assumes you're using a workspace access token for authentication, that has at least the "Edit variables" scope.

Could you try making a request based on the example above and let us know how it goes?

Thank you, @Chris Fouts !

Patrik 

Chris Fouts September 5, 2024

The following options made it work.

--location --globoff

However, they're not mentioned here.

https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-workspaces-workspace-pipelines-config-variables-put.

 

thanks!

Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 6, 2024

Hello @Chris Fouts ,

thanks for the update!

The --location option instructs curl to follow any redirects (it's not mandatory for this particular endpoint).

As for the --globoff, this is to make curl not to interpret special characters such as the curly brackets {} in the variable UUID.

If you don't provide this option, you would need to encode the curly brackets {} in the URL as follows :

{ %7B
} %7D

Example:

curl --request PUT 'https://api.bitbucket.org/2.0/workspaces/WORKSPACE/pipelines-config/variables/%7BVARIABLE UUID%7D' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <TOKEN>' \
--data '{
    "key": "TEST_VAR",
    "value": "TEST_VALUE2",
    "secured": false
}'

Thank you, @Chris Fouts !

Patrik S 

Chris Fouts September 6, 2024

Yes, that makes sense. I'm embedding this in a script so I like the CLI options instead of the character replacement. Thanks again!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin Site Admin
TAGS
AUG Leaders

Atlassian Community Events