# Combine a direct crew's task branches into one branch on the server.

> Merges the chosen task branches (default: every `done` task) in dependency order, then task order, onto a new branch `koidog/<crew>/combined-<n>` in Koidog's mirror, starting from the default branch's tip.
> https://koi.dog/docs/api/crews/integrate-crew/

`POST /api/v1/crews/{id}/integrate` · Scope: `crews`

No agent needs git credentials: the server holds every branch. A conflicting branch gets a resolver agent (`resolveConflicts`, default true) or is left out and reported. Then the project's checks run on the result on a machine (a check agent with no tools); `state: ready` with `checks.passed: true` is the evidence. A GitHub project's result is pushed and gets one pull request against the default branch (`openPr`, default true); never a merge into it. A project with no remote repository downloads the result from `downloads`. One integration per crew at a time (`409 integration_open`). A managed crew on a GitHub project delivers its own pull request and is refused (`409 managed_crew`); on a project with no repository its integration branch is taken as it is (every merged task `already_merged`) and the project's checks run on that commit, as for a direct crew, before the downloads (refused with `409 conflict` until the crew has completed: one a step stopped is resumed or retried first). Reopens a crew that had ended while the checks or a resolver run. The timeline records the key ID, action and outcome.

## Path parameters.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | yes | The crew's id. |

## Headers.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | `string` | no | Retry the same request with the same key. [Retry rules](https://koi.dog/docs/api/#retries). |

## Body.

An omitted body is read as `{}`.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `taskIds` | `array` | no | The tasks whose branches to combine. Default: every task of the crew that finished with evidence (`done`). Merged in dependency order, then the crew's task order. 1 to 100 items. |
| `resolveConflicts` | `boolean` | no | When a branch conflicts, start a resolver agent for it (the crew's builder connection, on a machine). False: leave it out and go on. Default true. Default `true`. |
| `openPr` | `boolean` | no | Push the result and open one pull request against the default branch (GitHub projects only). Default true for a GitHub project. |

## Response.

202 Accepted · JSON object. Follow progress on the timeline.

- **422 validation_failed**: Each schema error appears in `details`.

## Request.

```sh
curl -X POST "https://api.koi.dog/api/v1/crews/$ID/integrate" \
    -H "Authorization: Bearer $KOIDOG_API_KEY" \
    -H "Idempotency-Key: $(uuidgen)"
```

```js
const res = await fetch(`https://api.koi.dog/api/v1/crews/${process.env.ID}/integrate`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.KOIDOG_API_KEY}`,
    "Idempotency-Key": crypto.randomUUID(),
  },
});
console.log(await res.json());
```

```python
import os
import uuid

import requests

r = requests.post(
    f"https://api.koi.dog/api/v1/crews/{os.environ['ID']}/integrate",
    headers={
        "Authorization": f"Bearer {os.environ['KOIDOG_API_KEY']}",
        "Idempotency-Key": str(uuid.uuid4()),
    },
)
r.raise_for_status()
print(r.json())
```

## Response example.

```json
{
  "id": "string",
  "crewId": "string",
  "projectId": "string",
  "state": "merging",
  "stateReason": "string",
  "branch": "string",
  "baseSha": "string",
  "tipSha": "string",
  "branches": [],
  "checks": {
    "declared": false,
    "passed": false,
    "checkedSha": "string",
    "taskId": "string",
    "runId": "string",
    "results": []
  },
  "options": {
    "resolveConflicts": false,
    "openPr": false
  },
  "pullRequest": {
  },
  "by": {
  },
  "createdAt": "string",
  …
}
```
