Skip to content

Get terms and consent

After the app starts or the user logs in, check the user's consent status, and if consent is needed, show the terms and save the consent. If you have not registered the terms module yet, first complete Install and initialize the module.

Step Task Method
1 Choose the consent subject -
2 Get the consent status GetConsentStatusAsync()
3 Get the terms and show the terms consent screen GetTermsAsync(), GetItemRevisionAsync()
4 Save consent SaveConsentAsync()

Choose the consent subject (SubjectKey) according to when you obtain terms consent.

When you obtain terms consent SubjectKey
Before login (for example, the first-launch screen) The deviceKey that the app creates and stores
After login The Player ID of the login session

The Device Key and the Player ID are different consent subjects. Consent saved with a Device Key cannot be retrieved with a Player ID. If consent obtained before login must also apply after login, query the consent status again with the Player ID after login and, if needed, obtain consent again. Choosing a single point at which to obtain consent keeps the implementation simple.

SubjectKey is 1 to 128 characters and cannot contain /, ;, control characters, or leading or trailing whitespace.

After login, Player ID is a long, so convert it to a string. Use CultureInfo.InvariantCulture so the result does not depend on the device's language settings. The deviceKey used before login must follow the same rules. A standard Base64 string can contain /, so generate the deviceKey in a format that follows the rules, such as a UUID.

Decide subjectKey once when the consent flow starts, and use the same value to query the consent status and to save consent.

using System.Globalization;

// After login: convert the Player ID of the login session (session) to a string.
string subjectKey = session.PlayerId.ToString(CultureInfo.InvariantCulture);

// Before login: use the deviceKey the app keeps.
// string subjectKey = deviceKey;

Method

GetConsentStatusAsync

Retrieves whether the consent subject has consented to each item of the current terms, and determines whether to show the terms consent screen.

Field name Type Required Description
request GetConsentStatusRequest Required Request object. ArgumentNullException is thrown if it is null
request.CompanyCode string Required Company code. The part before the first . of the App ID
request.ServiceCode string Required The service code you entered in Terms > Terms Registration
request.SubjectKey string Required Identifier of the consent subject
context ApiCallContext? Optional Per-call settings object. For details, see Core call context
using Hive.Axyl.Core;
using Hive.Axyl.Terms;
using UnityEngine;

// terms: ITermsService registered during module initialization
ITermsService terms = HiveCore.Resolve<ITermsService>();

TermsGetConsentStatusResult result = await terms.GetConsentStatusAsync(new GetConsentStatusRequest
{
    CompanyCode = companyCode,   // Company code: the part before the first '.' of the App ID
    ServiceCode = "SAMPLE",
    SubjectKey  = subjectKey,    // consent subject from step 1
});

switch (result)
{
    case TermsGetConsentStatusResult.Success success:
        ConsentStatusResponseData status = success.Data;
        if (status.CanProceed && !status.ReconsentRequired)
        {
            // Proceed with the service without the terms consent screen.
        }
        else
        {
            // Proceed to step 3 and show the terms consent screen.
        }
        break;

    case TermsGetConsentStatusResult.ServiceNotFound:
    case TermsGetConsentStatusResult.TermsNotFound:
        // Check the company code, the service code, and the terms deployment status in the Hive Console.
        break;

    case TermsGetConsentStatusResult.Failure failure:
        HiveError err = failure.Problem;
        Debug.LogError($"[Terms] [{err.Code}] {err.ExternalCode} {err.Message} (trace: {err.TraceId})");
        break;

    case TermsGetConsentStatusResult.UnknownOutcome unknown:
        Debug.LogWarning($"[Terms] Unknown outcome: {unknown.Code}");
        break;

    // Safety net: unhandled results
    default:
        Debug.LogWarning($"Unhandled result: {result.GetType().Name}");
        break;
}
Field name Type Description
Data.TermId string ID of the current terms
Data.Version int Version number of the current terms
Data.CanProceed bool true if all required items have been consented to
Data.ConsentRequired bool true if there is a required item with no consent record
Data.ReconsentRequired bool true if there is an item that requires re-consent. Optional items are also included
Data.Items IReadOnlyList<ConsentItemStatus> Consent status of each item

