1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
---
stage: Deploy
group: Environments
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://handbook.gitlab.com/handbook/product/ux/technical-writing/#assignments
---
# Environments API
DETAILS:
**Tier:** Free, Premium, Ultimate
**Offering:** GitLab.com, Self-managed, GitLab Dedicated
> Support for [GitLab CI/CD job token](../ci/jobs/ci_job_token.md) authentication [introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/414549) in GitLab 16.2.
## List environments
Get all environments for a given project.
```plaintext
GET /projects/:id/environments
```
| Attribute | Type | Required | Description |
|-----------|----------------|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `id` | integer/string | yes | The ID or [URL-encoded](rest/index.md#namespaced-paths) path of the project. |
| `name` | string | no | Return the environment with this name. Mutually exclusive with `search`. |
| `search` | string | no | Return list of environments matching the search criteria. Mutually exclusive with `name`. Must be at least 3 characters long. |
| `states` | string | no | List all environments that match a specific state. Accepted values: `available`, `stopping`, or `stopped`. If no state value given, returns all environments. |
```shell
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/environments?name=review%2Ffix-foo"
```
Example response:
```json
[
{
"id": 1,
"name": "review/fix-foo",
"slug": "review-fix-foo-dfjre3",
"description": "This is review environment",
"external_url": "https://review-fix-foo-dfjre3.gitlab.example.com",
"state": "available",
"tier": "development",
"created_at": "2019-05-25T18:55:13.252Z",
"updated_at": "2019-05-27T18:55:13.252Z",
"enable_advanced_logs_querying": false,
"logs_api_path": "/project/-/logs/k8s.json?environment_name=review%2Ffix-foo",
"auto_stop_at": "2019-06-03T18:55:13.252Z",
"kubernetes_namespace": "flux-system",
"flux_resource_path": "HelmRelease/flux-system"
}
]
```
## Get a specific environment
```plaintext
GET /projects/:id/environments/:environment_id
```
| Attribute | Type | Required | Description |
|------------------|----------------|----------|--------------------------------------------------------------------------------------|
| `id` | integer/string | yes | The ID or [URL-encoded path](rest/index.md#namespaced-paths) of the project. |
| `environment_id` | integer | yes | The ID of the environment. |
```shell
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/environments/1"
```
Example of response
```json
{
"id": 1,
"name": "review/fix-foo",
"slug": "review-fix-foo-dfjre3",
"description": "This is review environment",
"external_url": "https://review-fix-foo-dfjre3.gitlab.example.com",
"state": "available",
"tier": "development",
"created_at": "2019-05-25T18:55:13.252Z",
"updated_at": "2019-05-27T18:55:13.252Z",
"enable_advanced_logs_querying": false,
"logs_api_path": "/project/-/logs/k8s.json?environment_name=review%2Ffix-foo",
"auto_stop_at": "2019-06-03T18:55:13.252Z",
"last_deployment": {
"id": 100,
"iid": 34,
"ref": "fdroid",
"sha": "416d8ea11849050d3d1f5104cf8cf51053e790ab",
"created_at": "2019-03-25T18:55:13.252Z",
"status": "success",
"user": {
"id": 1,
"name": "Administrator",
"state": "active",
"username": "root",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
"web_url": "http://localhost:3000/root"
},
"deployable": {
"id": 710,
"status": "success",
"stage": "deploy",
"name": "staging",
"ref": "fdroid",
"tag": false,
"coverage": null,
"created_at": "2019-03-25T18:55:13.215Z",
"started_at": "2019-03-25T12:54:50.082Z",
"finished_at": "2019-03-25T18:55:13.216Z",
"duration": 21623.13423,
"project": {
"ci_job_token_scope_enabled": false
},
"user": {
"id": 1,
"name": "Administrator",
"username": "root",
"state": "active",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
"web_url": "http://gitlab.dev/root",
"created_at": "2015-12-21T13:14:24.077Z",
"bio": null,
"location": null,
"public_email": "",
"skype": "",
"linkedin": "",
"twitter": "",
"website_url": "",
"organization": null
},
"commit": {
"id": "416d8ea11849050d3d1f5104cf8cf51053e790ab",
"short_id": "416d8ea1",
"created_at": "2016-01-02T15:39:18.000Z",
"parent_ids": [
"e9a4449c95c64358840902508fc827f1a2eab7df"
],
"title": "Removed fabric to fix #40",
"message": "Removed fabric to fix #40\n",
"author_name": "Administrator",
"author_email": "admin@example.com",
"authored_date": "2016-01-02T15:39:18.000Z",
"committer_name": "Administrator",
"committer_email": "admin@example.com",
"committed_date": "2016-01-02T15:39:18.000Z"
},
"pipeline": {
"id": 34,
"sha": "416d8ea11849050d3d1f5104cf8cf51053e790ab",
"ref": "fdroid",
"status": "success",
"web_url": "http://localhost:3000/Commit451/lab-coat/pipelines/34"
},
"web_url": "http://localhost:3000/Commit451/lab-coat/-/jobs/710",
"artifacts": [
{
"file_type": "trace",
"size": 1305,
"filename": "job.log",
"file_format": null
}
],
"runner": null,
"artifacts_expire_at": null
}
},
"cluster_agent": {
"id": 1,
"name": "agent-1",
"config_project": {
"id": 20,
"description": "",
"name": "test",
"name_with_namespace": "Administrator / test",
"path": "test",
"path_with_namespace": "root/test",
"created_at": "2022-03-20T20:42:40.221Z"
},
"created_at": "2022-04-20T20:42:40.221Z",
"created_by_user_id": 42
},
"kubernetes_namespace": "flux-system",
"flux_resource_path": "HelmRelease/flux-system"
}
```
## Create a new environment
Creates a new environment with the given name and `external_url`.
It returns `201` if the environment was successfully created, `400` for wrong parameters.
```plaintext
POST /projects/:id/environments
```
| Attribute | Type | Required | Description |
|------------------------|----------------|----------|---------------------------------------------------------------------------------------------------------------------|
| `id` | integer/string | yes | The ID or [URL-encoded path](rest/index.md#namespaced-paths) of the project. |
| `name` | string | yes | The name of the environment. |
| `description` | string | no | The description of the environment. |
| `external_url` | string | no | Place to link to for this environment. |
| `tier` | string | no | The tier of the new environment. Allowed values are `production`, `staging`, `testing`, `development`, and `other`. |
| `cluster_agent_id` | integer | no | The cluster agent to associate with this environment. |
| `kubernetes_namespace` | string | no | The Kubernetes namespace to associate with this environment. |
| `flux_resource_path` | string | no | The Flux resource path to associate with this environment. This must be the full resource path. For example, `helm.toolkit.fluxcd.io/v2/namespaces/gitlab-agent/helmreleases/gitlab-agent`. |
```shell
curl --data "name=deploy&external_url=https://deploy.gitlab.example.com" \
--header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/environments"
```
Example response:
```json
{
"id": 1,
"name": "deploy",
"slug": "deploy",
"description": null,
"external_url": "https://deploy.gitlab.example.com",
"state": "available",
"tier": "production",
"created_at": "2019-05-25T18:55:13.252Z",
"updated_at": "2019-05-27T18:55:13.252Z",
"kubernetes_namespace": "flux-system",
"flux_resource_path": "HelmRelease/flux-system"
}
```
## Update an existing environment
> - Parameter `name` [removed](https://gitlab.com/gitlab-org/gitlab/-/issues/338897) in GitLab 16.0.
Updates an existing environment's name and/or `external_url`.
It returns `200` if the environment was successfully updated. In case of an error, a status code `400` is returned.
```plaintext
PUT /projects/:id/environments/:environments_id
```
| Attribute | Type | Required | Description |
|------------------------|-----------------|----------|---------------------------------------------------------------------------------------------------------------------|
| `id` | integer/string | yes | The ID or [URL-encoded path of the project](rest/index.md#namespaced-paths). |
| `environment_id` | integer | yes | The ID of the environment. |
| `description` | string | no | The description of the environment. |
| `external_url` | string | no | The new `external_url`. |
| `tier` | string | no | The tier of the new environment. Allowed values are `production`, `staging`, `testing`, `development`, and `other`. |
| `cluster_agent_id` | integer or null | no | The cluster agent to associate with this environment or `null` to remove it. |
| `kubernetes_namespace` | string or null | no | The Kubernetes namespace to associate with this environment or `null` to remove it. |
| `flux_resource_path` | string or null | no | The Flux resource path to associate with this environment or `null` to remove it. |
```shell
curl --request PUT --data "external_url=https://staging.gitlab.example.com" \
--header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/environments/1"
```
Example response:
```json
{
"id": 1,
"name": "staging",
"slug": "staging",
"description": null,
"external_url": "https://staging.gitlab.example.com",
"state": "available",
"tier": "staging",
"created_at": "2019-05-25T18:55:13.252Z",
"updated_at": "2019-05-27T18:55:13.252Z",
"kubernetes_namespace": "flux-system",
"flux_resource_path": "HelmRelease/flux-system"
}
```
## Delete an environment
It returns `204` if the environment was successfully deleted, and `404` if the environment does not exist. The environment must be stopped first, otherwise the request returns `403`.
```plaintext
DELETE /projects/:id/environments/:environment_id
```
| Attribute | Type | Required | Description |
|------------------|----------------|----------|--------------------------------------------------------------------------------------|
| `id` | integer/string | yes | The ID or [URL-encoded path](rest/index.md#namespaced-paths) of the project. |
| `environment_id` | integer | yes | The ID of the environment. |
```shell
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/environments/1"
```
## Delete multiple stopped review apps
It schedules for deletion multiple environments that have already been
[stopped](../ci/environments/index.md#stopping-an-environment) and
are [in the review app folder](../ci/review_apps/index.md).
The actual deletion is performed after 1 week from the time of execution.
By default, it only deletes environments 30 days or older. You can change this default using the `before` parameter.
```plaintext
DELETE /projects/:id/environments/review_apps
```
| Attribute | Type | Required | Description |
|-----------|----------------|----------|--------------------------------------------------------------------------------------------------------------------------------------------------------|
| `id` | integer/string | yes | The ID or [URL-encoded path](rest/index.md#namespaced-paths) of the project. |
| `before` | datetime | no | The date before which environments can be deleted. Defaults to 30 days ago. Expected in ISO 8601 format (`YYYY-MM-DDTHH:MM:SSZ`). |
| `limit` | integer | no | Maximum number of environments to delete. Defaults to 100. |
| `dry_run` | boolean | no | Defaults to `true` for safety reasons. It performs a dry run where no actual deletion is performed. Set to `false` to actually delete the environment. |
```shell
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/environments/review_apps"
```
Example response:
```json
{
"scheduled_entries": [
{
"id": 387,
"name": "review/023f1bce01229c686a73",
"slug": "review-023f1bce01-3uxznk",
"external_url": null
},
{
"id": 388,
"name": "review/85d4c26a388348d3c4c0",
"slug": "review-85d4c26a38-5giw1c",
"external_url": null
}
],
"unprocessable_entries": []
}
```
## Stop an environment
It returns `200` if the environment was successfully stopped, and `404` if the environment does not exist.
```plaintext
POST /projects/:id/environments/:environment_id/stop
```
| Attribute | Type | Required | Description |
|------------------|----------------|----------|--------------------------------------------------------------------------------------|
| `id` | integer/string | yes | The ID or [URL-encoded path](rest/index.md#namespaced-paths) of the project. |
| `environment_id` | integer | yes | The ID of the environment. |
| `force` | boolean | no | Force environment to stop without executing `on_stop` actions. |
```shell
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/environments/1/stop"
```
Example response:
```json
{
"id": 1,
"name": "deploy",
"slug": "deploy",
"external_url": "https://deploy.gitlab.example.com",
"state": "stopped",
"created_at": "2019-05-25T18:55:13.252Z",
"updated_at": "2019-05-27T18:55:13.252Z",
"kubernetes_namespace": "flux-system",
"flux_resource_path": "HelmRelease/flux-system"
}
```
## Stop stale environments
Issue stop request to all environments that were last modified or deployed to before a specified date. Excludes protected environments. Returns `200` if stop request was successful and `400` if the before date is invalid. For details of exactly when the environment is stopped, see [Stop an environment](../ci/environments/index.md#stopping-an-environment).
```plaintext
POST /projects/:id/environments/stop_stale
```
| Attribute | Type | Required | Description |
|-----------|----------------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `id` | integer/string | yes | The ID or [URL-encoded path](rest/index.md#namespaced-paths) of the project. |
| `before` | date | yes | Stop environments that have been modified or deployed to before the specified date. Expected in ISO 8601 format (`2019-03-15T08:00:00Z`). Valid inputs are between 10 years ago and 1 week ago |
```shell
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/environments/stop_stale?before=10%2F10%2F2021"
```
Example response:
```json
{
"message": "Successfully requested stop for all stale environments"
}
```
|