skip to content

With the Play Integrity API, how does a standard request differ from a classic one, and how do requestHash and nonce bind a verdict to one action?

level: seniorimportance: should knowfreq 35%

answer

  1. warm up once, ask often
  2. token goes to your server
  3. digest of the request, max 500 bytes
  4. who owns replay protection

basics

~20 s

A standard request warms up a token provider once, then returns tokens in a few hundred milliseconds, replay-protected by Google Play. A classic request recomputes everything and relies on your nonce. requestHash or nonce carries a request digest the server recomputes.

solid answer

~40 s

Standard: call `prepareIntegrityToken` with your Cloud project number once (a few seconds, at most 5 times a minute per app instance) and keep the `StandardIntegrityTokenProvider`. Then call `request()` with `setRequestHash(...)` per action; it takes a few hundred milliseconds and Google Play blocks replays automatically. Classic: `requestIntegrityToken` with `setNonce(...)` recomputes all verdicts each time, takes seconds, and leaves replay protection to you. The nonce must be URL-safe Base64 of 16 to 500 characters, ideally holding a server-issued one-time value plus a request digest. Either way the token goes to your backend, which decodes it (`decodeIntegrityToken` for standard) and checks `requestDetails` (package name, `requestHash` or `nonce`, `timestampMillis`) before trusting `PLAY_RECOGNIZED`, `LICENSED` or device labels. If a standard provider expires, the request fails with `INTEGRITY_TOKEN_PROVIDER_INVALID`; prepare a new one.

code

kotlin · 24 lines
kotlin
class IntegrityTokens(appContext: Context, private val cloudProjectNumber: Long) {
    private val standardIntegrityManager = IntegrityManagerFactory.createStandard(appContext)
    private var tokenProvider: StandardIntegrityTokenProvider? = null

    // Warm up at launch or in the background, never on the tap that needs the token.
    suspend fun warmUp() {
        tokenProvider = standardIntegrityManager.prepareIntegrityToken(
            PrepareIntegrityTokenRequest.builder()
                .setCloudProjectNumber(cloudProjectNumber)
                .build()
        ).await()
    }

    // requestHash = SHA-256 of a stable serialization of the action, e.g. "confirm delivery 4821".
    suspend fun tokenFor(requestHash: String): String {
        val provider = checkNotNull(tokenProvider) { "warmUp() has not completed" }
        val response = provider.request(
            StandardIntegrityTokenRequest.builder()
                .setRequestHash(requestHash)
                .build()
        ).await()
        return response.token() // send to the backend, which decodes and checks it
    }
}

go deeper

for a junior

Recall that the app fetches an encrypted token and sends it to its server; the device never decides from the verdict itself.

for a middle

Explain warm-up then request for standard, a one-shot request with a nonce for classic, and what requestHash and nonce carry.

for a senior

Show the full loop: digest binding, decoding on the server, checking requestDetails before verdicts, handling INTEGRITY_TOKEN_PROVIDER_INVALID, and never caching verdicts.

for a principal

Decide where attestation earns its quota and latency, and what policy applies to clients that can never produce a token.

