# Security and identity verification

> How the SDK authenticates, why the app key is public, how to mint user tokens on your backend, the three verification modes, token rotation, and what Herald encrypts at rest.

An app key identifies your app to Herald. It ships inside the app, so anyone can extract it. Identity verification is what stops someone with the key from sending data as one of your users or reading their messages: your backend signs each External ID, and Herald checks the signature.

## How the SDK talks to Herald

- The SDK sends to the endpoint URL you pass to `configure`, and nowhere else. Herald's hosted API is served over HTTPS. If you run your own endpoint, use an `https://` URL; App Transport Security on iOS, and Android's default network security policy for apps targeting API 28 or later, block cleartext HTTP unless your app allows it.
- The SDK does not pin certificates. It trusts the operating system's certificate store.
- Each request carries two headers: `x-herald-app-key` (always) and `x-herald-user-token` (when a user is identified and you have set a token).
- Two endpoints are used: `POST /api/v1/sdk/batch` to send events and `GET /api/v1/sdk/messages` to fetch content cards and in-app messages. On iOS with `crashReports` on, a third, `POST /api/v1/sdk/diagnostics`, receives crash and hang reports. See [SDK REST endpoints](/docs/reference/rest-endpoints).

## Keys and secrets

| Credential | Where it lives | Secret? | What it allows |
|---|---|---|---|
| App key | In your app. Shown on the connection's **Mobile SDK** card. | No. Treat it as public. | Sending anonymous device data. With verification **Off**, also acting as any user. |
| Ingest token | On your backend only. Shown once when you rotate it on the connection page. | Yes | Minting user tokens and calling server APIs such as `/api/v1/contacts` |
| User token | Minted by your backend for one user, passed to the app at sign-in | Per user. It proves who the user is. | Sending data as that user and reading that user's messages |

Herald stores only the SHA-256 hash of the ingest token, never the token itself. That hash is also the key user tokens are signed with, so anyone who can read Herald's database could mint a valid user token for any user of the connection. The hash protects the ingest token itself, not user tokens. User tokens are not stored; Herald recomputes and compares them on each request, in constant time.

Herald does not rotate app keys today. Because the key is public by design, protection comes from identity verification, not from keeping the key hidden.

## Mint user tokens on your backend

A user token is the hex HMAC-SHA256 of the External ID, keyed with the SHA-256 hex digest of the connection's ingest token. Mint it after the user signs in and return it to the app with your session.

```javascript
// Node.js, on your backend. HERALD_INGEST_TOKEN is the connection's ingest token.
import { createHash, createHmac } from "node:crypto";

const key = createHash("sha256").update(process.env.HERALD_INGEST_TOKEN).digest("hex");
const userToken = createHmac("sha256", key).update(userId).digest("hex");
```

To check a token by hand:

```shell
key=$(printf %s "$HERALD_INGEST_TOKEN" | openssl dgst -sha256 -r | cut -d' ' -f1)
printf %s "u_123" | openssl dgst -sha256 -hmac "$key" -r | cut -d' ' -f1
```

In the app, call `setUserToken` right after `identify`:

:::tabs
::tab ios
```swift
Herald.shared.identify(userId: session.userId)
Herald.shared.setUserToken(session.heraldToken)
```
::tab android
```kotlin
Herald.identify(session.userId)
Herald.setUserToken(session.heraldToken)
```
:::

Never put the ingest token in the app, in source control or in client-side code.

## Verification modes

Set the mode on the connection's **Mobile SDK** card. A connection without a setting uses **Required**.

| Mode | Batch with a valid token | Batch with no token | Batch with a wrong token | Messages request |
|---|---|---|---|---|
| Required (default) | Accepted for that user | Rejected with 401 | Rejected with 401 | Returned with a valid token; 401 otherwise |
| Optional | Accepted for that user | Stored as anonymous: events are kept with the claimed External ID marked unverified, and nothing is written to the profile, contact points or message outcomes | Rejected with 401 | Valid token: returned. No token: empty lists. Wrong token: 401. |
| Off | Accepted for the claimed user | Accepted for the claimed user | Not checked | Returned for any External ID |

- A request that names no user (an anonymous device) never needs a token, in any mode. A token sent with it is ignored.
- When a signed-in user's batch gets a 401, the SDK keeps it on the device, logs `Herald needs a user token: call setUserToken` and holds that user's events until the app calls `setUserToken`, which sends them. A 401 for a wrong app key logs `Herald rejected the app key: check the publishable key in configure` instead; the SDK keeps every event and stops sending until the next launch.
- **Optional** is useful during rollout, while older app versions without tokens are still in use.

:::warning Off is for development only
With verification off, anyone with your app key can send data as any user and read their messages. Use it only for testing. App keys ship inside your app, so assume they are public.
:::

## Rotating the ingest token

Rotate the ingest token on the connection page if it may have leaked. Herald shows the new token once.

- Every user token minted with the old ingest token stops working at once. There is no overlap period.
- Under **Required** or **Optional**, identified requests from apps that still hold old tokens get 401 until they receive new ones. The SDK holds those users' events on the device (up to the 1,000-event queue limit) and sends them after the next `setUserToken`.
- Plan for it: have your backend return a fresh user token with every session refresh, and call `setUserToken` whenever the app receives one. Then a rotation costs at most the data sent until each app's next session refresh.
- The same ingest token also authorizes server API calls and web push user tokens. Update every system that uses it.

User tokens do not expire on their own. To revoke one user's access without rotating everything, there is no per-user switch today; rotation is the only revocation.

## What is encrypted at rest

| Data | Protection |
|---|---|
| Provider credentials on a connection (for example the APNs key and FCM service account) | Encrypted by Herald with AES-256-GCM, using a per-write data key wrapped by a key-encryption key held outside the database |
| Ingest token | Stored only as a SHA-256 hash. The hash is the user-token signing key, so a database read allows forging user tokens. |
| User tokens | Not stored |
| Events, profiles, device records, messages | Stored in Herald's database without additional application-level encryption |

On the device, the SDK keeps its state, including the user token and the event queue, in your app's private storage. On iOS that is Application Support, excluded from backups. On Android it is the `herald` folder in the app's no-backup files directory (`noBackupFilesDir`), which Android Auto Backup never copies, so a restored backup does not carry the device ID and user token to another phone. You do not need backup rules for it. SDK versions up to 0.4.0 kept it in the app's files directory; the next SDK release moves it on first launch. On those versions, exclude the `herald` folder in your backup rules.

## HTML in-app messages

HTML messages run in a web view with no file access, no persistent storage and no navigation away from the page. The rules are on [Custom views](/docs/in-app-messages/custom-views#what-the-sdk-enforces).

## Security reviews

For a security questionnaire or a review of the SDK and API, contact your Herald account team.

## Related

- [Users and identity](/docs/concepts/users-and-identity)
- [Data collection controls](/docs/data-and-privacy/data-collection-controls)
- [User data deletion](/docs/data-and-privacy/user-data-deletion)
