# Issue AP2 mandates

Get signed AP2 Checkout and Payment Mandates for your agent after the person approves the exact cart with a passkey, and let any AP2 verifier that lists Provenance as an issuer check them.

*Available on Pro, Growth and Enterprise.*

Use this when your agent pays through AP2 and the merchant or credential provider asks for a signed mandate. AP2 has a role for the screen that shows the person the mandate and signs it: the Trusted Surface. Provenance's approval page is that screen.

The person sees who is paid, each line, the total and the card, and approves with a passkey. Provenance then signs the closed Checkout Mandate (and the closed Payment Mandate when you name the card) as SD-JWTs, maps to AP2 0.2, and your agent hands them to the merchant.

The mandates carry the approval's intent token inside (`com.provenanceauth.intent`), so a merchant that already verifies Provenance tokens checks both. A merchant trusts the mandates by listing Provenance's AP2 keys (`/.well-known/ap2/jwks.json`) as an issuer. That key signs AP2 mandates only.

**Before you begin:** issuing AP2 mandates is off until your deployment turns the AP2 issuer on; until then the routes answer 503 `ap2_issuer_unconfigured`. It needs the Pro plan or higher (a Free account can use it during its trial), a secret key with the `mandate` scope, and the merchant's signed Checkout JWT for the cart.

```bash
export PROVENANCE_API=https://sandbox-api.provenanceauth.com
export PROVENANCE_KEY=prv_test_acct_P9xN2vQ7bT4mW8sYd3cFjLh6gAe1kR5
```

## 1. Make your agent's key

Bind the mandates to your agent's key, so a copied mandate is useless without it. The mandates name the key in `cnf.jwk`, and the agent presents them with a key-binding JWT. Make the key once:

```ts
import { generateHolderKey } from "@provenanceauth/rails/mandates";

const holder = await generateHolderKey();
// holder.publicJwk goes in the request; holder.privateJwk stays with the agent.
```

You can leave the key out: the mandates then work for whoever holds them.

## 2. Ask the person

Send the merchant's Checkout JWT, who is paid, the amount in minor units and the currency to [`POST /v1/mandate/ap2_mandate_requests`](https://provenanceauth.com/docs/api/approvals.md#post-v1-mandate-ap2-mandate-requests). Add `payment_instrument` (an id, a type and a display string, never a card number) to get a Payment Mandate too. Reach the person with `contact` and `delivery: "send"`, or show them `approve_url` yourself:

**curl**

```bash norun
curl -s -X POST $PROVENANCE_API/v1/mandate/ap2_mandate_requests \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "checkout_jwt": "eyJhbGciOiJFUzI1NiJ9.eyJ0b3RhbCI6NDI1MH0.c2ln",
        "payee": { "id": "harborbean", "name": "Harbor Bean", "website": "https://harborbean.example" },
        "amount": 4250, "currency": "USD",
        "payment_instrument": { "id": "pi_card_1", "type": "card", "description": "Visa ···· 4242" },
        "holder_key": { "kty": "EC", "crv": "P-256", "x": "NeVHu4qeD40iEEgSDcTm26Y0xApkjq41T6BuENVl0js", "y": "qS59-GAPcl0bJ-yYFvPJA3omsU9DJFedMw4rou5cXo8" },
        "delivery": "self" }'
```

**Node**

```ts
const request = await (
  await fetch(`${process.env.PROVENANCE_API}/v1/mandate/ap2_mandate_requests`, {
    method: "POST",
    headers: { Authorization: `Bearer ${process.env.PROVENANCE_KEY}`, "Content-Type": "application/json" },
    body: JSON.stringify({
      checkout_jwt: checkoutJwt,
      payee: { id: "harborbean", name: "Harbor Bean", website: "https://harborbean.example" },
      amount: 4250,
      currency: "USD",
      payment_instrument: { id: "pi_card_1", type: "card", description: "Visa ···· 4242" },
      holder_key: holder.publicJwk,
      delivery: "self",
    }),
  })
).json();
// Show request.approve_url to the person.
```

Provenance builds the mandate content itself from these fields and the Checkout JWT. Content your agent serialized is never signed as sent.

## 3. The person approves

The approval page shows a **Signed for your agent** block with exactly what will be signed: who is paid and their website, each line of the Checkout, the total and the card. It also tells the person that the agent gets a signed copy the store can check. They approve with a passkey. A decline, an expiry, or an approval answered without the person's passkey (an earlier approval reused, a confirmation from another issuer, the store's review) issues no mandates.

