# Accept AP2, ACP and x402

Recognize agents that arrive with an AP2 mandate, an ACP checkout session or an x402 payment, accept the ones that verify, and ask the person behind the agent to approve the exact cart when they don't.

Accept the agents you can recognize, and ask for a person when you can't. Identify reads a payment-protocol object on the request, verifies it, and reports it in the verdict's `protocol` field. Your traffic policy then allows it, or answers with the same `ProvenanceIntent` challenge a checkout step-up uses, so the person approves the exact cart.

**Before you begin:** [run the middleware](https://provenanceauth.com/docs/identify/guides/verify-signed-agents.md) with a route map that names `checkout`, and read [require a human](https://provenanceauth.com/docs/identify/guides/require-a-human.md).

| Protocol | What Identify reads | Verified by |
| --- | --- | --- |
| AP2 (maps to AP2 0.2) | A closed Checkout or Payment Mandate (SD-JWT), in the header you name or the body field `checkout_mandate` / `payment_mandate` | Your AP2 stack checks the SD-JWT signature through a hook; Identify then checks the mandate's content and its Provenance intent claim |
| ACP | A checkout session completion with `provenance_intent` | The intent token, bound to your session's id, total, currency and merchant |
| x402 v2 | `PAYMENT-SIGNATURE` | The intent token in `extensions["com.provenanceauth.intent"]`, bound to the payment you advertised |

Unsigned traffic still runs through your rules, and `protocol: null` is the normal case.

## 1. Turn on protocols in the middleware

```ts
import { Identify } from "@provenanceauth/identify";

const identify = new Identify({
  apiKey: process.env.PROVENANCE_KEY,
  protocols: {
    routes: ["/checkout*"],                 // bodies are read on these routes only, at most 64 KB
    ap2: {
      header: "AP2-Mandate",
      verify: (sdJwt) => myAp2Stack.verifyClosedMandate(sdJwt), // returns the mandate content, or throws
      expected: (req) => myCheckoutFacts(req),                   // needed for a Checkout Mandate
    },
    x402: { expected: (req) => myAdvertisedPayment(req) },
  },
});
```

The AP2 SD-JWT signature is your AP2 stack's to verify, because its trust roots are yours. Without the `verify` hook every AP2 object is `unverified`, never `verified`. A hook that throws marks it `invalid`; one that throws an error with `code: "unavailable"` leaves it `unverified`.

For ACP, Identify checks the token against the order you pass with `orderFor` (`checkout_session_id` is the order id), or against `acp.expected`.

## 2. Read the verdict

```json
{
  "protocol": {
    "name": "ap2",
    "kind": "payment_mandate",
    "status": "verified",
    "amount": 9600,
    "currency": "usd",
    "payee": "quillstack",
    "provenance_intent": true
  }
}
```

| Field | Meaning |
| --- | --- |
| `status` | `verified`, `unverified` (no verifier, no claim to check, or missing facts) or `invalid` (it failed a check) |
| `reason` | Why it isn't `verified`: `ap2_verifier_not_configured`, `no_expected_checkout`, `no_intent`, `payload_hash_mismatch`, … |
| `amount`, `currency`, `payee` | Reported for a verified object only, so a rule never reads an unverified amount |
| `provenance_intent` | The object carries a Provenance intent claim; a valid one also fills `human` |

A protocol object names no agent identity on its own: `class` comes from the Web Bot Auth signature and the detection rules.

## 3. Use the template

`accept_verified_protocols` allows a verified object up to an amount of 50000 in minor units and asks for a person over that, or when an object at checkout is unverified or invalid:

```json
{
  "rules": [
    { "id": "protocol-over-threshold", "when": [{ "fact": "protocol_status", "op": "eq", "value": "verified" }, { "fact": "protocol_amount", "op": "gt", "value": 50000 }], "then": { "action": "require_intent", "binding": "order" } },
    { "id": "accept-verified-protocols", "when": [{ "fact": "protocol_status", "op": "eq", "value": "verified" }], "then": { "action": "allow" } },
    { "id": "protocol-unverified-checkout", "when": [{ "fact": "protocol_status", "op": "in", "value": ["unverified", "invalid"] }, { "fact": "action", "op": "eq", "value": "checkout" }], "then": { "action": "require_intent", "binding": "order" } }
  ]
}
```

Change `50000` to your threshold in minor units. To require a listed payee, add a rule on `protocol_payee` with `not_in_list`.

## 4. Ask for a person on an AP2 checkout

When an AP2 checkout fails a rule and you passed no order of your own, Identify builds the challenge's order from the mandate's checkout: `order_id` is the Checkout's hash, with the amount, currency and payee. The agent asks its person through Authorize, they approve the cart, and the agent replays the same request with `Provenance-Intent`. Identify verifies the mandate again and checks the token against that order; pass `orderFor` and it checks against your own order instead.

x402 payments have no step-up: a payment that doesn't verify is your facilitator's to refuse.

## 5. Use the hosted check

`POST /v1/identify/assessments` takes the same objects. Send the body in `request.body` (at most 64 KB) and your own facts in `protocols`:

**curl**

```bash
curl -s -X PUT "$PROVENANCE_API/v1/identify/settings/site" \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "protocols": { "routes": ["/checkout*"] } }'
```

**Node**

```ts
await fetch(`${process.env.PROVENANCE_API}/v1/identify/settings/site`, {
  method: "PUT",
  headers: { Authorization: `Bearer ${process.env.PROVENANCE_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({ protocols: { routes: ["/checkout*"] } }),
});
```

**Python**

```python
import os, httpx

httpx.put(f"{os.environ['PROVENANCE_API']}/v1/identify/settings/site",
          headers={"Authorization": f"Bearer {os.environ['PROVENANCE_KEY']}"},
          json={"protocols": {"routes": ["/checkout*"]}})
```

For AP2 in the hosted check, set `protocols.ap2.verify_url` to your own AP2 verifier (HTTPS) and `protocols.ap2.secret`. Identify posts `{"object": "ap2_verify_request", "sd_jwt": "…"}` signed with that secret in `Provenance-Signature` (the same scheme as [webhooks](https://provenanceauth.com/docs/integrate/webhooks.md)), and your verifier answers `{"content": {…}}` with the closed mandate it verified, or a 4xx to reject it. The secret is encrypted at rest and only its last four characters are ever returned.

## Errors you might see

| Symptom | Cause | Fix |
| --- | --- | --- |
| `reason: "ap2_verifier_not_configured"` | No `ap2.verify` hook (or hosted `verify_url`) | Plug in your AP2 stack's verifier. |
| `reason: "no_expected_checkout"` | A Checkout Mandate, and no Checkout facts to check it against | Return them from `ap2.expected`, or pass `protocols.ap2_expected` to the hosted check. |
| `reason: "no_expected_order"` | An ACP completion with no order | Pass `orderFor`, or `acp.expected`. |
| `reason: "payload_hash_mismatch"` | The object's amount or payee isn't what the token binds | Treat it as tampered. |
| `protocol: null` on a body you expected | The route isn't in `routes`, or the body is over 64 KB | Add the route; keep protocol bodies small. |

## See also

- [Require a human](https://provenanceauth.com/docs/identify/guides/require-a-human.md)
- [Verify agent checkouts](https://provenanceauth.com/docs/authorize/guides/verify-agent-checkout.md)
- [Write a traffic policy](https://provenanceauth.com/docs/identify/guides/traffic-policy.md)
