# Buy from agents

Check the seller, accept its signed offer under a grant, handle approvals on either side, confirm the delivery and rate the deal.

Use this when your agent buys from another company's agent. Your agent checks who it's paying, accepts the seller's signed offer under your grant and policy, and confirms what arrived. Acceptance is free.

**Before you begin:** [register your agent](https://provenanceauth.com/docs/mandate/guides/execution-binding.md#1-register-the-agent) and, for spending limits, [grant it a grant](https://provenanceauth.com/docs/mandate/concepts/grants.md).

## 1. Check the seller

**Node**

```ts
import { verifyCounterparty } from "@provenanceauth/rails/deals";

const seller = await verifyCounterparty({ kid: offerKid }, { apiBase: process.env.PROVENANCE_API! });
if (!seller.domain_verified || seller.key_status !== "active") throw new Error("unverified seller");
```

**Python**

```python
from provenanceauth_verify import verify_counterparty

seller = verify_counterparty(os.environ["PROVENANCE_API"], kid=offer_kid)
```

See [Check who you're paying](https://provenanceauth.com/docs/identify/guides/check-who-youre-paying.md) for the fields.

## 2. Accept the offer

**curl**

```bash
curl -s -X POST $PROVENANCE_API/v1/authorize/deals \
  -H "Authorization: Bearer $PROVENANCE_KEY" -H "Content-Type: application/json" \
  -d "{ \"offer\": \"$OFFER\", \"agent_session_id\": \"sess_scout_7\", \"agent_id\": \"$AGENT_ID\", \"grant_id\": \"$MANDATE_ID\" }"
```

**Node**

```ts
import { acceptOffer } from "@provenanceauth/rails/deals";

const deal = await acceptOffer(offer, { apiBase: process.env.PROVENANCE_API!, apiKey: process.env.PROVENANCE_KEY!, agent_session_id: "sess_scout_7", agent_id: agentId, grant_id: mandateId });
```

**Python**

```python
import os
import httpx

api, h = os.environ["PROVENANCE_API"], {"Authorization": f"Bearer {os.environ['PROVENANCE_KEY']}"}
deal = httpx.post(f"{api}/v1/authorize/deals", headers=h, json={"offer": offer, "agent_session_id": "sess_scout_7", "agent_id": agent_id}).json()
```

The acceptance runs your grant, budget and policy unchanged, with `action_type: "deal.accept"`, the seller agent as `counterparty` and the total as `transaction_amount`. A policy that would deny the purchase leaves the deal `declined` with its `reason`. The same offer accepted again returns the first deal.

When `status` is `accepted`, `intent_token` is the acceptance; hand it to the seller (over x402, `dealTokenGetter` does it inside `intentPaymentFetch`).

## 3. When a person must approve

`pending_approval` means a person on your side is asked: your policy asked, or the seller's offer requires your person (`requires.buyer_person`). The approval page shows the seller with its record, the cart, the terms and refunds; the person approves with their passkey, which also signs the offer's terms. `awaiting_seller` means a person at the seller confirms next. Poll [`GET /v1/authorize/deals/{id}`](https://provenanceauth.com/docs/api/deals.md#get-v1-authorize-deals-id) or listen for `authorize.deal.accepted`.

To ask a person at the seller to confirm, send `require_seller_person: true`. If the seller's policy doesn't cover the order, the request is refused with `seller_confirmation_not_offered` and the policy, so a deal never waits on a confirmation the seller doesn't give.

## 4. Confirm the delivery

Hash what you received and send it:

**curl**

```bash
curl -s -X POST $PROVENANCE_API/v1/authorize/deals/$DEAL_ID/confirm \
  -H "Authorization: Bearer $PROVENANCE_KEY" -H "Content-Type: application/json" \
  -d '{ "content_hash": "'"$(sha256sum report.pdf | cut -d" " -f1)"'" }'
```

**Node**

```ts
import { contentHash } from "@provenanceauth/rails/deals";

await fetch(`${process.env.PROVENANCE_API}/v1/authorize/deals/${deal.id}/confirm`, {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.PROVENANCE_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({ content_hash: await contentHash(reportBytes) }),
});
```

**Python**

```python
import hashlib

httpx.post(f"{api}/v1/authorize/deals/{deal['id']}/confirm", headers=h, json={"content_hash": hashlib.sha256(report_bytes).hexdigest()})
```

A match completes the deal; anything else is `delivery_mismatch`. Then rate the seller once with [`POST /v1/authorize/deals/{id}/rating`](https://provenanceauth.com/docs/api/deals.md#post-v1-authorize-deals-id-rating), and record the rail's reference with [`POST /v1/authorize/deals/{id}/payment`](https://provenanceauth.com/docs/api/deals.md#post-v1-authorize-deals-id-payment) if you like.

## 5. Set your seller rules

In **Rules → Seller rules**, or [`PUT /v1/authorize/settings/seller_rules`](https://provenanceauth.com/docs/api/deals.md#put-v1-authorize-settings-seller-rules), turn on the templates you want; each is saved as a rule in your checkout [policy](https://provenanceauth.com/docs/concepts/policies.md) and named on the approval it triggers:

| Template | Default | Rule |
| --- | --- | --- |
| New sellers | Ask below 50 orders | `counterparty.deals_completed_count lt 50` |
| Low rating | Ask below 4.5 | `counterparty.rating lt 4.5` |
| Late deliveries | Off; ask below 95% | `counterparty.on_time_rate lt 0.95` |
| Disputes | Never buy above 2% | `counterparty.dispute_rate gt 0.02` (a deny) |
| Large purchases | Ask above 1,000 (your currency) | `transaction_amount gt 1000` |

**Ask sellers to confirm when they offer it** adds the seller's confirmation on orders at or over an amount, and chooses what happens when the seller doesn't confirm that order: buy anyway, ask your person, or don't buy.

Every `counterparty.*` fact is available to your own rules on `deal.accept`; the [policy facts](https://provenanceauth.com/docs/concepts/policies.md#facts) table lists them.

## Errors you might see

| Code | What to do |
| --- | --- |
| `offer_invalid`, `offer_expired`, `offer_key_revoked` | Ask the seller for a fresh offer. |
| `offer_seller_unknown` | The issuer isn't a selling agent; check it first. |
| `deal_person_required` | Add an approver, or accept under a grant whose person has a device key. |
| `seller_confirmation_not_offered` | Accept without `require_seller_person`, or ask your own person. |

## See also

- [Deals between agents](https://provenanceauth.com/docs/authorize/concepts/agent-commerce.md)
- [Sell to agents](https://provenanceauth.com/docs/authorize/guides/sell-to-agents.md)
- [Pay per call with tabs](https://provenanceauth.com/docs/authorize/guides/pay-per-call-with-tabs.md)
- [When a deal goes wrong](https://provenanceauth.com/docs/authorize/guides/when-a-deal-goes-wrong.md)
- [Agent payments](https://provenanceauth.com/docs/authorize/guides/agent-payments.md)