Each entry in Data.Items (ConsentItemStatus) holds ItemId, ItemRevisionId, ConsentType, Status, ConsentedVersion, and ConsentedAt. Status is of type ConsentStateValue, and its value is one of the following ConsentState values. Compare it as in item.Status == ConsentState.Consented.

Status Meaning
Consented Consented
ReconsentRequired Re-consent required
NotConsented No consent record
Declined Optional consent declined or withdrawn
NotApplicable Notice item that does not require consent
Response value Handling
CanProceed is true and ReconsentRequired is false Proceed with the service without showing the terms consent screen.
ConsentRequired is true There is a required item with no consent record. Show the terms consent screen.
ReconsentRequired is true The terms changed and re-consent is required. Show the terms consent screen.

If only optional items require re-consent, both CanProceed and ReconsentRequired can be true. In this case, the service can proceed, but show the terms consent screen to obtain re-consent.

Do not cache the consent status; retrieve it every time you need to make a decision. The consent status changes when the operator deploys terms as a re-consent request.

Method

GetTermsAsync

Retrieves the current terms that match the user's country and language.

  • Country: Determined from the IP address the request comes from, and you cannot specify it directly. If there are country-specific terms for that country, those terms are returned; otherwise, the default terms are returned.
  • Language: If you specify Lang, the terms are returned in that language; if the terms do not have that language, they are returned in the default language of the terms. When the user changes the language, retrieve again with one of the AvailableLanguages in the response set as Lang. For the language selection rules, see Terms country and language.
Field name Type Required Description
request GetTermsRequest Required Request object. ArgumentNullException is thrown if it is null
request.CompanyCode string Required Company code. The part before the first . of the App ID
request.ServiceCode string Required Service code
request.Lang string? Optional Language code. Example: ko, ko-KR, zh-Hans. _ cannot be used, as in ko_KR
request.IncludeBody bool Optional If false, Body is returned as null for all items. The default is true
context ApiCallContext? Optional Per-call settings object
TermsGetTermsResult result = await terms.GetTermsAsync(new GetTermsRequest
{
    CompanyCode = companyCode,
    ServiceCode = "SAMPLE",
    Lang        = "ko",
    IncludeBody = false,   // Show only titles and checkboxes first
});

switch (result)
{
    case TermsGetTermsResult.Success success:
        TermsResponseData doc = success.Data;
        // Keep doc so you can use it to save consent.
        foreach (TermItem item in doc.Items)   // Screen display order
        {
            bool showCheckbox = item.ConsentType != ConsentType.Notice;
            bool isRequired   = item.ConsentType == ConsentType.Required;
            bool hasDetail    = item.DetailView;
            // Draw the item with item.Title.
        }
        break;

    case TermsGetTermsResult.ServiceNotFound:
    case TermsGetTermsResult.TermsNotFound:
        // Check the company code, the service code, and the terms deployment status in the Hive Console.
        break;

    case TermsGetTermsResult.Failure failure:
        Debug.LogError($"[Terms] {failure.Problem.ExternalCode} (trace: {failure.Problem.TraceId})");
        break;

    case TermsGetTermsResult.UnknownOutcome unknown:
        Debug.LogWarning($"[Terms] Unknown outcome: {unknown.Code}");
        break;

    // Safety net: unhandled results
    default:
        Debug.LogWarning($"Unhandled result: {result.GetType().Name}");
        break;
}
Field name Type Description
Data.TermId string Terms ID. Pass it as is when saving consent
Data.ServiceCode string Service code
Data.TermNo int Terms number
Data.Version int Terms version number. Pass it as is when saving consent
Data.Language string The language actually returned. Pass it as is when saving consent
Data.AvailableLanguages IReadOnlyList<string> List of languages you can select
Data.MatchedCountry string Country code of the selected terms. DEFAULT for the default terms
Data.EffectiveAt DateTimeOffset Time this version takes effect
Data.NextChangeAt DateTimeOffset? Time the next scheduled deployment takes effect. null if there is none
Data.PageUrl string? URL of the full terms page. For details, see Terms page
Data.VersionPageUrl string? URL of the terms page for this version and language
Data.Items IReadOnlyList<TermItem> 1 to 10 terms items, in screen display order

Each entry in Data.Items (TermItem) holds the following values.

