How to create/update a workspace variable via API?
Asked by Chris Fouts:

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>


Answers:
author: Patrik S
body: Hey [@Chris Fouts](/t5/user/viewprofilepage/user-id/1503446) ,

thanks for reaching out to Community!

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

* [Update a variable for a repository](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-variables-variable-uuid-put)   

  ```
  PUT https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/pipelines_config/variables/{variable_uuid}
  ```

* [Update a variable for a workspace](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-workspaces-workspace-pipelines-config-variables-variable-uuid-put)   

  ```
  PUT https://api.bitbucket.org/2.0/workspaces/{workspace}/pipelines-config/variables/{variable_uuid}
  ```

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](/t5/user/viewprofilepage/user-id/1503446) !

Patrik S

comments: author: Chris Fouts
body: Thanks [@Patrik S](/t5/user/viewprofilepage/user-id/4603309) 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
  }'
```


author: Chris Fouts
body: I opened a Bitbucket support ticket too where I give more information that I can't put here.


author: Patrik S
body: Hey [@Chris Fouts](/t5/user/viewprofilepage/user-id/1503446) ,

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](/t5/user/viewprofilepage/user-id/1503446) !

Patrik


author: Chris Fouts
body: 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!


author: Patrik S
body: Hello [@Chris Fouts](/t5/user/viewprofilepage/user-id/1503446) ,

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](/t5/user/viewprofilepage/user-id/1503446) !

Patrik S


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


Tags: bitbucket-cloud

cloud