# Check an agent's grant

Before you act on another agent's request, check its grant token against Provenance's published keys, check that the request fits its limits, and ask whether anything above it was revoked.

Use this when another agent asks yours to do something on someone's behalf: quote a price, book a slot, run a task. The asking agent sends its grant token. You check the signature against Provenance's published keys, check that the request fits the grant's limits, and, for anything that matters, ask Provenance whether the grant or anything above it has been revoked since the token was signed.

**Before you begin:** cache Provenance's JWKS (`GET /.well-known/jwks.json`, see [verify tokens offline](https://provenanceauth.com/docs/authorize/guides/verify-tokens.md)). The check is free and needs no API key.

## 1. Ask for the grant token

Ask the agent to send the `mandate_token` it was given, in your protocol's request or a header of your choosing. A subagent's token carries its whole chain: the grant ids from the person's down, the registered agents holding each level, and each level's purpose.

## 2. Check it offline

`verifyMandate` checks the signature and expiry, and that what you were asked to do (`need`) fits the limits:

**Node**

```ts
import { fetchJwks } from "@provenanceauth/sdk";
import { verifyMandate } from "@provenanceauth/rails/mandates";

const jwks = await fetchJwks(process.env.PROVENANCE_API!);
const r = await verifyMandate(mandateToken, {
  jwks,
  need: { amount: 25, action_type: "quote", counterparty: "paper.example" },
});
if (!r.valid) throw new Error(`Not authorized: ${r.reason}`);
```

**Python**

```python
from provenanceauth_verify import fetch_jwks
from provenanceauth_verify.mandates import verify_mandate

jwks = fetch_jwks(os.environ["PROVENANCE_API"])
r = verify_mandate(mandate_token, jwks, need={"amount": 25, "action_type": "quote", "counterparty": "paper.example"})
if not r.valid:
    raise PermissionError(r.reason)
```

**Go**

```go
r := provenanceverify.VerifyMandate(mandateToken, jwks, &provenanceverify.MandateNeed{Amount: &amount, ActionType: &actionType}, 0)
if !r.Valid {
	return fmt.Errorf("not authorized: %s", r.Reason)
}
```

The result says:

| Field | What it is |
| --- | --- |
| `root_subject` | Who granted the root: a pairwise subject, never a name or an email. |
| `chain` | Each level from the person's grant down, with the registered agent (`{ id, name }`, or null) and its purpose. |
| `depth`, `parent` | How far below the person's grant this one is, and the grant it was delegated from. |
| `purpose`, `root_purpose` | What this agent is working on, and the person's task. |
| `authority` | The limits as values: `max_amount`, `budget_amount`, `budget_window`, `allowed_action_types`, `allowed_counterparties`, `expires_at`. |

A refusal carries a `reason`:

| Reason | What it means |
| --- | --- |
| `token_invalid`, `token_expired` | The signature doesn't verify against the JWKS, or the token expired. |
| `not_a_mandate` | It's another kind of token. |
| `amount_over_max`, `action_type_not_allowed`, `counterparty_not_allowed` | The request is outside the grant's limits. |
| `grant_expired` | The grant's own expiry passed. |

## 3. Check its status online

A token is a snapshot. Revoking a grant revokes everything delegated below it, so for anything that costs money or can't be undone, add `online: true`. It calls the keyless [`GET /v1/public/grants/{id}/status`](https://provenanceauth.com/docs/api/grants.md#get-v1-public-grants-id-status):

**curl**

```bash
curl -s $PROVENANCE_API/v1/mandate/grants \
  -H "Authorization: Bearer prv_test_acct_P9xN2vQ7bT4mW8sYd3cFjLh6gAe1kR5" \
  -H "Content-Type: application/json" \
  -d '{ "device_id": "dev_00000000000000000000000000000001", "purpose": "Restock printer paper", "constraints": { "max_amount": 80, "expires_at": "2027-12-31T23:59:59Z" } }' | tee m.json
curl -s $PROVENANCE_API/v1/public/grants/$(jq -r .id m.json)/status
```

**Node**

```ts
const r = await verifyMandate(mandateToken, { jwks, need, online: true, apiBase: process.env.PROVENANCE_API });
```

**Python**

```python
r = verify_mandate(mandate_token, jwks, need=need, online=True, api_base=os.environ["PROVENANCE_API"])
```

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

The status is `active`, `suspended` (a missed check-in), `revoked` or `expired`. When a grant above it stops it, `stopped_by` names that grant. Nothing else is returned: no limits, spend, account or agent. A status call that fails returns `status_unavailable`, never a pass. The endpoint allows 300 checks a minute per IP.

Budget left isn't part of either check. The person's grant and every level debit at verify, on the asking agent's side.

## Errors you might see

| Code | What to do |
| --- | --- |
| `resource_missing` | No such grant. `verifyMandate` reads a 404 as `grant_revoked`. |
| `rate_limit_exceeded` | Wait and retry, or cache an `active` answer for a few seconds. |

## See also

- [Delegate to subagents](https://provenanceauth.com/docs/mandate/guides/give-subagents-their-own-limits.md)
- [Verify tokens offline](https://provenanceauth.com/docs/authorize/guides/verify-tokens.md)
- [Grants](https://provenanceauth.com/docs/mandate/concepts/grants.md)
