# Finish installing the GitHub App (browser callback).

> The dashboard posts what GitHub's redirect carried: `state`, `installationId` and `code`.
> https://koi.dog/docs/api/github/complete-git-hub-installation/

`POST /api/v1/github/installation/complete` · Key: `session only`

The code is exchanged on the server, used once to check that the signed-in GitHub user may reach that installation of this App, and dropped. A state that is not this session's or workspace's is `403 consent_mismatch`; a used one `409 consent_used`; an old one `410 expired`; an installation another workspace holds `409 installation_conflict`. Adds the installation to the workspace's linked accounts; the same installation again updates it (a removed one is linked again). `409 installation_conflict` also when another installation on the same GitHub account is linked here (unlink it first), or when 20 are linked already. The timeline records the key ID, action and outcome. Use the `koidog_session` cookie at `https://app.koi.dog/api/v1` with `Origin: https://app.koi.dog`.

## 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.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `state` | `string` | yes | The single-use state from `POST /github/installation/consent`, as GitHub's redirect returned it. It must belong to this browser session and workspace. 1 to 512 characters. |
| `installationId` | `string` | yes | The installation GitHub's redirect named, as decimal digits. Matches `^\d{1,20}$`. |
| `code` | `string` | yes | The code from GitHub's redirect. The server exchanges it once to check that you may reach the installation, then drops it. 1 to 512 characters. |

## Response.

200 OK · JSON object

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

## Request.

```sh
# The dashboard session only: an API key is refused (403 session_required).
curl -X POST https://app.koi.dog/api/v1/github/installation/complete \
    -H "Cookie: koidog_session=$KOIDOG_SESSION" \
    -H "Origin: https://app.koi.dog" \
    -H "Idempotency-Key: $(uuidgen)" \
    -H "Content-Type: application/json" \
    -d '{
      "state": "<state from the redirect>",
      "installationId": "12345678",
      "code": "<code from the redirect>"
    }'
```

```js
// The dashboard session only: an API key is refused (403 session_required).
const res = await fetch("https://app.koi.dog/api/v1/github/installation/complete", {
  method: "POST",
  headers: {
    Cookie: `koidog_session=${process.env.KOIDOG_SESSION}`,
    Origin: "https://app.koi.dog",
    "Idempotency-Key": crypto.randomUUID(),
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    state: "<state from the redirect>",
    installationId: "12345678",
    code: "<code from the redirect>",
  }),
});
console.log(await res.json());
```

```python
import os
import uuid

import requests

# The dashboard session only: an API key is refused (403 session_required).
r = requests.post(
    "https://app.koi.dog/api/v1/github/installation/complete",
    headers={
        "Cookie": f"koidog_session={os.environ['KOIDOG_SESSION']}",
        "Origin": "https://app.koi.dog",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
        "state": "<state from the redirect>",
        "installationId": "12345678",
        "code": "<code from the redirect>",
    },
)
r.raise_for_status()
print(r.json())
```

## Response example.

```json
{
  "id": "string",
  "connectionId": "string",
  "installationId": "string",
  "accountLogin": "string",
  "repositorySelection": "all",
  "status": "active",
  "observedAt": "string",
  …
}
```
