Skip to content
Start free Sign in

Record consent from an app

Last updated

Send each choice made in your app to your site's consent log. The records appear in the site's consent log and its exports beside the ones from your website's banner.

The request

POST https://app.kukie.io/api/v1/consent (the configuration's consent_endpoint) with Content-Type: application/json and Accept: application/json. No API key and no token: send no Origin header either, as native HTTP clients don't. A request whose Origin header names another domain than your site's is refused. An Origin header that names no domain, such as null, is not refused. Without an Origin header, the Referer header is checked the same way.

FieldWhat to send
site_keyYour site key.
visitor_uidA random identifier the app creates and keeps with its data, up to 64 characters (below).
consent_idA new random version 4 UUID for each choice (below).
actionaccept_all, reject_all, custom, update: Accept all, Reject all, a choice saved from your preferences screen, and a later change.
categories_acceptedThe slugs of the categories the person allows, the required ones included. After Reject all, only the required ones. It may be empty, never missing.
services_acceptedOn a per_service site, the slugs of the services allowed, or an empty list.
banner_versionThe configuration's version.
page_urlOptional: a screen name such as settings/privacy, or nothing. Never a deep link that carries identifiers. Kukie.io cuts it at its first ? or #, so a query string is never stored.
platformOne of ios, android, web, other. A React Native, Flutter or Unity app names the system it runs on.
app_idYour bundle ID or application ID, up to 255 letters, digits, periods, hyphens and underscores.
sdk_versionThe version of your app's consent code, up to 32 letters, digits, periods, plus and minus signs.

platform, app_id and sdk_version describe the app, never the device: never put a device identifier, a device model or a user's details in them. A value in another shape is stored as empty and the record is still stored. Anyone can send any value in these fields, so they show where a record came from; they don't prove it.

The two identifiers

  • visitor_uid: generate a random identifier the first time it is needed, keep it in the app's own storage and send it with every record. Never use the advertising identifier (IDFA or the Android advertising ID) or the identifier for vendor (IDFV). The identifier lasts as long as the app's data: deleting the app removes it, but restoring the app's data from a backup brings it back. On Android, Auto Backup restores shared preferences, where the Kotlin sample keeps it, when the app is installed again. On iOS, restoring a device from an iCloud or computer backup restores UserDefaults, where the Swift sample keeps it. To give each installation a new identifier, keep it out of the backup. On Android, exclude kukie_consent.xml (domain sharedpref) in both sections of your dataExtractionRules (cloud-backup and device-transfer) and in your fullBackupContent rules, or keep the identifier in a file under noBackupFilesDir. On iOS, keep it in a file marked isExcludedFromBackup, which Apple treats as guidance, not a guarantee.
  • consent_id: generate a new random version 4 UUID for each choice and keep it with the choice. It is the person's receipt: you can look the record up by it (below), and a person can quote it to you.

Offline and retries

Store the choice on the device first and apply it at once, then send the record. Keep the records waiting to be sent in one queue on the device, oldest first. Add each new record to the end, and send it at once only when no record is waiting before it. Send one record at a time, and the next only after a 201 or a 200. After any other answer, such as a 422, drop that record and go on with the next, as the samples do. When the app is offline, or Kukie.io answers 429 or a 5xx status, stop at the first failure: keep that record and the rest in the queue, unchanged, and send them again later, oldest first, each with its same consent_id. Wait longer after each failure. If a copy of a record reaches Kukie.io within a day of the first one with the same consent_id, it is not stored again, so a retry doesn't add a second record. Send one record for each choice, not one for each app start.

The answers

StatusWhat it means
201Accepted. The record is stored a moment later, when the site key belongs to a site.
200The site's consent log is switched off, so nothing is stored.
422A field is missing, too long or in the wrong shape: a required field, or an optional one such as a page_url over 2,048 characters or a banner_version that is not a whole number. The answer's errors names the field. Fix the request; sending it again unchanged won't help.
403The request's Origin header, or without one its Referer header, names another domain than your site's. Send no Origin or Referer header.
429Too many requests from this network, for this visitor or for the site. Keep the record and send it again later.

Check a record

GET https://app.kukie.io/api/v1/consent/verify/CONSENT-ID?site_key=YOUR-SITE-KEY answers 200 with the record's consent ID, action, accepted categories and services, time, country and banner version, or 404 when there is no such record. Records are stored a moment after the 201, so wait a little before you check.

What is stored

Each record holds the visitor_uid, the consent_id, the action, the categories and services, the banner version, the page address without its query string, platform, app_id and sdk_version, and the time Kukie.io stores it, so a record sent later carries that later time. The record also holds a keyed hash of the IP address and of the user agent of your app's HTTP client, and the country the IP address belongs to, not the IP address or the user agent themselves. Like other requests to Kukie.io's servers, the request also appears in Kukie.io's technical server logs with its IP address and user agent, as the Data Processing Agreement describes. The site's consent log exports end with the columns Platform, App ID and SDK Version.