Field name Type Description
ItemId string Terms item ID. Pass it as is when saving consent
ItemRevisionId string Terms item version ID. Pass it as is when saving consent
Revision int Item version number
ConsentEpoch int Re-consent baseline value. It increases when an item whose content changed is deployed as a re-consent request
ConsentType ConsentTypeValue Required is required consent, Optional is optional consent, and Notice is a notice that does not require consent
DetailView bool If false, the item has no content, so show only the title
Title string Item title. Up to 200 characters
Body string? Content in HTML format. null if IncludeBody is false or DetailView is false
PublishedAt DateTimeOffset? Time the item version was published. Always null when you get the terms

The app builds the terms consent screen itself. Configure it as follows according to the ConsentType of each item.

ConsentType Screen configuration
Required Show a consent checkbox. Let the user proceed only after consenting to all required items.
Optional Show a consent checkbox. The user can proceed without consenting.
Notice Show only the notice, without a checkbox.

The item content is in HTML format, so show it by a method that can display HTML, such as a web view. To show the entire terms on one screen, open PageUrl. For details, see Terms page.

Method

GetItemRevisionAsync

If you retrieved the terms with IncludeBody set to false, retrieve only the content of an item when the user selects the detail view of that item. This keeps the initial response light when the content is long or there are many items. Do not call it for items whose DetailView is false, because they have no content.

TermsGetItemRevisionResult result = await terms.GetItemRevisionAsync(new GetItemRevisionRequest
{
    CompanyCode    = companyCode,
    ServiceCode    = "SAMPLE",
    TermId         = doc.TermId,
    ItemId         = item.ItemId,
    ItemRevisionId = item.ItemRevisionId,
    Lang           = doc.Language,
});

switch (result)
{
    case TermsGetItemRevisionResult.Success success:
        string? html = success.Data.Body;   // HTML content of the item
        break;

    case TermsGetItemRevisionResult.ServiceNotFound:
        // Check the company code and the service code.
        break;

    case TermsGetItemRevisionResult.TermsNotFound:
    case TermsGetItemRevisionResult.ItemRevisionNotFound:
        // The requested terms or item version was not found. Check the TermId, ItemId, and ItemRevisionId you passed.
        break;

    case TermsGetItemRevisionResult.Failure failure:
        Debug.LogError($"[Terms] {failure.Problem.ExternalCode} (trace: {failure.Problem.TraceId})");
        break;

    case TermsGetItemRevisionResult.UnknownOutcome unknown:
        Debug.LogWarning($"[Terms] Unknown outcome: {unknown.Code}");
        break;

    // Safety net: unhandled results
    default:
        Debug.LogWarning($"Unhandled result: {result.GetType().Name}");
        break;
}

The Data of the response holds the same fields as an item. In this method, PublishedAt holds the time the item version was published. If ItemId does not belong to the terms of TermId, it returns ItemRevisionNotFound.

Method

SaveConsentAsync

When the user consents on the terms consent screen, save the consent result. Pass the TermId, Version, and Language received in step 3 and the ItemId and ItemRevisionId of each item as is, and pass whether the user consented to each item as Agreed.

  • The first consent must include all required items whose ConsentType is Required.
  • For required items, you can pass Agreed only as true.
  • Notice items do not require consent.
Field name Type Required Description
request SaveConsentRequest Required Request object. ArgumentNullException is thrown if it is null
request.CompanyCode string Required Company code. The part before the first . of the App ID
request.ServiceCode string Required Service code
request.SubjectKey string Required Identifier of the consent subject
request.TermId string Required TermId from the get terms response
request.Version int Required Version from the get terms response
request.Language string Required Language from the get terms response
request.Channel string? Optional Consent channel. A value of up to 32 characters that the app decides, and it is kept as is in the consent history. Example: APP, WEB
request.Items IReadOnlyList<ConsentItemRequest> Required 1 to 10 items to consent to. The same ItemId cannot be included more than once
request.Items[].ItemId string Required ItemId from the get terms response
request.Items[].ItemRevisionId string Required ItemRevisionId from the get terms response
request.Items[].Agreed bool Required true is consent, and false is decline or withdrawal
context ApiCallContext? Optional Per-call settings object. IdempotencyKey prevents the same consent from being saved twice

