# Human approval for AI agents (/docs/recipes/agent-approval)

Your agent wants to do something important — refund money, deploy, delete data. Before acting, it calls a human and waits.

## 1. The approval function [#1-the-approval-function]

```js
const CALLHUMAN = "https://api.callhuman.es/v1/calls";
const headers = {
  Authorization: `Bearer ${process.env.CALLHUMAN_API_KEY}`,
  "Content-Type": "application/json",
};

/** Calls the approver and resolves to true (approved) or false. */
export async function askForApproval({ phone, action }) {
  const created = await fetch(CALLHUMAN, {
    method: "POST",
    headers,
    body: JSON.stringify({
      to: phone,
      message: `Your agent wants to ${action}.`,
      options: [
        { label: "Approve", reply: "Approved. Going ahead." },
        { label: "Reject", reply: "Rejected. Nothing will happen." },
      ],
    }),
  }).then((r) => r.json());

  // Poll until the call ends (or use a webhook in production).
  for (;;) {
    await new Promise((r) => setTimeout(r, 5000));
    const call = await fetch(`${CALLHUMAN}/${created.id}`, { headers }).then((r) => r.json());
    if (["answered", "not_answered", "failed"].includes(call.status)) {
      return call.status === "answered" && call.option_label === "Approve";
    }
  }
}
```

## 2. Use it as a tool [#2-use-it-as-a-tool]

```js
const approved = await askForApproval({
  phone: "+34600333444",
  action: "refund 1,240 euros to Acme Ltd for invoice 2231",
});
if (!approved) throw new Error("Refund not approved by a human");
await refund(invoice);
```

<CallTranscript lines={[{"from":"callhuman","kind":"message","text":"Your agent wants to refund 1,240 euros to Acme Ltd for invoice 2231."},{"from":"callhuman","kind":"menu","text":"Press 1 to approve. Press 2 to reject."},{"from":"person","via":"keypress","text":"1"},{"from":"callhuman","kind":"reply","text":"Approved. Going ahead."}]} />

## Tips [#tips]

- **Not answered means no.** Treat `not_answered` as a rejection, or escalate to a second approver.
- Say exactly what will happen, with amounts and names: the person decides only from what they hear.
- For agents that support MCP, a native server is [coming soon](/docs/mcp).