Samples

Swift, with URLSession and Codable:

import Foundation

// One record for one choice. Codable, so a record waiting to be sent can be
// kept in your app's storage and sent again unchanged.
struct ConsentRecord: Codable {
    let siteKey: String
    let visitorUid: String
    let consentId: String
    let action: String
    let categoriesAccepted: [String]
    let bannerVersion: Int?
    let pageUrl: String?
    let platform: String
    let appId: String
    let sdkVersion: String

    enum CodingKeys: String, CodingKey {
        case siteKey = "site_key"
        case visitorUid = "visitor_uid"
        case consentId = "consent_id"
        case action
        case categoriesAccepted = "categories_accepted"
        case bannerVersion = "banner_version"
        case pageUrl = "page_url"
        case platform
        case appId = "app_id"
        case sdkVersion = "sdk_version"
    }
}

enum ConsentLog {
    // A random identifier, kept in the app's own storage with its data.
    // Never the advertising identifier or the identifier for vendor.
    static func visitorId() -> String {
        let key = "kukie_visitor_uid"
        if let saved = UserDefaults.standard.string(forKey: key) {
            return saved
        }
        let fresh = UUID().uuidString.lowercased()
        UserDefaults.standard.set(fresh, forKey: key)
        return fresh
    }

    static func record(siteKey: String, action: String, categories: [String], bannerVersion: Int?) -> ConsentRecord {
        ConsentRecord(
            siteKey: siteKey,
            visitorUid: visitorId(),
            consentId: UUID().uuidString.lowercased(), // a new identifier for every choice
            action: action,
            categoriesAccepted: categories,
            bannerVersion: bannerVersion,
            pageUrl: "settings/privacy",
            platform: "ios",
            appId: Bundle.main.bundleIdentifier ?? "",
            sdkVersion: "1.0.0"
        )
    }

    enum Outcome {
        case stored, retryLater, dropped
    }

    static func send(_ record: ConsentRecord, to endpoint: URL) async -> Outcome {
        guard let body = try? JSONEncoder().encode(record) else {
            return .dropped
        }
        var request = URLRequest(url: endpoint)
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.setValue("application/json", forHTTPHeaderField: "Accept")
        request.httpBody = body
        guard let result = try? await URLSession.shared.data(for: request),
              let response = result.1 as? HTTPURLResponse else {
            return .retryLater // offline or timed out: keep it and send it again later
        }
        switch response.statusCode {
        case 200, 201:
            return .stored
        case 429, 500...599:
            return .retryLater
        default:
            return .dropped // fix the request: sending it again unchanged won't help
        }
    }
}

Kotlin, with HttpURLConnection and org.json:

import android.content.Context
import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URI
import java.util.UUID

object ConsentLog {
    // A random identifier, kept in the app's own storage with its data.
    // Never the advertising ID.
    fun visitorId(context: Context): String {
        val prefs = context.getSharedPreferences("kukie_consent", Context.MODE_PRIVATE)
        prefs.getString("visitor_uid", null)?.let { return it }
        val fresh = UUID.randomUUID().toString()
        prefs.edit().putString("visitor_uid", fresh).apply()
        return fresh
    }

    // One record for one choice. Keep the body until it is stored and send it again unchanged.
    fun body(context: Context, siteKey: String, action: String, categories: List<String>, bannerVersion: Int?): JSONObject =
        JSONObject()
            .put("site_key", siteKey)
            .put("visitor_uid", visitorId(context))
            .put("consent_id", UUID.randomUUID().toString()) // a new identifier for every choice
            .put("action", action)
            .put("categories_accepted", JSONArray(categories))
            .put("banner_version", bannerVersion)
            .put("page_url", "settings/privacy")
            .put("platform", "android")
            .put("app_id", context.packageName)
            .put("sdk_version", "1.0.0")

    enum class Outcome { STORED, RETRY_LATER, DROPPED }

    // Call it from a background thread.
    fun send(body: JSONObject, endpoint: String): Outcome {
        val connection = URI(endpoint).toURL().openConnection() as HttpURLConnection
        return try {
            connection.requestMethod = "POST"
            connection.doOutput = true
            connection.connectTimeout = 10000
            connection.readTimeout = 10000
            connection.setRequestProperty("Content-Type", "application/json")
            connection.setRequestProperty("Accept", "application/json")
            connection.outputStream.use { it.write(body.toString().toByteArray(Charsets.UTF_8)) }
            when (connection.responseCode) {
                200, 201 -> Outcome.STORED
                429, in 500..599 -> Outcome.RETRY_LATER
                else -> Outcome.DROPPED // fix the request: sending it again unchanged won't help
            }
        } catch (e: IOException) {
            Outcome.RETRY_LATER // offline or timed out: keep it and send it again later
        } finally {
            connection.disconnect()
        }
    }
}

The samples send settings/privacy as the screen and 1.0.0 as the version of the consent code: use your own.

Was this helpful?

Listed on