# Sign your agent's requests

Sign every request your agent sends per Web Bot Auth, so sites running Identify, or any Web Bot Auth verifier, name your agent instead of guessing.

Sign every request your agent sends so a site can name it. Web Bot Auth is an IETF draft built on HTTP Message Signatures (RFC 9421): each request carries a signature over its method, host and path, and a `Signature-Agent` header that says where the public key is published. Provenance hosts that directory for you. Signing is free on every plan.

**Before you begin:** [register your agent](https://provenanceauth.com/docs/identify/quickstart.md) with a category, and keep its Ed25519 private key in your secret store.

**This package isn't on npm yet.** Until then, every request in these docs runs over HTTP against the [sandbox](https://provenanceauth.com/docs/get-started/sandbox.md).

With access to the private Provenance repository, which your Provenance contact grants, install from it:

```bash
git clone https://github.com/michaelnbane-jpg/provenance.git provenance
cd provenance && pnpm install && pnpm build
node scripts/check-publishable.mjs --out ../provenance-packs
cd ../your-project
npm install ../provenance-packs/provenanceauth-sdk-1.0.0.tgz ../provenance-packs/provenanceauth-agent-adapters-1.0.0.tgz
```

## 1. Wrap your agent's fetch

`signedFetch` signs each request with a fresh nonce and a 60-second lifetime. With `agentId`, the `Signature-Agent` header points at your agent's hosted directory.

**Node**

```ts
import { signedFetch } from "@provenanceauth/agent-adapters";

const agentFetch = signedFetch({
  key: JSON.parse(process.env.AGENT_SIGNING_KEY!), // the private JWK (with d)
  agentId: "agt_01M5T7VVC8FGYV5V4TGZSFATGN",
  apiBaseUrl: process.env.PROVENANCE_API,
});
const page = await agentFetch("https://quillstack.example/products/linen-a5");
```

**Python**

```python
import json, os
from provenanceauth.web_bot_auth import signed_httpx

client = signed_httpx(
    private_jwk=json.loads(os.environ["AGENT_SIGNING_KEY"]),  # the private JWK (with d)
    agent_id="agt_01M5T7VVC8FGYV5V4TGZSFATGN",
    api_base_url=os.environ["PROVENANCE_API"],
)
page = client.get("https://quillstack.example/products/linen-a5")
```

Each request then carries three headers:

```http
Signature-Agent: sig1="https://api.provenanceauth.com/v1/public/agents/agt_01M5T7VVC8FGYV5V4TGZSFATGN/directory";type=jwks_uri
Signature-Input: sig1=("@method" "@authority" "@path" "signature-agent";key="sig1");created=1761419772;expires=1761419832;keyid="lyRUHRS4LQGoXlD_Oaa0IK6BCBsxD6DdTLQRUUK3JW4";alg="ed25519";nonce="Q2F0cy1hbmQtZG9ncy1hcmUtZnJpZW5kcw==";tag="web-bot-auth"
Signature: sig1=:9uYcxZ0bOzWwI1XyAm2hA4v1sQYp8qkKpZbL0sX3bQ8w9hK5ZtB7N1hX2cVnK6d0Wm3eT4rYu8iO2pQ5sD7fGA==:
```

The signature covers `@method` and `@path` as well as the draft's minimum, `@authority` and the `Signature-Agent` member, so a captured signature can't be replayed against another page or method.

## 2. Combine it with approvals

When a site answers with a `ProvenanceIntent` challenge, `checkoutFetch` gets the person's approval and retries. Give it the signed fetch so both the first request and the retry are signed:

```ts
import { checkoutFetch, signedFetch } from "@provenanceauth/agent-adapters";

const shop = checkoutFetch({ client: prov, deviceId, agentSessionId: "pantry-1", agentId, fetch: signedFetch({ key, agentId }) });
```

## 3. Rotate and revoke keys

Add the new key, switch the agent to it, then revoke the old one. An agent publishes up to five active keys.

**curl**

```bash
export PROVENANCE_API=https://sandbox-api.provenanceauth.com
export PROVENANCE_KEY=prv_test_acct_P9xN2vQ7bT4mW8sYd3cFjLh6gAe1kR5
export AGENT_ID=$(curl -s $PROVENANCE_API/v1/mandate/agents -H "Authorization: Bearer $PROVENANCE_KEY" -H "Content-Type: application/json" -d '{ "name": "Pantry Pal", "category": "shopping_agent" }' | jq -r .id)

curl -s $PROVENANCE_API/v1/identify/agents/$AGENT_ID/signing_keys \
  -H "Authorization: Bearer $PROVENANCE_KEY"
```

**Node**

```ts
const keys = await (await fetch(`${process.env.PROVENANCE_API}/v1/identify/agents/${agentId}/signing_keys`, {
  headers: { Authorization: `Bearer ${process.env.PROVENANCE_KEY}` },
})).json();
```

**Python**

```python
import os, httpx

keys = httpx.get(f"{os.environ['PROVENANCE_API']}/v1/identify/agents/{agent_id}/signing_keys",
                 headers={"Authorization": f"Bearer {os.environ['PROVENANCE_KEY']}"}).json()
```

```json
{ "object": "list", "data": [], "has_more": false, "next_cursor": null }
```

Revoke with [`DELETE /v1/identify/agents/{id}/signing_keys/{key_id}`](https://provenanceauth.com/docs/api/signed-agents.md#delete-v1-identify-agents-id-signing-keys-key-id). Suspending the agent empties its directory, so every one of its signatures stops verifying within five minutes.

## 4. Publish under your own domain instead

Any Web Bot Auth verifier can resolve three kinds of `Signature-Agent` member. Pass `signatureAgent` instead of `agentId` to use your own:

| Type | The value is | Example |
| --- | --- | --- |
| `directory` | Your origin; the keys are at its `/.well-known/http-message-signatures-directory` | `https://agents.example` |
| `jwks_uri` | A JWK Set URL | the Provenance-hosted directory |
| `cimd` | A Signature Agent Card that names the keys | `https://api.provenanceauth.com/v1/public/agents/agt_…/card` |

## Errors you might see

| Symptom | Cause | Fix |
| --- | --- | --- |
| `409` [`signing_key_limit_reached`](https://provenanceauth.com/docs/platform/errors.md#signing_key_limit_reached) | The agent already publishes five keys | Revoke a key you've rotated away from. |
| `409` [`signing_key_exists`](https://provenanceauth.com/docs/platform/errors.md#signing_key_exists) | That public key is already registered | Use the existing key. |
| `400` [`parameter_invalid`](https://provenanceauth.com/docs/platform/errors.md#parameter_invalid) on `jwk` | The JWK has a private part or isn't Ed25519 | Send `kty`, `crv` and `x` only. |
| Sites see `signature_expired` | The agent's clock is off, or a request was retried after a minute | Sign each attempt again; keep the clock in sync. |

## See also

- [Verify signed agents](https://provenanceauth.com/docs/identify/guides/verify-signed-agents.md)
- [Require a human](https://provenanceauth.com/docs/identify/guides/require-a-human.md)
- [Framework integrations](https://provenanceauth.com/docs/mandate/guides/framework-integrations.md)
