# Group-level Variables API

> 原文：[https://docs.gitlab.com/ee/api/group_level_variables.html](https://docs.gitlab.com/ee/api/group_level_variables.html)

*   [List group variables](#list-group-variables)
*   [Show variable details](#show-variable-details)
*   [Create variable](#create-variable)
*   [Update variable](#update-variable)
*   [Remove variable](#remove-variable)

# Group-level Variables API[](#group-level-variables-api "Permalink")

在 GitLab 9.5 中[引入](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/34519)

## List group variables[](#list-group-variables "Permalink")

获取组变量的列表.

```
GET /groups/:id/variables 
```

| Attribute | Type | required | Description |
| --- | --- | --- | --- |
| `id` | integer/string | yes | 经过身份验证的用户拥有的组的 ID 或[URL 编码的路径](README.html#namespaced-path-encoding) |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/1/variables" 
```

```
[  {  "key":  "TEST_VARIABLE_1",  "variable_type":  "env_var",  "value":  "TEST_1",  "protected":  false,  "masked":  false  },  {  "key":  "TEST_VARIABLE_2",  "variable_type":  "env_var",  "value":  "TEST_2",  "protected":  false,  "masked":  false  }  ] 
```

## Show variable details[](#show-variable-details "Permalink")

获取组特定变量的详细信息.

```
GET /groups/:id/variables/:key 
```

| Attribute | Type | required | Description |
| --- | --- | --- | --- |
| `id` | integer/string | yes | 经过身份验证的用户拥有的组的 ID 或[URL 编码的路径](README.html#namespaced-path-encoding) |
| `key` | string | yes | 变量的`key` |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/1/variables/TEST_VARIABLE_1" 
```

```
{  "key":  "TEST_VARIABLE_1",  "variable_type":  "env_var",  "value":  "TEST_1",  "protected":  false,  "masked":  false  } 
```

## Create variable[](#create-variable "Permalink")

创建一个新变量.

```
POST /groups/:id/variables 
```

| Attribute | Type | required | Description |
| --- | --- | --- | --- |
| `id` | integer/string | yes | 经过身份验证的用户拥有的组的 ID 或[URL 编码的路径](README.html#namespaced-path-encoding) |
| `key` | string | yes | 变量的`key` ； 不得超过 255 个字符； 仅允许`AZ` ， `az` ， `0-9`和`_` |
| `value` | string | yes | 变量的`value` |
| `variable_type` | string | no | 变量的类型. 可用类型为： `env_var` （默认）和`file` |
| `protected` | boolean | no | 变量是否受保护 |
| `masked` | boolean | no | 变量是否被屏蔽 |

```
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/1/variables" --form "key=NEW_VARIABLE" --form "value=new value" 
```

```
{  "key":  "NEW_VARIABLE",  "value":  "new value",  "variable_type":  "env_var",  "protected":  false,  "masked":  false  } 
```

## Update variable[](#update-variable "Permalink")

更新组的变量.

```
PUT /groups/:id/variables/:key 
```

| Attribute | Type | required | Description |
| --- | --- | --- | --- |
| `id` | integer/string | yes | 经过身份验证的用户拥有的组的 ID 或[URL 编码的路径](README.html#namespaced-path-encoding) |
| `key` | string | yes | 变量的`key` |
| `value` | string | yes | 变量的`value` |
| `variable_type` | string | no | 变量的类型. 可用类型为： `env_var` （默认）和`file` |
| `protected` | boolean | no | 变量是否受保护 |
| `masked` | boolean | no | 变量是否被屏蔽 |

```
curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/1/variables/NEW_VARIABLE" --form "value=updated value" 
```

```
{  "key":  "NEW_VARIABLE",  "value":  "updated value",  "variable_type":  "env_var",  "protected":  true,  "masked":  true  } 
```

## Remove variable[](#remove-variable "Permalink")

删除组的变量.

```
DELETE /groups/:id/variables/:key 
```

| Attribute | Type | required | Description |
| --- | --- | --- | --- |
| `id` | integer/string | yes | 经过身份验证的用户拥有的组的 ID 或[URL 编码的路径](README.html#namespaced-path-encoding) |
| `key` | string | yes | 变量的`key` |

```
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/1/variables/VARIABLE_1" 
```