# Add approvals

Pause a workflow at a `waitForApproval` step: its [run](/docs/concepts/runs.md) waits until a person approves, then carries on.

## Before you start

You need a workflow. See [Build a workflow](/docs/orchestration/workflows.md).

## Add an approval

**Studio**

1. In your project, open **Building blocks** > **Workflows**.

2. Click **Create workflow**.

3. Enter `approve-refund` as the **Name**.

4. Replace the **Source code** with a workflow that waits for approval before it issues a refund:

   ```ts
   import { tool } from "veryfront/tool";
   import { step, waitForApproval, workflow } from "veryfront/workflow";

   const issueRefund = tool({
     id: "issue-refund",
     description: "Issue a refund for an amount.",
     inputSchema: {
       type: "object",
       properties: { amount: { type: "number" } },
       required: ["amount"],
       additionalProperties: false,
     },
     execute: async ({ amount }) => ({ refunded: amount, status: "issued" }),
   });

   export default workflow({
     id: "approve-refund",
     description: "Issue a refund once a person approves it.",
     steps: [
       waitForApproval("manager-approval", {
         message: "Approve this refund?",
         timeout: "24h",
       }),
       step("issue-refund", {
         tool: issueRefund,
         input: (ctx) => ({ amount: (ctx.input as { amount: number }).amount }),
       }),
     ],
   });
   ```

5. Click **Create workflow**.

6. Open the **⋯** menu.

7. Click **Run workflow**.

8. Paste the **Payload**:

   ```json
   { "amount": 25 }
   ```

9. Click **Create run**. The run shows **Waiting**, with `manager-approval` under **Pending approvals**.

10. Ask a teammate who can run workflows to open **Orchestration** > **Runs**.

11. They click the run.

12. They click **Approve**.

13. Open the run again. You should now see it **Completed**, with `"refunded": 25` under **Output**.

### Troubleshooting

**If Studio says "You started this run, so someone else has to approve it"**, the person who starts a run can't approve it. Ask a teammate to approve it.

**API**

1. Set `PROJECT` to your project's ID ([Find your IDs](/docs/authentication/api-keys.md#find-your-ids)): `export PROJECT=<id>`.
2. Save the **Studio** tab's step 4 code as `approve-refund.ts`.
3. Write it to `workflows/approve-refund.ts` with your [API key](/docs/authentication/api-keys.md). `jq` sends it as `content`; `%2F` is the `/`:

```bash
jq -n --rawfile content approve-refund.ts '{content: $content}' | \
  curl -X PUT https://api.veryfront.com/projects/$PROJECT/files/workflows%2Fapprove-refund.ts \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @-
```

4. Get the project. A run takes its `id`, not the slug:

```bash
curl https://api.veryfront.com/projects/$PROJECT \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

5. Set `PROJECT_ID` to the `id` in the response: `export PROJECT_ID=<id>`.
6. Run the workflow with the payload:

```bash
curl -X POST https://api.veryfront.com/runs \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: approve-refund-1" \
  -d '{
    "project_id": "'$PROJECT_ID'",
    "target": { "type": "workflow", "id": "approve-refund" },
    "input": { "amount": 25 }
  }'
```

7. Set `RUN_ID` to the `run_id` in the response: `export RUN_ID=<run_id>`.
8. Read the run until `status` is `waiting`. You should now see `manager-approval` in `control.waiting.node_ids`:

```bash
curl https://api.veryfront.com/runs/$RUN_ID \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

9. A teammate approves it in Studio (**Studio** tab, steps 10 to 12). An API key can't.

### Try it in a sandbox

```playground
step: Set PROJECT | export PROJECT=$VERYFRONT_PROJECT_ID
step: Save the Studio tab's step 4 code as approve-refund.ts | printf '%s\n' 'import { tool } from "veryfront/tool";' 'import { step, waitForApproval, workflow } from "veryfront/workflow";' '' 'const issueRefund = tool({' '  id: "issue-refund",' '  description: "Issue a refund for an amount.",' '  inputSchema: {' '    type: "object",' '    properties: { amount: { type: "number" } },' '    required: ["amount"],' '    additionalProperties: false,' '  },' '  execute: async ({ amount }) => ({ refunded: amount, status: "issued" }),' '});' '' 'export default workflow({' '  id: "approve-refund",' '  description: "Issue a refund once a person approves it.",' '  steps: [' '    waitForApproval("manager-approval", {' '      message: "Approve this refund?",' '      timeout: "24h",' '    }),' '    step("issue-refund", {' '      tool: issueRefund,' '      input: (ctx) => ({ amount: (ctx.input as { amount: number }).amount }),' '    }),' '  ],' '});' > approve-refund.ts
step: Write it to workflows/approve-refund.ts | jq -n --rawfile content approve-refund.ts '{content: $content}' | curl -X PUT https://api.veryfront.com/projects/$PROJECT/files/workflows%2Fapprove-refund.ts -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" --data-binary @-
step: Get the project | curl https://api.veryfront.com/projects/$PROJECT -H "Authorization: Bearer $VERYFRONT_API_KEY"
step: Set PROJECT_ID | export PROJECT_ID=$(curl -s https://api.veryfront.com/projects/$PROJECT -H "Authorization: Bearer $VERYFRONT_API_KEY" | jq -r .id)
step: Run the workflow | curl -X POST https://api.veryfront.com/runs -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -H "Idempotency-Key: approve-refund-1" -d '{ "project_id": "'$PROJECT_ID'", "target": { "type": "workflow", "id": "approve-refund" }, "input": { "amount": 25 } }' | tee run.json
step: Set RUN_ID | export RUN_ID=$(jq -r .run_id run.json)
step: Wait until it is waiting | until [ "$(curl -s https://api.veryfront.com/runs/$RUN_ID -H "Authorization: Bearer $VERYFRONT_API_KEY" | jq -r .status)" = waiting ]; do sleep 3; done
check: curl https://api.veryfront.com/runs/$RUN_ID -H "Authorization: Bearer $VERYFRONT_API_KEY"
expect: "node_ids":["manager-approval"]
```

## With MCP

1. Call `veryfront__create_file` with `"project_reference": "my-project"`, `"path": "workflows/approve-refund.ts"` and the workflow's source from the Studio tab as `content`.
2. Call `veryfront__get_project` with `"project_reference": "my-project"`. A run takes the project's `id`, not the slug.
3. Call `veryfront__create_run` with the `id` as `project_id`:

```json
{
  "input": {
    "project_id": "<id>",
    "target": { "type": "workflow", "id": "approve-refund" },
    "input": { "amount": 25 }
  },
  "idempotency_key": "approve-refund-1"
}
```

4. Call `veryfront__get_run` with the returned `run_id` until `status` is `waiting`. It worked when `control.waiting.node_ids` holds `manager-approval`.
5. A teammate approves the run in Studio. `veryfront__resume_run` with an API key answers "Approvals must be decided by a signed-in person".

## Next steps

* [Debug a run](/docs/observability/debug.md)
* [Set roles](/docs/account/roles.md)
