---
title: "Create a variable"
method: POST
path: "/api/v4/projects/{id}/variables"
tags: ["ci_variables"]
---

# Create a variable

`POST /api/v4/projects/{id}/variables`

## Request

- Base URL: `https://gitlab.com`
- URL: `https://gitlab.com/api/v4/projects/{id}/variables`
- Auth: none declared

Creates a variable. If a variable with the same `key` already exists, the variable must have a different `environment_scope`. Otherwise, GitLab returns a message similar to: `VARIABLE_NAME has already been taken`.

## Path parameters

- `id` string, required

## Request body

- PostApiV4ProjectsIdVariables: Create a variable
  - `key` string, required: The key of a variable
  - `value` string, required: The value of a variable
  - `protected` boolean: Whether the variable is protected
  - `masked` boolean: Whether the variable is masked
  - `masked_and_hidden` boolean: Whether the variable is masked and hidden
  - `raw` boolean: Whether the variable will be expanded
  - `variable_type` 'env_var' | 'file': The type of the variable. Default: env_var
  - `environment_scope` string: The environment_scope of the variable
  - `description` string: The description of the variable

## Response `201`

Create a variable

- APIEntitiesCiVariable: API_Entities_Ci_Variable model
  - `variable_type` string, required
  - `key` string, required
  - `value` string, required
  - `hidden` boolean
  - `protected` boolean
  - `masked` boolean
  - `raw` boolean
  - `environment_scope` string
  - `description` string

## Other responses

- `400`: 400 Bad Request

---

[API](https://skmtc.dev/gitlab/apis/gitlab-api.md) - [All operations](https://skmtc.dev/gitlab/apis/gitlab-api/llms.txt) - [OpenAPI document](https://skmtc.dev/gitlab/apis/gitlab-api/revisions/b234947ef3f2?raw)