## What Play Integrity gives you The **Play Integrity API** helps a backend decide whether a request comes from your **genuine app binary**, installed through Google Play, on a **genuine certified Android device**. The app obtains an encrypted, signed **integrity token** and sends it to its own server; the server decodes it and acts on the verdicts. The decision is never made on the device. The client library is `com.google.android.play:integrity` (1.6.0 at the time of writing), and requests need both the Play Store and Google Play services on the device. There are two request types, and both return verdicts in the same format. ## Standard requests A standard request has two phases: 1. **Prepare (warm up) once.** Create a manager with `IntegrityManagerFactory.createStandard(context)` and call `prepareIntegrityToken(PrepareIntegrityTokenRequest.builder().setCloudProjectNumber(n).build())`. You get a `StandardIntegrityTokenProvider` that you keep in memory. Warm-up takes a few seconds and calls a server, so allow a long timeout (Google suggests about a minute). Each app instance can prepare at most 5 times per minute, so do it at launch or in the background. 2. **Request on demand.** For each action to protect, call `tokenProvider.request(StandardIntegrityTokenRequest.builder().setRequestHash(hash).build())`. Latency is typically a few hundred milliseconds, and the result's `token()` goes to your backend. The **`requestHash`** binds the token to one action. Compute a digest (for example SHA-256) of a stable serialization of the request parameters, at most 500 bytes, and pass it in. The value comes back verbatim inside the signed verdict. The server recomputes the digest from the request it actually received and rejects a mismatch. Without it, the token is bound only to the device. **Replay protection is automatic**: decoding the same token repeatedly returns cleared verdicts (an empty device verdict, and `UNEVALUATED` app and licensing verdicts). If a provider is kept too long it can expire (clearing the Play Store app's data also removes it), and the next request fails with `INTEGRITY_TOKEN_PROVIDER_INVALID`. Prepare a new provider and retry. ## Classic requests A classic request is a one-shot: `IntegrityManagerFactory.create(context).requestIntegrityToken(IntegrityTokenRequest.builder().setNonce(nonce).build())`. Apps distributed through Google Play don't set a Cloud project number, because Play links one in the Console. Every classic request recomputes all verdicts, takes a few seconds, uses more data and battery, and is limited to 5 tokens per minute per app instance. Here **you** own replay protection through the **`nonce`**: - It must be a URL-safe, non-wrapping Base64 string of 16 to 500 characters. - For tampering, put a digest of the request in it, as with `requestHash`. - For replay, have the server issue an unpredictable unique value, put it in the nonce, and have the server accept each value only once. - Google recommends combining both. ## Side by side | | Standard | Classic | |---|---|---| | Warm-up | Yes, a few seconds | No | | Typical latency | A few hundred ms | A few seconds | | Intended frequency | Any action or server call | Rare, highest-value actions | | Binding field | `requestHash` | `nonce` | | Replay protection | Automatic, by Google Play | Your server, through the nonce | | Decode locally on your server | No | Yes, in a secure environment | Both share a default quota of **10,000 requests per app per day**, which can be raised on request. Google advises making a standard request instead of caching a classic verdict, because cached verdicts invite proxying. ## What the server checks first The backend decodes the token. For a standard token it calls `playintegrity.googleapis.com/v1/PACKAGE_NAME:decodeIntegrityToken` with a service account from the linked Cloud project. Before reading any verdict it checks `requestDetails`: - `requestPackageName` matches your app; - `requestHash` (or `nonce`) matches what it recomputes; - `timestampMillis` is recent enough. Only then does it read `appIntegrity.appRecognitionVerdict` (`PLAY_RECOGNIZED`), `accountDetails.appLicensingVerdict` (`LICENSED`) and the device labels. How strict to be with each tier is a security design decision, covered in the mobile system design material on transport and trust. ## Client-side failures to plan for - `PLAY_STORE_NOT_FOUND` or `PLAY_SERVICES_NOT_FOUND`: the Play Store or Play services is missing, disabled or outdated. Where the user can fix it, Play's `GET_INTEGRITY` remediation dialog guides them; on a device that has neither, no token is possible and the server's policy for unattested clients applies. - `NETWORK_ERROR`, `TOO_MANY_REQUESTS`, `GOOGLE_SERVER_UNAVAILABLE`: retry with backoff, and treat repeated failure as a failed check. - `INTEGRITY_TOKEN_PROVIDER_INVALID`: warm up again.

  • Why does Google advise against caching a classic integrity verdict and reusing it for later actions?
    A cached verdict can be proxied: an attacker reuses a good device's verdict for traffic from a bad environment. Classic tokens are expensive, so the temptation is real. Google's answer is to make a standard request whenever you need a verdict, because it is cheap, on demand and replay-protected by Google Play.
  • What should the delivery app do when the Play Integrity request fails with PLAY_SERVICES_NOT_FOUND?
    The error means Play services is unavailable or needs an update. If the user can fix it, show Play's `GET_INTEGRITY` remediation dialog, which helps install, update or enable it. Blind retries won't help. If it stays unavailable, send the request with the error and let the server apply its policy for unattested clients: allow, limit, or require extra verification. Never skip the check silently on the client.

  • The app can decode the integrity token on the device and decide by itself whether to proceed.
  • A standard request needs a server-generated nonce to be protected against replay.
  • Calling prepareIntegrityToken before every single request is the recommended pattern.
  • The requestHash should contain the raw request data, including personal details, in plain text.
  • Once verdicts say PLAY_RECOGNIZED, the server can skip checking requestDetails.

Assumes the Play Integrity library 1.6.0; since 1.4.0 standard and classic requests share the library's minSdkVersion as their minimum Android version.