# Compose capture requests

Let your staff send a capture request by email, text or QR from your own admin tool with <provenance-capture-request>, within options your server fixes, and see whether the message went out.

`<provenance-capture-request>` lets your staff send a [capture request](https://provenanceauth.com/docs/attest/guides/capture-requests.md) from inside your own admin tool: email or text, or a QR the person scans at the counter. Your server fixes what a session may send (the kinds, the hardware requirement, the longest expiry, the contact); the element only chooses within that. After a send it shows each message's status, then when the person opened the link and when the photos arrived.

**Before you begin:** a publishable key (see [embeds](https://provenanceauth.com/docs/embeds.md#1-create-a-publishable-key)) and a secret key with the `attest` scope. The embed needs Pro or the Free trial; the console's **New request** form (Attest › Captures) does the same on every plan: Person and Case, where the link opens (Your app, Text or Email) and what to ask for.

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

## 1. Mint a session on your server

Your server mints a session for one case with [`POST /v1/embed/tokens`](https://provenanceauth.com/docs/api/embeds.md#post-v1-embed-tokens), `widget: "capture_request"`, your case reference, who is sending, and the options every send stays within. Put it behind a route your admin page calls, after your own sign-in check:

In the SDK tabs, `prov` (Node, Python) and `embed` (Go) are your server-side clients, set up as in [admin results and requests](https://provenanceauth.com/docs/embeds/admin-tools.md#3-mint-a-session-for-an-embed).

**curl**

```bash
curl -s -X POST $PROVENANCE_API/v1/embed/tokens \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "widget": "capture_request",
    "object_id": "LMB-20418",
    "author": { "id": "usr_8841", "display_name": "Ria Anand" },
    "request": {
      "kinds": ["capture.id"],
      "accepted_documents": ["passport", "driving_license"],
      "require_tier": { "default": "hardware", "editable": true },
      "expires_in": { "default": 86400, "max": 604800 },
      "contact": { "phone": "+14155550132" },
      "max_sends": 3
    },
    "ttl": 900
  }'
```

**Node**

```ts
// GET /admin/provenance/composer?case=LMB-20418 (your route, behind your staff sign-in)
const session = await prov.embed.createToken({
  widget: "capture_request",
  objectId: caseId,
  author: { id: staff.id, displayName: staff.name },
  request: { kinds: ["capture.id"], acceptedDocuments: ["passport", "driving_license"], expiresIn: { default: 86400, max: 604800 }, contact: { phone: applicant.phone } },
  ttl: 900,
});
res.json({ token: session.embedToken });
```

**Python**

```python
token = prov.embed.create_token(
    widget="capture_request",
    object_id=case_id,
    author={"id": staff_id, "display_name": staff_name},
    request={"kinds": ["capture.id"], "accepted_documents": ["passport", "driving_license"], "contact": {"phone": applicant_phone}},
    ttl=900,
)["embed_token"]
```

**Go**

```go
session, err := embed.CreateToken(ctx, pv.EmbedTokenParams{
	Widget:   pv.WidgetCaptureRequest,
	ObjectID: caseID,
	Author:   &pv.EmbedAuthor{ID: staffID, DisplayName: staffName},
	Request:  &pv.CaptureRequestSessionOptions{Kinds: []string{"capture.id"}, Document: "any", Contact: &pv.EmbedContact{Phone: applicantPhone}},
	TTL:      900,
})
```

| Field | What it does |
| --- | --- |
| `object_id` | Your case reference, 1 to 200 characters. It becomes each request's `case_id`, so staff can't send for another case. |
| `author` | Required: `id` (your reference) and `display_name` for the staff member. Recorded on each request the session sends; Provenance doesn't check it. |
| `request.kinds` | What the session may send: `capture.id` (an ID, then a selfie), `capture.claim` (photos and videos) and `capture.items` (your own items). |
| `request.accepted_documents` | `capture.id`: the document kinds accepted (`passport`, `passport_card`, `driving_license`, `national_id_card`, `residence_permit`; default all but `passport_card`). With one, the person isn't asked which document they have. |
| `request.photos`, `request.videos` | `capture.claim`: `{ min, max }` limits, as on a create. |
| `request.items` | `capture.items` (required with it): the items, with your own labels and instructions. |
| `request.require_tier` | `{ default, editable }`: `any` or `hardware`, and whether staff may switch **Require hardware proof**. Not editable by default. |
| `request.expires_in` | `{ default, max }` in seconds: the expiry, and the longest staff may pick. |
| `request.reminders` | `account` (your reminder schedule, which staff may turn off) or `off`. |
| `request.contact` | A phone or email to send to. It stays on Provenance's server; the element shows it masked. |
| `request.contact_editable` | Whether staff may replace that contact. Without a contact, staff type one. |
| `request.entry` | Who types the contact: `staff` (default) or `person`, for a kiosk or a counter where the person types their own number. |
| `request.qr` | Whether **Show a QR** is offered. On by default. |
| `request.max_sends` | Requests the session may send, 1 to 10. 3 by default. |
| `request.reference_addresses` | Up to five addresses every send carries, as on a [capture request](https://provenanceauth.com/docs/attest/guides/capture-requests.md) create: one of each kind (`other` up to two), each an `address` or a `location`. Capture rules then compare where each photo was taken with them: a bank's application address as a `person` reference is `capture.min_distance_km.person_address`. The element shows each by its `label` (or its kind), never the address. |
| `ttl` | Seconds, up to 900 (15 minutes). |

A key without the `attest` scope gets `403` [`insufficient_scope`](https://provenanceauth.com/docs/platform/errors.md#insufficient_scope). A copied session sends only within its options, at most `max_sends` times, and only until it expires.

## 2. Drop the element in your page

```bash
npm install @provenance/embed
```

```html
<script type="module" src="https://unpkg.com/@provenance/embed"></script>

<provenance-capture-request base-url="https://sandbox-api.provenanceauth.com" publishable-key="prv_pub_1D10TzJo8F73Z7ZHhWGKPpn0K7suJ0se" theme="inherit">
</provenance-capture-request>

<script>
  const el = document.querySelector("provenance-capture-request");
  el.getToken = async () => (await (await fetch("/admin/provenance/composer?case=LMB-20418")).json()).token;
  el.addEventListener("pv:request_sent", (e) => console.log("sent", e.detail.request_id));
</script>
```

`getToken()` works as it does for [the capture result](https://provenanceauth.com/docs/embeds/capture-result.md#3-keep-the-session-alive-with-gettoken): the element calls it on load and one minute before each session ends, and a half-typed number stays when the session renews.

In React:

```tsx
import { ProvenanceCaptureRequest } from "@provenance/embed/react";

<ProvenanceCaptureRequest
  baseUrl={process.env.NEXT_PUBLIC_PROVENANCE_API!}
  publishableKey="prv_pub_1D10TzJo8F73Z7ZHhWGKPpn0K7suJ0se"
  getToken={async () => (await (await fetch(`/admin/provenance/composer?case=${caseId}`)).json()).token}
  onRequestSent={(d) => setRequestId(d.request_id)}
/>;
```

## What it shows

| Row | Example | From the session |
| --- | --- | --- |
| Who | Email or Phone, one field; or the contact you gave, masked | `request.contact`, `request.contact_editable` |
| Consent to text | See [consent to text](#consent-to-text) | Your consent mode and `request.entry` |
| Asks for | ID front and back, a selfie | `request.kinds` and their options |
| Require hardware proof | A switch, only when `editable` | `request.require_tier` |
| Expires | 24 hours | `request.expires_in` |
| Reminders | A switch | `request.reminders` |
| Address, for rules | From the case | `request.reference_addresses`, by label |
| Send the link | Sends by email or text |  |
| Or have them scan this on their phone | **Show a QR** | `request.qr` |

After a send, the card shows one line per fact: "Text sent to •••• 0132 · 1:23 pm", "Opened 1:24 pm", "Next reminder tomorrow 9:00 am", then "Received 1:31 pm". **Send another** starts over while the session has sends left.

**Show a QR** creates its own request with nothing sent (`delivery: "self"`) and draws its link. The link lasts 15 minutes and the QR blurs after 2 minutes until **Show again**, because it's a working link on a staff screen. A QR request and a sent request are separate requests, so one link is never both in a message and on a screen.

On a desktop, **Or open the link** sits under the QR and blurs with it. On a phone (a touch screen with no hover, up to 820 px wide), the row reads "Or take the photos on this phone" with **Use this phone**, and the request opens with one **Open on this phone** button in place of the QR. The `handoff` attribute forces one: `handoff="qr"` always draws the QR, `handoff="link"` always shows the button. In React, pass `handoff="qr"` or `handoff="link"`.

When your page already chose the link-only request (the person is on their own phone), `start="link"` opens straight on it: nothing is sent and no picker shows (`start="link"` in React too).

## Consent to text

A phone send follows your [consent mode](https://provenanceauth.com/docs/messaging/text-consent.md) and `request.entry`. A staff member never ticks a box worded as the person:

| Your mode | `entry: "staff"` (default) | `entry: "person"` |
| --- | --- | --- |
| Provenance asks | No box. Provenance asks the person before the first text, as for any request | The consent box with its fixed wording and Terms · Privacy; the tick is the person's own |
| We have it | A box worded for staff, "They agreed to texts from Larkmoor Bank", with how they agreed; it's recorded as your statement, naming the author | The consent box, as above |

Consent records are on Pro and up, so on Free the composer shows no box.

## Records

Each request the composer sends is an ordinary capture request: it's billed as one, fires the same [webhooks](https://provenanceauth.com/docs/integrate/webhooks.md), and reads the same on the API, with two more fields:

```json
{ "created_via": "embed", "author": { "ref": "usr_8841", "name": "Ria Anand" } }
```

The console's request drawer shows **Sent from an embed by Ria Anand**.

## What the browser sees

The element reads `GET /v1/embed/capture_request_form`, sends with `POST /v1/embed/capture_request_sends`, and reads each sent request's status with `GET /v1/embed/capture_request_sends/{id}`. None of them returns the full contact, another request, the author's ID or the API object, and the link only for a QR. The send also returns a `capture_result` session for the new request, so your page can swap in [`<provenance-capture-result>`](https://provenanceauth.com/docs/embeds/capture-result.md).

## Events

| Event | When | Detail |
| --- | --- | --- |
| `pv:ready` | The session opened | `widget`, `object_id`, `mode` |
| `pv:request_sent` | A request was created | `request_id`, `kind`, `delivery` |
| `pv:delivery` | One of its messages changed state | `request_id`, `channel`, `state` |
| `pv:token_refresh_failed` | `getToken()` couldn't renew the session | `widget` |
| `pv:error` | The element couldn't start, or a send was refused | `code`, `message` |

Confirm on your server from the [webhook](https://provenanceauth.com/docs/integrate/webhooks.md), never from the event alone.

## Embed, console or your own

|  | This embed | The console | Your own UI on the API |
| --- | --- | --- | --- |
| Where | Your admin tool | **Attest › Captures**, **New request** | Anywhere you like |
| Plan | Pro, or the Free trial | Every plan | Every plan |
| Work | Mint a session, drop the tag | None | Create with [`POST /v1/attest/capture_requests`](https://provenanceauth.com/docs/api/capture-requests.md#post-v1-attest-capture-requests), track deliveries, render the consent wording |

## Errors you might see

| Symptom | Cause | Fix |
| --- | --- | --- |
| `400` [`embed_request_out_of_scope`](https://provenanceauth.com/docs/platform/errors.md#embed_request_out_of_scope) | The send asked for something the session doesn't allow; `param` names it | Send within the session's options, or mint one that allows it. |
| `429` [`embed_sends_exhausted`](https://provenanceauth.com/docs/platform/errors.md#embed_sends_exhausted) | The session sent its `max_sends` | Mint a new session. |
| `403` [`insufficient_scope`](https://provenanceauth.com/docs/platform/errors.md#insufficient_scope) | The key has no `attest` scope | Mint with an Attest key. |
| `401` [`embed_token_invalid`](https://provenanceauth.com/docs/platform/errors.md#embed_token_invalid) | The session expired, or the status asked for a request it didn't send | Mint a new one, or give the element `getToken()`. |
| `402` [`trial_ended`](https://provenanceauth.com/docs/platform/errors.md#trial_ended) | The Free trial ended | Upgrade, or use the console's form. |

## See also

- [Capture result in your admin](https://provenanceauth.com/docs/embeds/capture-result.md)
- [Embeds](https://provenanceauth.com/docs/embeds.md)
- [Admin results and requests](https://provenanceauth.com/docs/embeds/admin-tools.md)
- [Capture requests](https://provenanceauth.com/docs/attest/guides/capture-requests.md)