## 4. Collect the mandates

Read [`GET /v1/mandate/ap2_mandate_requests/{id}`](https://provenanceauth.com/docs/api/approvals.md#get-v1-mandate-ap2-mandate-requests-id) with a DPoP proof from your agent's key. Without the proof, a holder-bound request shows its state only:

```ts
import { signHolderProof } from "@provenanceauth/rails/mandates";

const url = `${process.env.PROVENANCE_API}/v1/mandate/ap2_mandate_requests/${request.id}`;
const dpop = await signHolderProof(holder.privateJwk, { method: "GET", url });
const got = await (await fetch(url, { headers: { Authorization: `Bearer ${process.env.PROVENANCE_KEY}`, DPoP: dpop } })).json();
if (got.ap2_mandates?.status === "issued") {
  const { checkout_mandate, payment_mandate } = got.ap2_mandates;
}
```

Each read signs a fresh copy with the same claims; a copy expires after 10 minutes and never after the intent token inside. The states of `ap2_mandates.status`:

| Status | Means |
| --- | --- |
| `issued` | The mandates are here. |
| `pending_app_signature` | The person approved from the Provenance app and its key is signing; ask again after `retry_after`. |
| `holder_proof_required` | The request is holder-bound and this read had no DPoP proof from that key. |
| `revoked` | The approval's token was revoked. |
| `not_issued` | This approval issues none; `reason` says why. |
| `unavailable` | The issuer is off or couldn't sign; `reason` says which. |

## 5. The merchant verifies

Present each mandate with your agent's key-binding JWT (AP2's `+kb`), naming the merchant's origin. The merchant lists Provenance's AP2 keys and verifies with `verifyAp2UserMandate`:

```ts
import { verifyAp2UserMandate } from "@provenanceauth/rails/ap2";

const jwks = await (await fetch(`${process.env.PROVENANCE_API}/.well-known/ap2/jwks.json`)).json();
const result = verifyAp2UserMandate(presentedMandate, {
  issuers: [{ name: "Provenance", jwks }],
  expected: { origin: "https://harborbean.example", checkout_jwt: myCheckoutJwt, payee_id: "harborbean", amount: 4250, currency: "USD" },
});
// result.accepted, result.issuer, result.signer ("issuer" or "app")
```

In Python use `provenanceauth_verify.ap2.verify_ap2_user_mandate`, and in Go `VerifyAp2UserMandate`, with the same issuers and cart. `verifyAp2UserMandateOnline` also checks the intent token inside against Provenance's revocation status. A store already set up for [AP2 mandates from other issuers](https://provenanceauth.com/docs/authorize/guides/agent-payments.md) adds Provenance's JWKS URL to `external_confirmations.ap2_issuers` on its agent policy.

## Mandates on a store's own cart approval

