---
title: "Create an environment's repository"
url: https://docs.corefinity.com/api/reference/enterprise/create-environment-repository
type: endpoint
last_updated: 2026-11-13
feature: enterprise-deployments
related:
  - https://docs.corefinity.com/guides/corestack-enterprise/deployments-and-pipelines.md
---
# Create an environment's repository

Connects a git repository to the environment through one of the git accounts `GET
/environments/{environment}/repositories/connections` lists. Your role needs the Manage Deployment
Repositories permission and, while a push deploys on its own (`git_deploys` is `automatic`), Create
Deployments as well.

```http
POST /api/environments/{environment}/repositories
```

- Scope: `write`
- Two-factor: required on your account (all)
- Rate limit: 60 a minute
- Try it in the console: off. It has not been cleared for the console. Copy the request and run it yourself when you are sure.

## Path parameters

- `environment` (integer, required): The id of a CoreStack Enterprise environment. Not the same number as a site row id.

## Body

- `branch` (string, required): The branch to deploy. No spaces, no `..`, and no `-` at the start.
- `connection_id` (integer, required): The `id` of a git account `GET /environments/{environment}/repositories/connections` lists. The repository's provider is the account's.
- `pipeline_id` (integer, required): The `id` of the environment's pipeline the repository deploys with. A missing or wrong value is reported on `environment_deployment_pipeline_id`.
- `repo` (string, required): The repository's path at the provider, such as `northwind-digital/harbourandfinch-trade`: parts of letters, digits, `.`, `_` and `-` separated by `/`, none of them `.` or `..`, at most 255 characters. Exactly `owner/name` for a GitHub or Bitbucket account; two or more parts for a GitLab account.
- `root_directory` (string, required): The folder of the repository that is deployed, such as `/web` or `apps/site`; `/` for the whole repository. Letters, digits, `_`, `.`, `-` and `/` only, with no `..` part, at most 255 characters.

## Request

### curl

```bash
curl -X POST https://manage.corefinity.com/api/environments/1311/repositories \
  -H "Authorization: Bearer $COREFINITY_TOKEN" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -d '{
    "branch": "main",
    "connection_id": 4216,
    "pipeline_id": 2210,
    "repo": "northwind-digital/harbourandfinch-trade-theme",
    "root_directory": "/"
  }'
```

### PHP

```php
use Illuminate\Support\Facades\Http;

$response = Http::withToken(env('COREFINITY_TOKEN'))
    ->acceptJson()
    ->post('https://manage.corefinity.com/api/environments/1311/repositories', [
        'branch' => 'main',
        'connection_id' => 4216,
        'pipeline_id' => 2210,
        'repo' => 'northwind-digital/harbourandfinch-trade-theme',
        'root_directory' => '/',
    ]);

$data = $response->json();
```

### Node

```javascript
const res = await fetch('https://manage.corefinity.com/api/environments/1311/repositories', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.COREFINITY_TOKEN}`,
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    branch: 'main',
    connection_id: 4216,
    pipeline_id: 2210,
    repo: 'northwind-digital/harbourandfinch-trade-theme',
    root_directory: '/',
  }),
});

// An answer with no body (204) has nothing to parse.
const text = await res.text();
const data = text && JSON.parse(text);
```

### Python

```python
import os
import requests

res = requests.post(
    "https://manage.corefinity.com/api/environments/1311/repositories",
    headers={
        "Authorization": f"Bearer {os.environ['COREFINITY_TOKEN']}",
        "Accept": "application/json",
    },
    json={
        "branch": "main",
        "connection_id": 4216,
        "pipeline_id": 2210,
        "repo": "northwind-digital/harbourandfinch-trade-theme",
        "root_directory": "/",
    },
)

data = res.json() if res.content else None
```

## Responses

### 201 The repository, as the list shows it.

```json
{
  "environment_repository": {
    "branch": "main",
    "connection_hidden": false,
    "connection_owner": null,
    "environment_deployment_pipeline_id": "2210",
    "id": "3302",
    "provider": "github",
    "repo": "northwind-digital/harbourandfinch-trade-theme"
  }
}
```

### 401 No token, or a token that is not valid.

```json
{
  "message": "Unauthenticated."
}
```

### 403 The token lacks the `write` scope (`scope_required`), your company requires two-factor authentication and you have not set it up, or you may not change the repository (`forbidden`): your role lacks a permission (`permission` names it), the environment's repositories are not yours to change, or the repository was set up with a git account you cannot use.

```json
{
  "code": "company_unavailable",
  "message": "Your company is unavailable.",
  "reason": "company_unavailable"
}
```

### 404 The id does not exist, or it is something you cannot see. Both answer the same body.

```json
{
  "message": "Not found."
}
```

### 422 A field is missing or wrong (`invalid`). `connection_id` is checked first; the other fields are then checked together and `errors` lists each one: a `repo` that is not a repository path of the account's provider, a `branch` that is not a branch name, a `root_directory` that is not a folder path, or any of them longer than 255 characters. A git account you cannot use answers as one that does not exist, on `connection_id`, or, when it is an agency's, on `deployment_connection`. A CoreStack or CoreCluster environment is refused on `environment`.

```json
{
  "code": "invalid",
  "errors": {
    "connection_id": [
      "The selected git account could not be found."
    ]
  },
  "field": "connection_id",
  "message": "The selected git account could not be found.",
  "reason": "invalid"
}
```

### 429 Too many requests (`rate_limited`). Wait for the number of seconds in `Retry-After`, which `retry_after_seconds` repeats.

```json
{
  "code": "rate_limited",
  "message": "Too many requests. Try again in 60 seconds.",
  "reason": "rate_limited",
  "retry_after_seconds": 60
}
```

## If it is refused

| Status | Reason | What to do |
|---|---|---|
| 401 |  | Send a valid token as `Authorization: Bearer <token>`. |
| 403 | `company_unavailable` | Your company is unavailable. |
| 403 | `forbidden` | The person is not allowed to do this. |
| 403 | `scope_required` | Use a token with the scope named in `scope`. |
| 403 | `two_factor_required` | Turn on two-factor on your account, then send again. |
| 404 |  | Check the id. You see only what your account can reach. |
| 422 | `invalid` | Fix the field named in `field`; `errors` says why. |
| 429 | `rate_limited` | Wait for the seconds in `Retry-After`. |

## The same task elsewhere

- Guide: [Deployments and pipelines](https://docs.corefinity.com/guides/corestack-enterprise/deployments-and-pipelines.md)
