2020-07-30 08:09:33 -04:00
---
2020-08-04 02:10:16 -04:00
stage: Verify
2021-05-26 23:10:55 -04:00
group: Pipeline Authoring
2022-09-21 17:13:33 -04:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2020-07-30 08:09:33 -04:00
type: reference, api
---
2022-08-19 14:10:17 -04:00
# Project-level CI/CD variables API **(FREE)**
2016-01-07 08:10:49 -05:00
## List project variables
2017-07-19 07:57:27 -04:00
Get list of a project's variables.
2016-01-07 08:10:49 -05:00
2020-02-27 19:09:08 -05:00
```plaintext
2016-01-07 08:10:49 -05:00
GET /projects/:id/variables
```
2021-12-17 19:14:00 -05:00
| Attribute | Type | Required | Description |
2021-09-26 23:11:36 -04:00
| --------- | -------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
2021-06-28 11:08:03 -04:00
| `id` | integer/string | yes | The ID of a project or [URL-encoded NAMESPACE/PROJECT_NAME of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2016-01-13 14:23:48 -05:00
2020-02-27 19:09:08 -05:00
```shell
2018-12-27 04:03:08 -05:00
curl --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/projects/1/variables"
2016-01-13 14:23:48 -05:00
```
2016-01-07 08:10:49 -05:00
```json
[
{
2019-05-06 09:11:42 -04:00
"variable_type": "env_var",
2021-09-26 23:11:36 -04:00
"key": "TEST_VARIABLE_1",
"value": "TEST_1",
"protected": false,
"masked": true,
"environment_scope": "*"
2016-01-07 08:10:49 -05:00
},
{
2019-05-06 09:11:42 -04:00
"variable_type": "env_var",
2021-09-26 23:11:36 -04:00
"key": "TEST_VARIABLE_2",
"value": "TEST_2",
"protected": false,
"masked": false,
"environment_scope": "*"
2016-01-07 08:10:49 -05:00
}
]
```
2022-08-19 14:10:17 -04:00
## Get a single variable
2016-01-07 08:10:49 -05:00
2022-08-19 14:10:17 -04:00
Get the details of a single variable. If there are multiple variables with the same key,
use `filter` to select the correct `environment_scope` .
2016-01-07 08:10:49 -05:00
2020-02-27 19:09:08 -05:00
```plaintext
2016-01-07 08:10:49 -05:00
GET /projects/:id/variables/:key
```
2021-12-17 19:14:00 -05:00
| Attribute | Type | Required | Description |
2021-09-26 23:11:36 -04:00
| --------- | -------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | integer/string | yes | The ID of a project or [URL-encoded NAMESPACE/PROJECT_NAME of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
| `key` | string | yes | The `key` of a variable |
| `filter` | hash | no | Available filters: `[environment_scope]` . See the [`filter` parameter details ](#the-filter-parameter ). |
2016-01-13 14:23:48 -05:00
2020-02-27 19:09:08 -05:00
```shell
2018-12-27 04:03:08 -05:00
curl --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/projects/1/variables/TEST_VARIABLE_1"
2016-01-13 14:23:48 -05:00
```
2016-01-07 08:10:49 -05:00
```json
{
"key": "TEST_VARIABLE_1",
2019-05-06 09:11:42 -04:00
"variable_type": "env_var",
2019-05-16 16:07:42 -04:00
"value": "TEST_1",
"protected": false,
2021-09-26 23:11:36 -04:00
"masked": true,
"environment_scope": "*"
2016-01-07 08:10:49 -05:00
}
```
2022-08-19 14:10:17 -04:00
## Create a variable
2016-01-07 08:10:49 -05:00
2022-08-19 14:10:17 -04:00
Create a new variable. If a variable with the same `key` already exists, the new variable
must have a different `environment_scope` . Otherwise, GitLab returns a message similar to:
`VARIABLE_NAME has already been taken` .
2016-01-07 08:10:49 -05:00
2020-02-27 19:09:08 -05:00
```plaintext
2016-01-07 08:10:49 -05:00
POST /projects/:id/variables
```
2021-12-17 19:14:00 -05:00
| Attribute | Type | Required | Description |
2021-09-26 23:11:36 -04:00
| ------------------- | -------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | integer/string | yes | The ID of a project or [URL-encoded NAMESPACE/PROJECT_NAME of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
| `key` | string | yes | The `key` of a variable; must have no more than 255 characters; only `A-Z` , `a-z` , `0-9` , and `_` are allowed |
| `value` | string | yes | The `value` of a variable |
| `variable_type` | string | no | The type of a variable. Available types are: `env_var` (default) and `file` |
| `protected` | boolean | no | Whether the variable is protected. Default: `false` |
| `masked` | boolean | no | Whether the variable is masked. Default: `false` |
| `environment_scope` | string | no | The `environment_scope` of the variable. Default: `*` |
2016-01-13 14:23:48 -05:00
2020-02-27 19:09:08 -05:00
```shell
2021-06-02 11:09:59 -04:00
curl --request POST --header "PRIVATE-TOKEN: < your_access_token > " \
"https://gitlab.example.com/api/v4/projects/1/variables" --form "key=NEW_VARIABLE" --form "value=new value"
2016-01-13 14:23:48 -05:00
```
2016-01-07 08:10:49 -05:00
```json
{
2021-09-26 23:11:36 -04:00
"variable_type": "env_var",
2016-01-07 08:10:49 -05:00
"key": "NEW_VARIABLE",
2017-05-25 07:49:46 -04:00
"value": "new value",
2019-06-06 03:57:39 -04:00
"protected": false,
"masked": false,
"environment_scope": "*"
2016-01-07 08:10:49 -05:00
}
```
2022-08-19 14:10:17 -04:00
## Update a variable
2016-01-07 08:10:49 -05:00
2022-08-19 14:10:17 -04:00
Update a project's variable. If there are multiple variables with the same key,
use `filter` to select the correct `environment_scope` .
2016-01-07 08:10:49 -05:00
2020-02-27 19:09:08 -05:00
```plaintext
2016-01-07 08:10:49 -05:00
PUT /projects/:id/variables/:key
```
2021-12-17 19:14:00 -05:00
| Attribute | Type | Required | Description |
2021-09-26 23:11:36 -04:00
| ------------------- | -------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | integer/string | yes | The ID of a project or [URL-encoded NAMESPACE/PROJECT_NAME of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
| `key` | string | yes | The `key` of a variable |
| `value` | string | yes | The `value` of a variable |
| `variable_type` | string | no | The type of a variable. Available types are: `env_var` (default) and `file` |
| `protected` | boolean | no | Whether the variable is protected |
| `masked` | boolean | no | Whether the variable is masked |
| `environment_scope` | string | no | The `environment_scope` of the variable |
| `filter` | hash | no | Available filters: `[environment_scope]` . See the [`filter` parameter details ](#the-filter-parameter ). |
2016-01-13 14:23:48 -05:00
2020-02-27 19:09:08 -05:00
```shell
2021-06-02 11:09:59 -04:00
curl --request PUT --header "PRIVATE-TOKEN: < your_access_token > " \
"https://gitlab.example.com/api/v4/projects/1/variables/NEW_VARIABLE" --form "value=updated value"
2016-01-13 14:23:48 -05:00
```
2016-01-07 08:10:49 -05:00
```json
{
2021-09-26 23:11:36 -04:00
"variable_type": "env_var",
2016-01-07 08:10:49 -05:00
"key": "NEW_VARIABLE",
2017-05-25 07:49:46 -04:00
"value": "updated value",
2019-05-16 16:07:42 -04:00
"protected": true,
2019-06-06 03:57:39 -04:00
"masked": false,
"environment_scope": "*"
2016-01-07 08:10:49 -05:00
}
```
2022-08-19 14:10:17 -04:00
## Delete a variable
2016-01-07 08:10:49 -05:00
2022-08-19 14:10:17 -04:00
Delete a project's variable. If there are multiple variables with the same key,
use `filter` to select the correct `environment_scope` .
2016-01-07 08:10:49 -05:00
2020-02-27 19:09:08 -05:00
```plaintext
2016-01-07 08:10:49 -05:00
DELETE /projects/:id/variables/:key
```
2021-12-17 19:14:00 -05:00
| Attribute | Type | Required | Description |
2021-09-26 23:11:36 -04:00
| --------- | -------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | integer/string | yes | The ID of a project or [URL-encoded NAMESPACE/PROJECT_NAME of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
| `key` | string | yes | The `key` of a variable |
| `filter` | hash | no | Available filters: `[environment_scope]` . See the [`filter` parameter details ](#the-filter-parameter ). |
2016-01-13 14:23:48 -05:00
2020-02-27 19:09:08 -05:00
```shell
2018-12-27 04:03:08 -05:00
curl --request DELETE --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/projects/1/variables/VARIABLE_1"
2016-01-13 14:23:48 -05:00
```
2020-07-08 05:09:17 -04:00
## The `filter` parameter
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/34490) in GitLab 13.2.
2020-09-14 08:09:34 -04:00
> - [Feature flag removed](https://gitlab.com/gitlab-org/gitlab/-/issues/227052) in GitLab 13.4.
2020-07-08 05:09:17 -04:00
2022-08-19 14:10:17 -04:00
When multiple variables have the same `key` , [GET ](#get-a-single-variable ), [PUT ](#update-a-variable ),
or [DELETE ](#delete-a-variable ) requests might return:
2020-07-08 05:09:17 -04:00
2022-08-19 14:10:17 -04:00
```plaintext
There are multiple variables with provided parameters. Please use 'filter[environment_scope]'.
2020-07-08 05:09:17 -04:00
```
2022-08-19 14:10:17 -04:00
Use `filter[environment_scope]` to select the variable with the matching `environment_scope` attribute.
For example:
- GET:
```shell
curl --globoff --header "PRIVATE-TOKEN: < your_access_token > " \
"https://gitlab.example.com/api/v4/projects/1/variables/SCOPED_VARIABLE_1?filter[environment_scope]=production"
```
- PUT:
```shell
curl --request PUT --globoff --header "PRIVATE-TOKEN: < your_access_token > " \
"https://gitlab.example.com/api/v4/projects/1/variables/SCOPED_VARIABLE_1?value=scoped-variable-updated-value& environment_scope=production& filter[environment_scope]=production"
```
- DELETE:
```shell
curl --request DELETE --globoff --header "PRIVATE-TOKEN: < your_access_token > " \
"https://gitlab.example.com/api/v4/projects/1/variables/SCOPED_VARIABLE_1?filter[environment_scope]=production"
```