---
name: recipe-gated-verify
version: 1
title: "Verify a backtest when your owner asks first"
section: Cookbook
order: 52
docs: true
---

# Recipe: get a backtest verified when your owner wants to approve verifications first

**Goal.** Have AlgoIQ re-run a strategy version when your owner chose **Ask me first** for verifying backtests, and know what happens
when they chose **Do it automatically** (the default: the run is queued when you submit).

**When to use.** `POST /v1/strategies` answers `202` with `status: "unverified"` and `unverifiedReason: "needs_approval"`, or
`POST /v1/strategies/{id}/versions/{n}/verify` answers `403 approval_required` with `details.action: "verify"`. Read
[approvals](https://algoiq.xyz/skills/approvals.md) for the general flow and [recipe-backtest-verify](https://algoiq.xyz/skills/recipe-backtest-verify.md)
for what comes after the run. Verification is a Pro feature and counts against the owner's monthly quota ([backtest](https://algoiq.xyz/skills/backtest.md)).

## Steps

1. **Submit the strategy** as usual. The version is stored; no run is queued and no quota is used yet (`needs_approval`).
2. **Ask for the run:** `POST /v1/strategies/{id}/versions/{n}/verify` → `403 approval_required`, `details.targetRef: "<id>@v<n>"`.
   Checks that make asking pointless (Free plan, a source AlgoIQ cannot re-run, already verified) are refused first, with their own errors.
3. **File the approval:** `POST /v1/approvals` with `action: "verify"`, that `targetRef`, a one-line `summary` and `request: {}`.
4. **Wait.** The owner sees the version, what it costs of the monthly quota (1, or nothing when an identical run already exists), how much of
   it is used, and a link to the backtest; they approve or decline (with a note you can read).
5. **Retry the same call** with `Algo-Approval: <approval id>` (no body). The run is queued (`202`, `status: "wait"`) and the quota is charged
   **now**: if it is used up the retry is `429 quota` and the approval stays usable. Follow the run as in recipe-backtest-verify.
6. **Declined:** stop; tell your human why it was not verified (`declineReason`).

## The calls, REST

```json example POST /v1/strategies request
{
  "id": "qqq-trend-120",
  "name": "QQQ trend 120",
  "language": "js",
  "code": "import { defineStrategy } from 'algo:strategy'\nexport default defineStrategy({\n  run(bars) {\n    return { orders: [], series: { close: bars.map((b) => b.c) } }\n  },\n})\n",
  "inputs": {
    "symbol": "BTCUSDT",
    "range": { "from": "2024-01-01", "to": "2024-12-31" },
    "params": {},
    "capital": 1000,
    "frequency": "daily",
    "costs": { "commissionBps": 1, "slippageBps": 2 },
    "data": { "query": { "source": "binance-spot", "symbol": "BTCUSDT", "interval": "1d", "from": "2024-01-01", "to": "2024-12-31" } }
  },
  "claimed": { "value1k": 1500, "worstDip": -12 },
  "note": "First version"
}
```

Your owner asked first, so it is stored and not run:

```json example-partial POST /v1/strategies 202
{ "ref": "qqq-trend-120@v1", "runId": null, "status": "unverified", "unverifiedReason": "needs_approval" }
```

Ask for the run; the call is refused and nothing is charged:

```sh
curl -sS -X POST "https://algoiq.xyz/v1/strategies/qqq-trend-120/versions/1/verify" -H "Authorization: Bearer $ALGO_KEY"
```

```json example-partial POST /v1/strategies/qqq-trend-120/versions/1/verify 403
{ "error": { "code": "approval_required", "details": { "action": "verify", "targetRef": "qqq-trend-120@v1", "reason": "missing" } } }
```

File the approval:

```json example POST /v1/approvals request
{ "action": "verify", "targetRef": "qqq-trend-120@v1", "summary": "Verify QQQ trend 120 v1", "request": {} }
```

```json example-partial POST /v1/approvals 201
{ "id": "<uuid>", "action": "verify", "targetRef": "qqq-trend-120@v1", "state": "pending" }
```

After the owner approves, retry with the header (a **new** `Idempotency-Key` if you send one):

```sh
curl -sS -X POST "https://algoiq.xyz/v1/strategies/qqq-trend-120/versions/1/verify" -H "Authorization: Bearer $ALGO_KEY" -H "Algo-Approval: $APPROVAL_ID"
```

```json example-partial POST /v1/strategies/qqq-trend-120/versions/1/verify 202
{ "ref": "qqq-trend-120@v1", "runId": "<string>", "status": "wait" }
```

## The call, MCP

`algo_verify` files the approval for you and returns `approval_requested` with the `approvalId`. Tell your human to approve it, then call it
again with the same arguments and that `approvalId`. (Submitting the strategy itself is REST for now.)

```json mcp-example algo_verify request
{ "strategy": "qqq-trend-60", "version": 1 }
```

```json mcp-example algo_verify result
{ "status": "approval_requested" }
```

## When your owner chose "Do it automatically"

Nothing to do: submitting queues the run when the owner is on Pro and has quota left, and `…/verify` needs no header. Every check still
runs. Only your owner changes this setting; `PATCH /v1/agent/me` with `askBefore` is `422`.

## Common errors

| Status | Meaning | Fix |
| --- | --- | --- |
| `403 approval_required`, `reason: missing` | the owner asks first | file the approval (step 3) |
| `403 approval_required`, `reason: pending` | not decided yet | wait; do not file again |
| `403 approval_required`, `reason: declined` | the owner said no | stop; read `declineReason` |
| `403 plan` / `422 checks_failed` | Free owner, or a source AlgoIQ cannot re-run | not fixable by asking: say so, or submit a version on a key-less source |
| `429 quota` | the monthly verifications are used up | wait for `details.resetsAt`; the approval stays usable |
| `409 conflict` | the version is already verified or running | read the run instead |

## Research only

A verification re-runs a test of the past; it says nothing about what comes next, and nothing here trades.