Save consent uses the IdempotencyKey of ApiCallContext to prevent the same consent from being saved twice. When you create an ApiCallContext, a UUIDv4 idempotency key is filled in by default, and the same key is kept only when the SDK transport layer retries internally. Because you must create a new ApiCallContext for each call, the key changes when the app calls SaveConsentAsync() again.

  • When the user gives new consent, use a new idempotency key.
  • When the app retries the same consent, keep the idempotency key that the app generated, set the same key in the IdempotencyKey of a new ApiCallContext, and call with the same request. If the request was already saved, the original success result is returned as is.
using System;
using System.Linq;

// doc: Response data of GetTermsAsync() in step 3
// agreedByItemId: Whether the user consented to each item on the terms consent screen
var request = new SaveConsentRequest
{
    CompanyCode = companyCode,
    ServiceCode = "SAMPLE",
    SubjectKey  = subjectKey,    // consent subject from step 1
    TermId      = doc.TermId,
    Version     = doc.Version,
    Language    = doc.Language,
    Channel     = "APP",
    Items       = doc.Items
        .Where(item => item.ConsentType != ConsentType.Notice)
        .Select(item => new ConsentItemRequest
        {
            ItemId         = item.ItemId,
            ItemRevisionId = item.ItemRevisionId,
            Agreed         = agreedByItemId[item.ItemId],
        })
        .ToList(),
};

// New consent, so generate a new idempotency key and keep it for retries.
string idempotencyKey = Guid.NewGuid().ToString();
TermsSaveConsentResult result = await terms.SaveConsentAsync(
    request,
    new ApiCallContext { IdempotencyKey = idempotencyKey });

switch (result)
{
    case TermsSaveConsentResult.Success success:
        if (success.Data.CanProceed)
        {
            // Proceed with the service.
        }
        break;

    case TermsSaveConsentResult.TermsOutdated outdated:
        // The terms changed. Start again from step 3 and show the changed terms.
        // Current terms: outdated.Outcome.TermId, outdated.Outcome.Version
        break;

    case TermsSaveConsentResult.RequiredConsentDeclined:
    case TermsSaveConsentResult.RequiredConsentMissing:
        // Check that you passed all required items as consented.
        break;

    case TermsSaveConsentResult.IdempotencyKeyReused:
        // Save again with a new idempotency key.
        break;

    case TermsSaveConsentResult.ServiceNotFound:
    case TermsSaveConsentResult.TermsNotFound:
        // Check the company code, the service code, and the terms deployment status in the Hive Console.
        break;

    case TermsSaveConsentResult.Failure failure:
        HiveError err = failure.Problem;
        Debug.LogError($"[Terms] [{err.Code}] {err.ExternalCode} {err.Message} (trace: {err.TraceId})");
        // For a transient error, retry with the same request and the same idempotencyKey.
        break;

    case TermsSaveConsentResult.UnknownOutcome unknown:
        Debug.LogWarning($"[Terms] Unknown outcome: {unknown.Code}");
        break;

    // Safety net: unhandled results
    default:
        Debug.LogWarning($"Unhandled result: {result.GetType().Name}");
        break;
}

On success, Data (ConsentStatusResponseData) holds the consent status after saving. The fields are the same as in Response data of Get consent status.

Result case Description App client handling
Success The consent was saved. If Data.CanProceed is true, proceed with the service
TermsOutdated The terms changed after you retrieved them, or the terms values in the request differ from the current terms. Outcome holds the TermId and Version of the current terms. Retrieve the terms again, show the changed terms, and save with a new idempotency key
IdempotencyKeyReused The idempotency key was already used for a different request or a different consent subject. This also applies if you send a key used before consent was deleted. Save with a new idempotency key
RequiredConsentDeclined Agreed was passed as false for a required item. Pass required items only as true
RequiredConsentMissing A required item was missing from the first consent. Save with all required items included
ServiceNotFound The company code or the service code is not registered. This also applies if CompanyCode differs from the company of the App ID. Check the company code and the service code
TermsNotFound There are no terms that apply to the request's country. Check the terms deployment status in the Hive Console
Failure A common Failure. Network errors and transient server errors fall under this. Handle according to Common error handling. When you retry, use the same request and the same IdempotencyKey
UnknownOutcome A new result that this SDK version does not recognize. Treat it as a failure and log Code

When you receive TermsOutdated, do not just save again without showing the user the changed terms. You must record the consent against the terms version the user actually saw.

Next steps