2020-07-13 21:09:24 +00:00
---
stage: Release
2020-12-01 15:09:28 +00:00
group: Release
2020-11-26 06:09:20 +00:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-07-13 21:09:24 +00:00
type: concepts, howto
---
2021-09-08 09:09:10 +00:00
# Environments API **(FREE)**
2016-07-26 07:37:02 +00:00
## List environments
Get all environments for a given project.
2020-02-27 09:09:01 +00:00
```plaintext
2016-07-26 07:37:02 +00:00
GET /projects/:id/environments
```
| Attribute | Type | Required | Description |
| --------- | ------- | -------- | --------------------- |
2021-06-28 15:08:03 +00:00
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2019-06-24 12:18:40 +00:00
| `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` |
2020-06-23 06:08:52 +00:00
| `states` | string | no | List all environments that match a specific state. Accepted values: `available` or `stopped` . If no state value given, returns all environments. |
2016-07-26 07:37:02 +00:00
2020-01-30 15:09:15 +00:00
```shell
2020-05-28 00:08:37 +00:00
curl --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/projects/1/environments?name=review%2Ffix-foo"
2016-07-26 07:37:02 +00:00
```
Example response:
```json
[
{
"id": 1,
2016-12-08 01:09:18 +00:00
"name": "review/fix-foo",
"slug": "review-fix-foo-dfjre3",
2020-08-19 18:10:34 +00:00
"external_url": "https://review-fix-foo-dfjre3.gitlab.example.com",
2019-09-26 09:06:04 +00:00
"state": "available"
2016-07-26 07:37:02 +00:00
}
]
```
2019-04-09 09:16:57 +00:00
## Get a specific environment
2020-02-27 09:09:01 +00:00
```plaintext
2019-04-09 09:16:57 +00:00
GET /projects/:id/environments/:environment_id
```
| Attribute | Type | Required | Description |
|-----------|---------|----------|---------------------|
2021-06-28 15:08:03 +00:00
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2019-04-09 09:16:57 +00:00
| `environment_id` | integer | yes | The ID of the environment |
2020-01-30 15:09:15 +00:00
```shell
2019-04-09 09:16:57 +00:00
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",
2020-08-19 18:10:34 +00:00
"external_url": "https://review-fix-foo-dfjre3.gitlab.example.com",
2019-09-26 09:06:04 +00:00
"state": "available",
2021-09-27 12:13:56 +00:00
"created_at": "2019-05-25T18:55:13.252Z",
"updated_at": "2019-05-27T18:55:13.252Z",
2019-04-09 09:16:57 +00:00
"last_deployment": {
"id": 100,
"iid": 34,
"ref": "fdroid",
"sha": "416d8ea11849050d3d1f5104cf8cf51053e790ab",
"created_at": "2019-03-25T18:55:13.252Z",
2019-09-26 09:06:04 +00:00
"status": "success",
2019-04-09 09:16:57 +00:00
"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"
2020-08-18 18:10:10 +00:00
},
2019-04-09 09:16:57 +00:00
"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,
"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
2020-08-18 18:10:10 +00:00
},
2019-04-09 09:16:57 +00:00
"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
}
}
}
```
2016-07-26 07:37:02 +00:00
## Create a new environment
2020-06-02 18:08:32 +00:00
Creates a new environment with the given name and `external_url` .
2016-07-26 07:37:02 +00:00
2017-02-28 13:55:21 +00:00
It returns `201` if the environment was successfully created, `400` for wrong parameters.
2016-07-26 07:37:02 +00:00
2020-02-27 09:09:01 +00:00
```plaintext
2017-11-15 22:44:07 +00:00
POST /projects/:id/environments
2016-07-26 07:37:02 +00:00
```
| Attribute | Type | Required | Description |
| ------------- | ------- | -------- | ---------------------------- |
2021-06-28 15:08:03 +00:00
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2016-07-26 07:37:02 +00:00
| `name` | string | yes | The name of the environment |
2016-07-26 12:19:37 +00:00
| `external_url` | string | no | Place to link to for this environment |
2016-07-26 07:37:02 +00:00
2020-01-30 15:09:15 +00:00
```shell
2021-06-02 15:09:59 +00:00
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"
2016-07-26 07:37:02 +00:00
```
Example response:
```json
{
"id": 1,
"name": "deploy",
2016-12-08 01:09:18 +00:00
"slug": "deploy",
2020-08-19 18:10:34 +00:00
"external_url": "https://deploy.gitlab.example.com",
2021-09-27 12:13:56 +00:00
"state": "available",
"created_at": "2019-05-25T18:55:13.252Z",
"updated_at": "2019-05-27T18:55:13.252Z"
2016-07-26 07:37:02 +00:00
}
```
2016-07-29 10:14:36 +00:00
## Edit an existing environment
2016-07-26 07:37:02 +00:00
2020-06-02 18:08:32 +00:00
Updates an existing environment's name and/or `external_url` .
2016-07-29 10:14:36 +00:00
2017-02-28 13:55:21 +00:00
It returns `200` if the environment was successfully updated. In case of an error, a status code `400` is returned.
2016-07-26 07:37:02 +00:00
2020-02-27 09:09:01 +00:00
```plaintext
2016-07-29 10:14:36 +00:00
PUT /projects/:id/environments/:environments_id
2016-07-26 07:37:02 +00:00
```
2016-07-29 10:14:36 +00:00
| Attribute | Type | Required | Description |
| --------------- | ------- | --------------------------------- | ------------------------------- |
2021-06-28 15:08:03 +00:00
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2019-09-23 09:06:22 +00:00
| `environment_id` | integer | yes | The ID of the environment |
2021-08-26 21:11:25 +00:00
| `name` | string | no | [Deprecated and will be removed in GitLab 15.0 ](https://gitlab.com/gitlab-org/gitlab/-/issues/338897 ) |
2020-06-02 18:08:32 +00:00
| `external_url` | string | no | The new `external_url` |
2016-07-26 07:37:02 +00:00
2020-01-30 15:09:15 +00:00
```shell
2021-06-02 15:09:59 +00:00
curl --request PUT --data "name=staging& external_url=https://staging.gitlab.example.com" \
--header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/projects/1/environments/1"
2016-07-26 07:37:02 +00:00
```
Example response:
```json
{
"id": 1,
2016-07-29 10:14:36 +00:00
"name": "staging",
2016-12-08 01:09:18 +00:00
"slug": "staging",
2020-08-19 18:10:34 +00:00
"external_url": "https://staging.gitlab.example.com",
2021-09-27 12:13:56 +00:00
"state": "available",
"created_at": "2019-05-25T18:55:13.252Z",
"updated_at": "2019-05-27T18:55:13.252Z"
2016-07-26 07:37:02 +00:00
}
```
2016-07-29 10:14:36 +00:00
## Delete an environment
2016-07-26 07:37:02 +00:00
2017-09-06 07:15:34 +00:00
It returns `204` if the environment was successfully deleted, and `404` if the environment does not exist.
2016-07-26 07:37:02 +00:00
2020-02-27 09:09:01 +00:00
```plaintext
2016-07-29 10:14:36 +00:00
DELETE /projects/:id/environments/:environment_id
2016-07-26 07:37:02 +00:00
```
2016-07-29 10:14:36 +00:00
| Attribute | Type | Required | Description |
| --------- | ------- | -------- | --------------------- |
2021-06-28 15:08:03 +00:00
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2016-07-29 10:14:36 +00:00
| `environment_id` | integer | yes | The ID of the environment |
2016-07-26 07:37:02 +00:00
2020-01-30 15:09:15 +00:00
```shell
2018-12-27 09:03:08 +00:00
curl --request DELETE --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/projects/1/environments/1"
2016-07-26 07:37:02 +00:00
```
2017-02-28 13:55:21 +00:00
2021-08-04 15:08:46 +00:00
## Delete multiple stopped review apps
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/296625) in GitLab 14.2.
It schedules for deletion multiple environments that have already been
2021-08-11 09:10:00 +00:00
[stopped ](../ci/environments/index.md#stop-an-environment ) and
2021-08-04 15:08:46 +00:00
are [in the review app folder ](../ci/review_apps/index.md ).
The actual deletion is performed after 1 week from the time of execution.
```plaintext
DELETE /projects/:id/environments/review_apps
```
| Attribute | Type | Required | Description |
| --------- | ------- | -------- | --------------------- |
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user. |
| `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 will be 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": []
}
```
2017-02-28 13:55:21 +00:00
## Stop an environment
It returns `200` if the environment was successfully stopped, and `404` if the environment does not exist.
2020-02-27 09:09:01 +00:00
```plaintext
2017-02-28 13:55:21 +00:00
POST /projects/:id/environments/:environment_id/stop
```
| Attribute | Type | Required | Description |
| --------- | ------- | -------- | --------------------- |
2021-06-28 15:08:03 +00:00
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2017-02-28 13:55:21 +00:00
| `environment_id` | integer | yes | The ID of the environment |
2020-01-30 15:09:15 +00:00
```shell
2018-12-27 09:03:08 +00:00
curl --request POST --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/projects/1/environments/1/stop"
2017-02-28 13:55:21 +00:00
```
Example response:
```json
{
"id": 1,
"name": "deploy",
"slug": "deploy",
2020-08-19 18:10:34 +00:00
"external_url": "https://deploy.gitlab.example.com",
2021-09-27 12:13:56 +00:00
"state": "stopped",
"created_at": "2019-05-25T18:55:13.252Z",
"updated_at": "2019-05-27T18:55:13.252Z"
2017-02-28 13:55:21 +00:00
}
```