A store that creates a cart approval itself adds `ap2: { "issue": true }` (and optionally `payment_instrument` and `ap2.holder_key`) to [`POST /v1/authorize/checkout_approvals`](https://provenanceauth.com/docs/api/checkout-approvals.md#post-v1-authorize-checkout-approvals), with its `checkout_jwt`. The store's read of the approval and the keyless `status_url` then carry `ap2_mandates` beside the intent token. The keyless read of a holder-bound approval needs the agent's DPoP proof over that request.

## Open mandates from a grant

For an agent that pays without asking each time (AP2's Human Not Present mode), issue the person's [grant](https://provenanceauth.com/docs/mandate/concepts/grants.md) as open mandates. The grant must be [bound to the agent's key](https://provenanceauth.com/docs/mandate/guides/bind-a-grant-to-its-holder.md). Send its token and holder key to [`POST /v1/mandate/grants/{id}/ap2_open_mandates`](https://provenanceauth.com/docs/api/grants.md#post-v1-mandate-grants-id-ap2-open-mandates), with a DPoP proof from the holder:

```ts
const url = `${process.env.PROVENANCE_API}/v1/mandate/grants/${grant.id}/ap2_open_mandates`;
const dpop = await signHolderProof(holder.privateJwk, { method: "POST", url, token: grant.mandate_token });
const open = await (
  await fetch(url, {
    method: "POST",
    headers: { Authorization: `Bearer ${process.env.PROVENANCE_KEY}`, "Content-Type": "application/json", DPoP: dpop },
    body: JSON.stringify({
      mandate_token: grant.mandate_token,
      holder_key: holder.publicJwk,
      currency: "USD",
      payees: [{ id: "harborbean", name: "Harbor Bean", website: "https://harborbean.example" }],
    }),
  })
).json();
```

The open Payment Mandate carries the grant's per-payment limit (`payment.amount_range`) and allowed merchants (`payment.allowed_payees`), and the grant token unchanged. With allowed merchants you also get an open Checkout Mandate. Both end with the grant, and revoking the grant withdraws them. A monthly budget has no AP2 counterpart: `not_mapped` says so, and Provenance still enforces it at verify.

For each purchase, the agent mints an intent token under the grant, then closes the open mandate for that cart with its own key:

```ts
import { closeAp2OpenMandate } from "@provenanceauth/rails/ap2";

const chain = await closeAp2OpenMandate(open.open_payment_mandate, {
  holderKey: holder.privateJwk,
  aud: "https://harborbean.example",
  content: { vct: "mandate.payment.1", transaction_id: checkoutHash, payee, payment_amount: { amount: 4250, currency: "USD" }, payment_instrument, "com.provenanceauth.intent": { token: intentToken } },
});
```

The merchant verifies the chain with `verifyAp2UserMandate`, as in step 5. A cart outside the grant's limits returns `unresolved_constraint` with `hand_back: true`: ask the person to approve that cart. The spend counts when the merchant verifies the intent token inside, through the usual verify; checking the chain spends nothing.

## When the person approves in the Provenance app

When the person opens the request from the Provenance app, the app's device key signs the mandates after face or fingerprint, instead of Provenance's issuer key. The mandate carries `com.provenanceauth.signer: "app"` and Provenance's attestation of the device key (`com.provenanceauth.app_key`).

Verifiers check the attestation against the same AP2 JWKS, then the mandate against the attested key: nothing else changes for the merchant, and no device id is in the mandate. If the app can't sign within two minutes, Provenance's key signs and `fallback_reason` says why.

## Withdraw the mandates

Revoke the approval's intent token, or its session (the approval id), with [`POST /v1/mandate/revocations`](https://provenanceauth.com/docs/api/intent-tokens.md#post-v1-mandate-revocations). AP2 has no status list, so verifiers learn of it through Provenance's revocation status; reads of the request then show `ap2_mandates.status: "revoked"`.

## Errors you might see

| Symptom | Cause | Fix |
| --- | --- | --- |
| `503` [`ap2_issuer_unconfigured`](https://provenanceauth.com/docs/platform/errors.md#ap2_issuer_unconfigured) | The deployment hasn't turned the AP2 issuer on | Use the intent token and the AP2 content (`ap2`) on the approval. |
| `400` `parameter_invalid` on `checkout_jwt` | AP2 binds the Checkout by its hash, so the merchant's signed Checkout JWT is required | Ask the merchant for its Checkout JWT. |
| `400` `parameter_invalid` on `ap2.holder_key` | The key isn't a P-256 public JWK, or carries its private part | Send `holder.publicJwk` only. |
| `ap2_mandates.status: "holder_proof_required"` | The request is holder-bound and the read had no DPoP proof from that key | Sign a proof for this exact request. |
| `ap2_mandates.status: "not_issued"`, `reason: "reused"` | An earlier approval of the same cart answered the request, without a passkey this time | Ask again for a new cart, or use the intent token. |
| `issuer_not_trusted` at the merchant | The merchant doesn't list Provenance's AP2 JWKS | List `/.well-known/ap2/jwks.json` as an issuer. |
| `binding_mismatch` at the merchant | The mandate is holder-bound and the presentation has no key-binding JWT, or names another origin | Present it with your agent's key-binding JWT for that merchant. |
| `409` `ap2_mandates_not_issuable` on open mandates | The grant isn't bound to a holder key, or isn't active | Grant with `holder_key`. |
| `unresolved_constraint` on a chain | The closed mandate is over the grant's limit or to another merchant | Ask the person to approve that cart. |

## See also

- [Agent payments](https://provenanceauth.com/docs/authorize/guides/agent-payments.md), for AP2 content and verification at the merchant
- [Bind a grant to its holder](https://provenanceauth.com/docs/mandate/guides/bind-a-grant-to-its-holder.md)
- [Show the full cart](https://provenanceauth.com/docs/authorize/guides/show-the-full-cart.md)
