# Text consent for Kotlin

The Jetpack Compose opt-in step and consent box for your own Android app, their parameters, the calls they make and what they record.

`com.provenanceauth:text-consent` puts the [consent to text](https://provenanceauth.com/docs/messaging/text-consent.md) step and box in your own Android app, as two Jetpack Compose composables: `TextConsentStep` before your request screen and `TextConsent` on your sign-up or intake screen. Each records with `surface: "kotlin"`. The library needs Android 7.0 (API 24) or later.

**This package isn't on Maven Central 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
// settings.gradle
includeBuild("provenance/kotlin/provenance-text-consent")
// your app's build.gradle
implementation("com.provenanceauth:text-consent:1.0.0")
```

## TextConsentStep

The opt-in step a company's app shows before its own request screen, under **Provenance asks**. It reads the request, shows **Texts from** with your company's name and the masked phone, Provenance's wording with **Terms** · **Privacy**, **Not now** and **Text me**, **Next** with the request, and the Provenance mark.

```kotlin
import com.provenanceauth.textconsent.TextConsentStep
import com.provenanceauth.textconsent.TextConsentStepSource

TextConsentStep(
    baseUrl = apiUrl,
    source = TextConsentStepSource.CaptureEmbedToken(captureToken, publishableKey),
) { outcome ->
    showRequestScreen()
}
```

| Parameter | Type | What it is |
| --- | --- | --- |
| `baseUrl` | `String` | Your Provenance API's base URL |
| `source` | `TextConsentStepSource` | The request: `CaptureEmbedToken(embedToken, publishableKey)` or `CaptureLink(token)` |
| `transport` | `TextConsentTransport` | Optional. The HTTP client; `HttpUrlConnectionTransport()` by default |
| `onError` | `(TextConsentException) -> Unit` | Optional. A refusal or a network failure |
| `onFinish` | `(TextConsentStepOutcome) -> Unit` | Called once when the step ends; show your request screen then |

Mint the embed token on your backend with `{ "widget": "capture", "object_id": "capr_…" }`, as for the [capture widget](https://provenanceauth.com/docs/embeds.md#2-mint-an-embed-token-on-your-backend). The capture link's token is the part after `/capture/` in the request's link.

| `TextConsentStepOutcome` | When |
| --- | --- |
| `AGREED` | **Text me** recorded the consent |
| `NOT_NOW` | **Not now**: nothing was sent and nothing is stored |
| `NOT_OFFERED` | The request offers no step, or it couldn't be read: the composable draws nothing and finishes at once |

The step is offered only when your mode is **Provenance asks**, you're on the Pro plan or above, the request carries a phone with `text` in its `channels`, and that phone has no consent for you and no stop. A refused **Text me** stays on the step with the reason under the wording.

## TextConsent

The consent box for your own form, in either mode. One box, unticked on every load, with the wording beside it, **Terms** · **Privacy** and the Provenance mark. Ticking it with a valid phone records a `box` consent; unticking withdraws it. On the Free plan it draws nothing.

```kotlin
import com.provenanceauth.textconsent.TextConsent

TextConsent(
    baseUrl = apiUrl,
    publishableKey = publishableKey,
    embedToken = textConsentToken,
    phone = mobile,
) { consentId ->
    viewModel.consentId = consentId
}
```

| Parameter | Type | What it is |
| --- | --- | --- |
| `baseUrl` | `String` | Your Provenance API's base URL |
| `publishableKey` | `String` | A publishable key with no allowed origins: an app sends no `Origin` |
| `embedToken` | `String` | A token your backend minted with `{ "widget": "text_consent", "object_id": "<box_id>" }` |
| `phone` | `String` | The mobile number your screen collected: E.164, or ten US digits |
| `onConsented` | `(String) -> Unit` | Optional. The record's id after a tick |
| `onWithdrawn` | `(String) -> Unit` | Optional. The record's id after an untick |
| `onError` | `(TextConsentException) -> Unit` | Optional. A refusal or a network failure |

Read the record from your backend with [`GET /v1/account/text_consents/{id}`](https://provenanceauth.com/docs/api/account.md#get-v1-account-text-consents-id) before you rely on it. It shows as **Your app** in **Agreed to texts**, with no `form_origin`.

Neither composable takes a colour, font, modifier or checked argument: the wording, the box and the mark can't be restyled or pre-ticked. Both follow the system's light or dark setting. The library declares the `INTERNET` permission.

## The calls they make

| Composable | Read | Record | Undo |
| --- | --- | --- | --- |
| `TextConsentStep`, embed token | `GET /v1/embed/capture_request` `?surface=kotlin` | `POST /v1/embed/text_consents` `{ step: true, wording_version, surface }` | None: a consent is withdrawn by the person |
| `TextConsentStep`, capture link | `GET /v1/attest/capture_links` `?surface=kotlin` | `POST /v1/public/text_consents` `{ link_token, wording_version, surface }` | None |
| `TextConsent` | `GET /v1/embed/text_consents/form` `?surface=kotlin` | `POST /v1/embed/text_consents` `{ phone, wording_version, surface }` | `POST /v1/embed/text_consents/{id}/withdraw` |

The step never sends a phone: the token decides your account, the request and its phone. Every call sends `User-Agent: provenance-kotlin/1.0.0 (text-consent)`. `TextConsentClient` makes the same calls without a composable, and `TextConsentStepModel` and `TextConsentBoxModel` hold the composables' state as `StateFlow`s for your own layout or tests.

## TextConsentException

| Field | What it is |
| --- | --- |
| `code` | The API's error code, such as `plan_required` or `parameter_invalid`, or `network_error` when nothing came back |
| `message` | A sentence you can show |
| `status` | The HTTP status, or `0` with no answer |

## See also

- [Get consent to text](https://provenanceauth.com/docs/messaging/text-consent.md): the modes, the step, the box and the records
- [Text consent for Swift](https://provenanceauth.com/docs/messaging/text-consent/swift.md): the same step and box in SwiftUI
- [Embeds](https://provenanceauth.com/docs/embeds.md): publishable keys and embed tokens
