*Cloud › Environments*

# Create an environment

```http
POST https://devapi.dispralabs.com/v1/cloud/projects/:prj/environments
```

Adds an environment to the project: `staging`, `preview-142`, any name. Pass `copy_from` to copy another environment's services, settings and variables (its volumes and domains are not copied).

Needs a key with `full` access.

- **Authentication:** Secret key (send it as `Authorization: Bearer <key>`)
- **Idempotency:** Safe to retry. Send an `Idempotency-Key` header and repeated calls within 24 hours return the first result.

## Parameters

- `prj` (string, required): The project's id. Example: `prj_01jdk3m9x2v7c4q8w5n6b1t0ra`
- `name` (string, required): The new environment's name: lowercase letters, digits and dashes. Example: `staging`
- `copy_from` (string, optional): An environment to copy the services and variables of. Example: `env_01jdk3m9x5h2p8r4t6y1u3i0oa`

## Returns

- **200**: env and status (created).

## Errors

| Status | Code | When |
|---|---|---|
| 400 | [invalid_request](https://docs.dispralabs.com/errors.md#invalid_request) | A field is missing or not valid. The message names it. |
| 404 | [not_found](https://docs.dispralabs.com/errors.md#not_found) | No such object in the key's project (or environment). |
| 409 | [name_taken](https://docs.dispralabs.com/errors.md#name_taken) | The project already has an environment with that name. |
| 409 | [account_frozen](https://docs.dispralabs.com/errors.md#account_frozen) | The workspace is out of credits: add credits on the dashboard to create, change or deploy. |

## Request samples

### Dispra Cloud CLI

```bash
dcloud env create staging --from production
```

### cURL

```bash
curl -X POST "https://devapi.dispralabs.com/v1/cloud/projects/prj_01jdk3m9x2v7c4q8w5n6b1t0ra/environments" \
  -H "Authorization: Bearer dsp_live_YOUR_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{"name":"staging","copy_from":"env_01jdk3m9x5h2p8r4t6y1u3i0oa"}'
```

### Node

```javascript
const res = await fetch("https://devapi.dispralabs.com/v1/cloud/projects/prj_01jdk3m9x2v7c4q8w5n6b1t0ra/environments", {
  method: "POST",
  headers: { Authorization: "Bearer dsp_live_YOUR_KEY", "Idempotency-Key": crypto.randomUUID(), "Content-Type": "application/json" },
  body: JSON.stringify({
    "name": "staging",
    "copy_from": "env_01jdk3m9x5h2p8r4t6y1u3i0oa"
  }),
});
console.log(await res.json());
```

### Python

```python
import uuid, requests
res = requests.request("POST", "https://devapi.dispralabs.com/v1/cloud/projects/prj_01jdk3m9x2v7c4q8w5n6b1t0ra/environments", headers={"Authorization": "Bearer dsp_live_YOUR_KEY", "Idempotency-Key": str(uuid.uuid4())}, json={
  "name": "staging",
  "copy_from": "env_01jdk3m9x5h2p8r4t6y1u3i0oa"
})
print(res.json())
```

### Go

```go
body := strings.NewReader(`{"name":"staging","copy_from":"env_01jdk3m9x5h2p8r4t6y1u3i0oa"}`)
req, _ := http.NewRequest("POST", "https://devapi.dispralabs.com/v1/cloud/projects/prj_01jdk3m9x2v7c4q8w5n6b1t0ra/environments", body)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer dsp_live_YOUR_KEY")
req.Header.Set("Idempotency-Key", uuid.NewString())
res, _ := http.DefaultClient.Do(req)
```

## Responses

### 200

```json
{}
```

### 400 invalid_request

```json
{
  "error": {
    "code": "invalid_request",
    "message": "A field is missing or not valid. The message names it.",
    "request_id": "req_7f3a9c21d84be056"
  }
}
```

### 404 not_found

```json
{
  "error": {
    "code": "not_found",
    "message": "No such object in the key's project (or environment).",
    "request_id": "req_7f3a9c21d84be056"
  }
}
```

### 409 name_taken

```json
{
  "error": {
    "code": "name_taken",
    "message": "The project already has an environment with that name.",
    "request_id": "req_7f3a9c21d84be056"
  }
}
```

### 409 account_frozen

```json
{
  "error": {
    "code": "account_frozen",
    "message": "The workspace is out of credits: add credits on the dashboard to create, change or deploy.",
    "request_id": "req_7f3a9c21d84be056"
  }
}
```
