# Merge a pull request.

> An approval action: the session, or a key the founder gave `approve`.
> https://koi.dog/docs/api/pull-requests/merge-pull-request/

`POST /api/v1/pull-requests/{id}/merge` · Scope: `approve`

`headSha` is the head commit that was reviewed; the merge is refused if the pull request has moved since. Reads GitHub first and answers `409 not_mergeable` (the reasons in `details`) unless it is open, not a draft, mergeable and its checks are neither failing nor pending. Merges with a merge commit and records the approval and who gave it. Idempotent: an already merged pull request answers `merged: true, alreadyMerged: true`. When an API key calls it, the timeline records who did what and the outcome. No query parameters.

## Path parameters.

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

## Headers.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | `string` | no | Makes a retry safe: the same key with the same request returns the first answer and does nothing else. |

## Body.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `approve` | `true` | yes | The founder's explicit approval. |
| `headSha` | `string` | yes | The head commit the founder reviewed; the merge is refused if the PR has moved since. Matches `^[0-9a-f]{40}$`. |

## Response.

200 OK: a JSON object. The example has placeholder values.

- **422 validation_failed**: The body does not match its schema; each problem is in `details`.

## Request.

```sh
curl -X POST "https://app.koi.dog/api/v1/pull-requests/$ID/merge" \
    -H "Authorization: Bearer $KOIDOG_API_KEY" \
    -H "Idempotency-Key: $(uuidgen)" \
    -H "Content-Type: application/json" \
    -d '{
      "approve": true,
      "headSha": "<head commit sha>"
    }'
```

```js
const res = await fetch(`https://app.koi.dog/api/v1/pull-requests/${process.env.ID}/merge`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.KOIDOG_API_KEY}`,
    "Idempotency-Key": crypto.randomUUID(),
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    approve: true,
    headSha: "<head commit sha>",
  }),
});
console.log(await res.json());
```

```python
import os
import uuid

import requests

r = requests.post(
    f"https://app.koi.dog/api/v1/pull-requests/{os.environ['ID']}/merge",
    headers={
        "Authorization": f"Bearer {os.environ['KOIDOG_API_KEY']}",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
        "approve": True,
        "headSha": "<head commit sha>",
    },
)
r.raise_for_status()
print(r.json())
```

## Response example.

```json
{
  "merged": false,
  "alreadyMerged": false,
  "pr": {
    "id": "string",
    "url": "string",
    "repo": "string",
    "number": 0,
    "state": "unknown",
    "draft": false,
    …
  }
}
```
