# CLI

The Provenance CLI forwards your sandbox events to a local URL, signed like webhook deliveries, and resends webhook deliveries while you build a handler.

The Provenance CLI, `provenance`, forwards your sandbox events to a URL on your machine while you build a webhook handler, and resends a webhook delivery on demand. It works with sandbox keys only; live events go to your registered [webhook endpoints](https://provenanceauth.com/docs/integrate/webhooks.md).

## Install

The CLI needs Node 20 or later.

**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-cli-1.0.0.tgz
```

## Sign in

`provenance login` asks for a sandbox key (`prv_test_…`), checks it with the API and stores it: in the macOS keychain or the Linux Secret Service where there is one, otherwise in a file only you can read. A live key is refused. To use a key without storing it, set `PROVENANCE_KEY`; it wins over the stored key. `provenance logout` forgets the stored key.

```bash norun
provenance login
```

## Commands

| Command | What it does |
| --- | --- |
| `provenance login` | Asks for a sandbox key, checks it and stores it. |
| `provenance listen` | Prints each sandbox event as it happens. |
| `provenance listen --forward-to URL` | Also posts each event to the URL, signed like a webhook delivery. |
| `provenance listen --events a,b` | Forwards only the event types you list, such as `mandate.approval.approved`. |
| `provenance events resend <delivery_id>` | Sends one webhook delivery again and prints your endpoint's answer. |
| `provenance logout` | Forgets the stored key. |

Every command takes `--api <url>` for the API's base URL. Without it, the CLI reads `PROVENANCE_API`.

```bash norun
provenance listen --forward-to http://localhost:3000/webhooks --events mandate.approval.approved,mandate.approval.declined
```

## Verify forwarded events

`provenance listen` prints a `whsec_` signing secret, the same every time for your account. Each forwarded event is a POST with the event as its body and these headers:

| Header | Value |
| --- | --- |
| `Provenance-Signature` | `t=<unix time>,v1=<hex HMAC-SHA256 of "<t>.<raw body>">`, keyed by the secret |
| `Provenance-Event-Type` | The event type, such as `mandate.approval.approved` |
| `Provenance-Event-Id` | The event ID, the same across deliveries and replays |

Give the secret to your handler and verify the signature exactly as for a registered endpoint, for example with `verifyWebhookSignature` from `@provenanceauth/sdk` or `verify_webhook_signature` from `provenanceauth-sdk`.

## Errors you might see

| Message | What to do |
| --- | --- |
| That's a live key | Log in with a `prv_test_` key. The CLI forwards sandbox events only. |
| No API key | Run `provenance login`, or set `PROVENANCE_KEY`. |
| `--forward-to` takes a URL | Pass a full URL, such as `http://localhost:3000/webhooks`. |

## Related

- [Test webhooks](https://provenanceauth.com/docs/integrate/test-webhooks.md): send a test event, replay one, or forward events with the CLI
- [Receive webhook events](https://provenanceauth.com/docs/integrate/webhooks.md)
- [Webhook events](https://provenanceauth.com/docs/reference/webhook-events.md)
- [SDKs and tools](https://provenanceauth.com/docs/get-started/sdks.md)
