# Use a policy webhook

Ask your own service for a policy decision through a webhook endpoint, verify its signed requests, and rotate its signing secret.

A policy webhook asks your own service for a decision. Use one when the decision depends on something only your systems know, such as a risk engine or an internal limit. A rule that requires the webhook sends the action's facts to your service and waits up to 3 seconds for its decision. No answer denies the action.

The service is an ordinary [webhook endpoint](https://provenanceauth.com/docs/integrate/webhooks.md) subscribed to `mandate.policy.decision_requested`. Each request is signed with the endpoint's secret, which is shown once, when you create the endpoint.

**Before you begin:** you need an HTTPS service that answers JSON, and an account API key. Creating an endpoint is open on every plan; saving a rule that uses one needs Pro or above.

## 1. Create the webhook endpoint

[`POST /v1/webhook_endpoints`](https://provenanceauth.com/docs/api/webhooks.md#post-v1-webhook-endpoints) takes an HTTPS URL and the event types it hears. Subscribe it to `mandate.policy.decision_requested`. The response carries the endpoint's id and its signing secret, once:

**curl**

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

curl -s -X POST "$PROVENANCE_API/v1/webhook_endpoints" \
  -H "Authorization: Bearer $PROVENANCE_KEY" -H "Content-Type: application/json" \
  -d '{ "url": "https://risk.example.com/provenance/decide", "event_types": ["mandate.policy.decision_requested"] }' \
  | tee endpoint.json
```

```json
{
  "id": "we_01M7VG323DVNN693QRQ99HGAJ1",
  "object": "webhook_endpoint",
  "url": "https://risk.example.com/provenance/decide",
  "event_types": ["mandate.policy.decision_requested"],
  "status": "active",
  "secret": "whsec_vWHapZL77CG5JffPNqDi5fCEEBafrY372r1Je8"
}
```

Store the secret in your service now. An API key can never read it back. An endpoint subscribed to `*` doesn't hear decision requests: it must name the event.

## 2. Verify each request

Provenance posts an event: `{ "id": "evt_…", "object": "event", "type": "mandate.policy.decision_requested", "data": { "rule_id": "…", "facts": { … } } }`, with a `Provenance-Signature` header of the form `t=<unix seconds>,v1=<hex>`. Compute HMAC-SHA256 over `<t>.<raw body>` with the secret and compare it with each `v1` value in constant time, exactly as for [webhooks](https://provenanceauth.com/docs/integrate/webhooks.md). Reject a request whose `t` is more than 5 minutes old.

Answer with JSON:

```json
{ "decision": "deny", "reason": "over_daily_limit" }
```

The request is sent only while a rule is deciding. It isn't listed in your events and isn't retried.

## 3. Use it in a rule

Name the endpoint by its id:

```json
{
  "rules": [
    { "id": "ask-risk-engine", "when": [{ "fact": "transaction_amount", "op": "gt", "value": 25000 }], "require": { "webhook": "we_01M7VG323DVNN693QRQ99HGAJ1" } }
  ]
}
```

Saving the rule checks that the endpoint is yours, active and subscribed. A `deny` fails the verify with `policy_webhook_denied`, and `require_approval` asks a person. A timeout, a disabled or deleted endpoint, or any other answer fails it with `policy_webhook_unavailable`.

## 4. Rotate the secret

[`POST /v1/webhook_endpoints/{id}/rotate_secret`](https://provenanceauth.com/docs/api/webhooks.md#post-v1-webhook-endpoints-id-rotate-secret) issues a new secret and returns it, once. With `previous_secret_expires_in` (up to 86400 seconds) the old secret keeps signing beside it:

**curl**

```bash
export ENDPOINT_ID=$(jq -r .id endpoint.json)

curl -s -X POST "$PROVENANCE_API/v1/webhook_endpoints/$ENDPOINT_ID/rotate_secret" \
  -H "Authorization: Bearer $PROVENANCE_KEY" -H "Content-Type: application/json" \
  -d '{ "previous_secret_expires_in": 86400 }'
```

```json
{
  "id": "we_01M7VG323DVNN693QRQ99HGAJ1",
  "object": "webhook_endpoint",
  "secret": "whsec_pT3kQ8vN2mW7sYd4cFjLh6gAe1rR5xB9nKz0Lq",
  "previous_secret_expires_at": "2026-11-21T09:14:02.511Z"
}
```

Until `previous_secret_expires_at` every request carries two `v1` values, one per secret, so your service keeps verifying while you deploy the new one. Without `previous_secret_expires_in` the old secret stops at once, which is what you want after a leak.

## Errors you might see

| Symptom or code | Cause | Fix |
| --- | --- | --- |
| `policy_invalid` on `require.webhook` | The id isn't an endpoint of yours, or it is disabled or not subscribed to `mandate.policy.decision_requested` | Create the endpoint, or add the event to its `event_types`. |
| [`idempotency_secret_withheld`](https://provenanceauth.com/docs/platform/errors.md#idempotency_secret_withheld) | A retried create or rotation with the same `Idempotency-Key` | The first call succeeded; rotate again for a secret you can read. |
| [`policy_webhook_unavailable`](https://provenanceauth.com/docs/platform/reason-codes.md#policy_webhook_unavailable) | Your service didn't answer within 3 seconds, answered something else, or the endpoint is disabled or gone | Check its logs, the URL and the endpoint. |

## See also

- [Policies](https://provenanceauth.com/docs/concepts/policies.md)
- [Webhooks](https://provenanceauth.com/docs/integrate/webhooks.md)
- [Set limits and policy](https://provenanceauth.com/docs/mandate/guides/set-limits-and-policy.md)
