# Sign with two parties

Have two parties each confirm the same document with a passkey, bound to its hash, and get a completion receipt anyone can verify offline.

Use two-sided agreements when both sides must confirm exactly the same content, such as a services agreement, a consent or an escrow handoff. Each party confirms with a passkey over the document's hash and their own role, so a different copy or a swapped party can't complete the agreement. When both have signed, you get a receipt anyone can verify offline.

**Before you begin:** export the sandbox account key. Provenance never receives the document, only its SHA-256 hash.

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

**Limitations:** this is evidence of who agreed to which content and when, for your contract or e-signature workflow. It isn't an e-signature service and makes no claim about legal validity.

## 1. Create the agreement

Hash the document and create the agreement with [`POST /v1/mandate/agreements`](https://provenanceauth.com/docs/api/agreements.md#post-v1-mandate-agreements). In `parallel` mode both parties can sign at once; in `sequential` mode the second party's link opens only after the first signs.

**curl**

```bash
printf 'Services agreement v3 between Brindlecote Supplies and Acme Corp\n' > agreement.txt
export CONTENT_HASH=$(shasum -a 256 agreement.txt | cut -d' ' -f1)

curl -s $PROVENANCE_API/v1/mandate/agreements \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "content_hash": "'"$CONTENT_HASH"'",
    "content_description": "Services agreement v3",
    "mode": "parallel",
    "parties": [
      { "label": "Provider", "email": "ops@brindlecote.example" },
      { "label": "Client", "email": "legal@acme.example" }
    ]
  }' | tee agreement.json
export AGREEMENT_ID=$(jq -r .id agreement.json)
export SIGN_TOKEN=$(jq -r '.signing_links[0].url' agreement.json | sed 's#.*/sign/##')
```

**Node**

```ts
import { createHash } from "node:crypto";
import { readFileSync } from "node:fs";
import { Provenance } from "@provenanceauth/sdk";

const prov = new Provenance({ apiKey: process.env.PROVENANCE_KEY!, baseUrl: process.env.PROVENANCE_API });
const contentHash = createHash("sha256").update(readFileSync("agreement.txt")).digest("hex");
const agreement = await prov.mandate.agreements.create({
  contentHash,
  contentDescription: "Services agreement v3",
  mode: "parallel",
  parties: [
    { label: "Provider", email: "ops@brindlecote.example" },
    { label: "Client", email: "legal@acme.example" },
  ],
});
```

**Python**

```python
import hashlib, os
import httpx

api, headers = os.environ["PROVENANCE_API"], {"Authorization": f"Bearer {os.environ['PROVENANCE_KEY']}"}
content_hash = hashlib.sha256(open("agreement.txt", "rb").read()).hexdigest()
agreement = httpx.post(f"{api}/v1/mandate/agreements", json={
    "content_hash": content_hash,
    "content_description": "Services agreement v3",
    "mode": "parallel",
    "parties": [
        {"label": "Provider", "email": "ops@brindlecote.example"},
        {"label": "Client", "email": "legal@acme.example"},
    ],
}, headers=headers).json()
```

The response, trimmed:

```json
{
  "id": "agr_01M5Q2KJMKC7J3XJPD6BEYMFGB",
  "object": "agreement",
  "content_description": "Services agreement v3",
  "mode": "parallel",
  "status": "pending",
  "parties": [
    { "party_id": "pty_01M5Q2KJMKVY6QJKSJXEA6T7VB", "label": "Provider", "status": "pending", "signed_at": null, "tier": null },
    { "party_id": "pty_01M5Q2KJMKWE1BBYXSTFF2Q836", "label": "Client", "status": "pending", "signed_at": null, "tier": null }
  ],
  "signing_links": [
    { "party_id": "pty_01M5Q2KJMKVY6QJKSJXEA6T7VB", "label": "Provider" },
    { "party_id": "pty_01M5Q2KJMKWE1BBYXSTFF2Q836", "label": "Client" }
  ]
}
```

Each entry in `signing_links` also has the party's `url`, which opens the hosted signing page in the console. Its last path segment is a token that works only for that party and that agreement. Agreements expire after seven days.

## 2. Show each party what they're signing

Deliver each party their link, which opens the hosted signing page. To sign inside your own product instead, use the [`<provenance-sign>` embed](https://provenanceauth.com/docs/embeds.md), or build a page on `GET /v1/mandate/agreements/sign`, which needs no API key:

**curl**

```bash
curl "$PROVENANCE_API/v1/mandate/agreements/sign?token=$SIGN_TOKEN"
```

**Node**

```ts
const token = agreement.signingLinks[0]!.url.split("/sign/")[1];
const signing = await (await fetch(`${process.env.PROVENANCE_API}/v1/mandate/agreements/sign?token=${encodeURIComponent(token)}`)).json();
```

**Python**

```python
token = agreement["signing_links"][0]["url"].split("/sign/")[1]
signing = httpx.get(f"{api}/v1/mandate/agreements/sign", params={"token": token}).json()
```

```json
{
  "id": "agr_01M5Q2KJMKC7J3XJPD6BEYMFGB",
  "object": "agreement",
  "content_description": "Services agreement v3",
  "mode": "parallel",
  "overall_status": "pending",
  "party": { "party_id": "pty_01M5Q2KJMKVY6QJKSJXEA6T7VB", "label": "Provider", "status": "pending" },
  "other_parties": [{ "label": "Client", "status": "pending" }],
  "sign_challenge": "hP3r08-W2ilfltScyxEPE6TVcRqSPAQ790k3KQKZ9DY",
  "can_sign": true
}
```

Show the `content_hash` and let the party check their own copy against it before signing; the embed hashes the file in the browser without uploading it.

## 3. Each party signs with a passkey

The party signs `sign_challenge` with their passkey (`navigator.credentials.get`, as in [biometric approval](https://provenanceauth.com/docs/mandate/guides/biometric-approval.md)) on a device enrolled with one, then posts the result to `POST /v1/mandate/agreements/sign`. The challenge is derived from the agreement, the content hash and the party, so the same assertion can't sign for another party or another document.

**curl**

```bash norun
curl "$PROVENANCE_API/v1/mandate/agreements/sign?token=$SIGN_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{ \"action\": \"sign\", \"device_id\": \"$DEVICE_ID\", \"assertion\": $(cat assertion.json) }"
```

**Node**

```ts
await fetch(`${process.env.PROVENANCE_API}/v1/mandate/agreements/sign?token=${encodeURIComponent(token)}`, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ action: "sign", device_id: deviceId, assertion }),
});
```

**Python**

```python
httpx.post(f"{api}/v1/mandate/agreements/sign", params={"token": token}, json={"action": "sign", "device_id": device_id, "assertion": assertion})
```

```json
{
  "id": "agr_01M5Q2KJMKC7J3XJPD6BEYMFGB",
  "object": "agreement",
  "party_status": "signed",
  "overall_status": "pending"
}
```

To refuse, a party posts `{ "action": "decline" }`; the agreement becomes `declined` and can't be completed.

## 4. Check the agreement

Read it with [`GET /v1/mandate/agreements/{id}`](https://provenanceauth.com/docs/api/agreements.md#get-v1-mandate-agreements-id). It's `completed` when every party has signed; each party shows when they signed and their device's hardware tier.

**curl**

```bash
curl $PROVENANCE_API/v1/mandate/agreements/$AGREEMENT_ID \
  -H "Authorization: Bearer $PROVENANCE_KEY"
```

**Node**

```ts
const current = await prov.mandate.agreements.get(agreement.id);
```

**Python**

```python
current = httpx.get(f"{api}/v1/mandate/agreements/{agreement['id']}", headers=headers).json()
```

```json
{
  "id": "agr_01M5Q2KJMKC7J3XJPD6BEYMFGB",
  "object": "agreement",
  "status": "pending",
  "completed_at": null
}
```

The `mandate.agreement.completed` and `mandate.agreement.declined` [webhooks](https://provenanceauth.com/docs/integrate/webhooks.md) tell you without polling.

## 5. Get the completion receipt

Once completed, [`GET /v1/mandate/agreements/{id}/receipt`](https://provenanceauth.com/docs/api/agreements.md#get-v1-mandate-agreements-id-receipt) returns a `receipt` object: `claims` covers the agreement and each party's signing evidence, `token` is the ES256 JWS that signs them, and `verify_url` opens the console's Verify a proof page. It verifies offline against the [published keys](https://provenanceauth.com/docs/authorize/guides/verify-tokens.md) with the free [verify endpoint](https://provenanceauth.com/docs/integrate/verify-a-proof.md), and holds the content's hash, never the content.

**curl**

```bash norun
curl $PROVENANCE_API/v1/mandate/agreements/$AGREEMENT_ID/receipt \
  -H "Authorization: Bearer $PROVENANCE_KEY"
```

**Node**

```ts
const { token: receiptToken, verifyUrl } = await prov.mandate.agreements.receipt(agreement.id);
```

**Python**

```python
receipt = httpx.get(f"{api}/v1/mandate/agreements/{agreement['id']}/receipt", headers=headers).json()
```

```json
{
  "object": "receipt",
  "id": "rcpt_3e9a71c0d2b84f5e8a6c1d0b9f2e4a7c",
  "claims": { "receipt_id": "rcpt_3e9a71c0d2b84f5e8a6c1d0b9f2e4a7c", "type": "agreement_completion", "agreement_id": "agr_01M5Q2KJMKC7J3XJPD6BEYMFGB", "status": "completed" },
  "token": "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZ3JlZW1lbnRfaWQiOiJhZ3JfMDFNNVEyS0pNS0M3SjNYSlBENkJFWU1GR0IifQ.c2lnbmF0dXJl",
  "verify_url": "https://…/developers/verify?receipt=eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9…"
}
```

## Errors you might see

| Symptom | Cause | Fix |
| --- | --- | --- |
| `400` [`agreement_invalid`](https://provenanceauth.com/docs/platform/errors.md#agreement_invalid), "agreement is not completed" | The receipt was requested before every party signed | Wait for `mandate.agreement.completed`. |
| `400` [`agreement_invalid`](https://provenanceauth.com/docs/platform/errors.md#agreement_invalid), "the first party must sign first" | A sequential agreement's second party signed early | Sign in order. |
| `400` [`agreement_invalid`](https://provenanceauth.com/docs/platform/errors.md#agreement_invalid), "biometric assertion failed" | The assertion wasn't over this party's challenge, or came from another device | Sign the `sign_challenge` from this party's link with the enrolled device. |
| `401` [`agreement_link_invalid`](https://provenanceauth.com/docs/platform/errors.md#agreement_link_invalid) | The signing token is malformed or expired | Use the link from the create response. |

## See also

- [Biometric approval](https://provenanceauth.com/docs/mandate/guides/biometric-approval.md)
- [Embeds](https://provenanceauth.com/docs/embeds.md)
- [Verify tokens offline](https://provenanceauth.com/docs/authorize/guides/verify-tokens.md)
