# Connect an agent

Ask a person for a network grant for your agent, collect the token when they grant it, and buy under it at any store that accepts Provenance.

Use a [network grant](https://provenanceauth.com/docs/mandate/concepts/network-grants.md) when your agent shops for people at many stores. The person grants your agent one set of limits, and your agent buys inside them without asking each time.

**Before you begin:** register the agent and give it an Identify signing key. Network grants are for verified agents only.

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

## 1. Register the agent and its signing key

**curl**

```bash
export AGENT_ID=$(curl -s -X POST $PROVENANCE_API/v1/mandate/agents -H "Authorization: Bearer $PROVENANCE_KEY" -H "Content-Type: application/json" -d '{ "name": "cartwise", "category": "shopping_agent" }' | jq -r .id)
curl -s -X POST $PROVENANCE_API/v1/identify/agents/$AGENT_ID/signing_keys \
  -H "Authorization: Bearer $PROVENANCE_KEY" -H "Content-Type: application/json" \
  -d '{ "jwk": { "kty": "OKP", "crv": "Ed25519", "x": "G_STrQBvTD7Bi8OjlNnLcKPPiPp03Vh0sJKTMG7QzFY" } }'
```

**Node**

```ts
const headers = { Authorization: `Bearer ${process.env.PROVENANCE_KEY}`, "Content-Type": "application/json" };
const agent = await (await fetch(`${process.env.PROVENANCE_API}/v1/mandate/agents`, { method: "POST", headers, body: JSON.stringify({ name: "cartwise", category: "shopping_agent" }) })).json();
await fetch(`${process.env.PROVENANCE_API}/v1/identify/agents/${agent.id}/signing_keys`, {
  method: "POST", headers, body: JSON.stringify({ jwk: { kty: "OKP", crv: "Ed25519", x: "G_STrQBvTD7Bi8OjlNnLcKPPiPp03Vh0sJKTMG7QzFY" } }),
});
```

**Python**

```python
import os
import httpx

api, h = os.environ["PROVENANCE_API"], {"Authorization": f"Bearer {os.environ['PROVENANCE_KEY']}"}
agent = httpx.post(f"{api}/v1/mandate/agents", headers=h, json={"name": "cartwise", "category": "shopping_agent"}).json()
httpx.post(f"{api}/v1/identify/agents/{agent['id']}/signing_keys", headers=h, json={"jwk": {"kty": "OKP", "crv": "Ed25519", "x": "G_STrQBvTD7Bi8OjlNnLcKPPiPp03Vh0sJKTMG7QzFY"}})
```

```json
{
  "object": "agent_signing_key",
  "status": "active"
}
```

## 2. Ask the person

[`POST /v1/mandate/grant_requests`](https://provenanceauth.com/docs/api/network.md#post-v1-mandate-grant-requests) takes the limits you ask for (the example registers the signing key first, as in step 1). Send the person to `grant_url`; they see your agent, your company and the limits in plain words, lower any of them, and grant with their passkey.

**curl**

```bash
curl -s -X POST $PROVENANCE_API/v1/identify/agents/$AGENT_ID/signing_keys -H "Authorization: Bearer $PROVENANCE_KEY" -H "Content-Type: application/json" -d '{ "jwk": { "kty": "OKP", "crv": "Ed25519", "x": "G_STrQBvTD7Bi8OjlNnLcKPPiPp03Vh0sJKTMG7QzFY" } }' > /dev/null
curl -s -X POST $PROVENANCE_API/v1/mandate/grant_requests \
  -H "Authorization: Bearer $PROVENANCE_KEY" -H "Content-Type: application/json" \
  -d '{ "agent_id": "'$AGENT_ID'", "proposed": { "max_amount": 50, "budget_amount": 200, "budget_window": "month", "currency": "USD", "expires_at": "2027-06-30T00:00:00Z" }, "return_url": "https://cartwise.example/connected" }'
```

**Node**

```ts
const request = await (await fetch(`${process.env.PROVENANCE_API}/v1/mandate/grant_requests`, {
  method: "POST", headers,
  body: JSON.stringify({ agent_id: agent.id, proposed: { max_amount: 50, budget_amount: 200, budget_window: "month", currency: "USD", expires_at: "2027-06-30T00:00:00Z" }, return_url: "https://cartwise.example/connected" }),
})).json();
```

**Python**

```python
request = httpx.post(f"{api}/v1/mandate/grant_requests", headers=h, json={
    "agent_id": agent["id"],
    "proposed": {"max_amount": 50, "budget_amount": 200, "budget_window": "month", "currency": "USD", "expires_at": "2027-06-30T00:00:00Z"},
    "return_url": "https://cartwise.example/connected",
}).json()
```

```json
{
  "object": "network_grant_request",
  "status": "pending"
}
```

## 3. Collect the grant

When the person grants it, the `mandate.network_grant.granted` webhook carries `network_mandate` (the token), or [`GET /v1/mandate/grant_requests/{id}`](https://provenanceauth.com/docs/api/network.md#get-v1-mandate-grant-requests-id) returns it. The token is an ES256 `network-mandate+jwt` with `aud: provenance-network`; its `sub` is the person, pairwise for your account. Keep it like a credential.

## 4. Buy under it

[`POST /v1/mandate/network_orders`](https://provenanceauth.com/docs/api/network.md#post-v1-mandate-network-orders) takes the token, the store's cart as `checkoutBinding(order)` and the store's origin as `aud`:

```ts
import { checkoutBinding } from "@provenanceauth/rails/checkout";

const order = checkoutBinding({ order_id: "ord_88213", total: 3800, currency: "usd", merchant_id: "m_quillstack", origin: "https://shop.quillstack.example",
  items: [{ sku: "nb-linen", name: "Linen notebook", quantity: 2, unit_amount: 1900 }] });
const res = await prov.mandate.network.createOrder({ networkMandate, order, aud: order.origin });
if (res.result === "authorized") {
  // Send res.intentToken in the store's Provenance-Intent header. It works for 10 minutes.
} else if (res.result === "approval_required") {
  // Show res.approval.approveUrl to the person as-is; once they approve, authorize again with approvalId.
}
```

From an MCP client, the `request_grant` and `buy_under_grant` tools do the same ([agent tools](https://provenanceauth.com/docs/provenance-mcp/agent-tools.md)).

Send `agent_session` with each authorize call so the person sees each run of your agent as its own session, and can stop or limit one without turning your agent off. A stopped session's calls are refused with `session_stopped`, and its reserved orders are released.

- `agent_session.id` (string, required): Your ID for this run. It's stored hashed.
- `agent_session.client` (object): `name` and `version`: the label the person sees.
- `agent_session.workspace` (string): What this run works on, shown to the person.

## Errors you might see

| Code or reason | What to do |
| --- | --- |
| `network_agent_unverified` | Register a signing key for the agent (step 1). |
| `over_max_amount`, `over_budget` | Show `approval.approve_url` to the person, then authorize again with `approval_id`. |
| `network_grant_inactive` | The person revoked or paused it, or it expired. Ask again. |
| `store_not_allowed` | The person or the grant blocks this store. |
| `session_stopped`, `session_limit` | The person stopped or limited this session. Start a new session, or stay within its limits. |
| `blocked_by_person` | The person blocked your agent. Stop asking them; only they can unblock it. |

A response can also carry `notes`: short notes and questions from the person. See [receive notes from people](https://provenanceauth.com/docs/mandate/guides/receive-notes-from-people.md).

## See also

- [Network grants](https://provenanceauth.com/docs/mandate/concepts/network-grants.md)
- [Accept network orders](https://provenanceauth.com/docs/authorize/guides/accept-network-orders.md)
- [Provenance MCP](https://provenanceauth.com/docs/provenance-mcp.md)
