# Approve and run a plan

Source: https://docs.coderhelm.com/api/reference/plans/approve-and-execute-plan

`POST /api/plans/{plan_id}/approve-and-execute`

Approves every `draft` task of the plan and runs the plan: each approved task becomes a GitHub issue or Jira ticket and is queued as a run, in task order. The plan becomes `executing`. Rejected tasks are skipped. Answers `400` when no task is left to run, so calling it again after a successful call does not run anything twice.

## Authentication

A team API key: `Authorization: Bearer chk_...`. The key needs the `plans` scope. See [Authentication](https://docs.coderhelm.com/api/authentication).

## Parameters

### Path parameters

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

## Responses

### 200

The plan is running.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `status` | string | yes | Always `executing`. |
| `tasks_queued` | integer | yes | How many tasks were queued. |

Headers: `RateLimit-Limit`, `RateLimit-Remaining`, `RateLimit-Reset`.

### 400

The id is not a plan id, or the plan has no draft or approved tasks.

### 401

The key is missing, malformed, revoked or expired, or the person who made it left the team.

### 403

The key lacks the scope this endpoint needs (`missing_scope`).

### 429

Over the key's rate limit (`rate_limited`), or, for endpoints that start work, over the team's monthly token limit (`token_limit_reached`).

Headers: `RateLimit-Limit`, `RateLimit-Remaining`, `RateLimit-Reset`, `Retry-After`.

### 500

Something failed on CoderHelm's side. Safe to retry reads.

## Example request

cURL:

```bash
curl -X POST 'https://api.coderhelm.com/api/plans/01JB8V0K3M5P7R9T1W3Y5A7C9E/approve-and-execute' \
  -H "Authorization: Bearer $CODERHELM_API_KEY"
```

TypeScript:

```ts
const res = await fetch("https://api.coderhelm.com/api/plans/01JB8V0K3M5P7R9T1W3Y5A7C9E/approve-and-execute", {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.CODERHELM_API_KEY}`,
  },
});
if (!res.ok) {
  const { error, code } = await res.json();
  throw new Error(`${res.status} ${code}: ${error}`);
}
const data = await res.json();
```

Python:

```python
import os
import requests

headers = {
    "Authorization": f"Bearer {os.environ['CODERHELM_API_KEY']}",
}
res = requests.request("POST", "https://api.coderhelm.com/api/plans/01JB8V0K3M5P7R9T1W3Y5A7C9E/approve-and-execute", headers=headers)
if not res.ok:
    raise RuntimeError(f"{res.status_code}: {res.json().get('error')}")
data = res.json()
```

## Example response

```json
{
  "status": "executing",
  "tasks_queued": 3
}
```
