How to use the masking and restoration API

Updated at:

When integrating with large language model (LLM) applications, enterprises often need to mask sensitive information in user input (such as internal product information and unpublished patents) before it is sent to the model, and restore the masked content to the original information after the model response returns, so that business content remains complete and usable. AI Guardrails provides custom data masking and masked data restoration capabilities and supports business systems in chaining the complete "inbound masking → call the LLM → outbound restoration" workflow through APIs. This topic describes the prerequisites, invocation method, and usage notes of the masking and restoration API.

Note

The Custom Data Masking described in this topic is an independent check item of AI Guardrails. In the Configure check items document, sensitive content detection is the built-in PII-related sensitive content detection of the system. It is different from the custom data masking described in this topic. Take care to distinguish between them.

Prerequisites

Activate the service

Go to the service activation page for AI Guardrails and activate AI Guardrails.

Grant permissions to a RAM user

Before you access the API, grant permissions to a Resource Access Management (RAM) user. You can create an AccessKey pair for your Alibaba Cloud account and RAM users. When you call Alibaba Cloud APIs, the AccessKey pair is used for identity verification. For information about authorization and AccessKey creation, see the official Alibaba Cloud documentation.

Install and integrate the SDK

For the SDK of AI Guardrails, see the SDK reference.

Complete the masking and restoration configuration in the console

Before you call the API, you need to complete the custom data masking and masked data restoration configuration in the AI Guardrails console. Only after the configuration is complete can you use the corresponding capabilities through the API. For details, see Configure custom data masking and restoration.

  1. Log on to the AI Guardrails console, and go to Protection Configuration.

  2. Open the service configuration through the configuration entry of the target service.

  3. Turn on Custom Data Masking. Custom Data Masking requires you to configure the custom masking content in advance. You can configure the masking content in Knowledge Base Management > Masking Library Management. Currently, at most one masking keyword library can be configured, and each library can contain up to 1,000 keywords. You can also turn on Sensitive Content detection as needed. Sensitive Content is the built-in PII sensitive content rule detection of the system, which also supports masking and restoration. Turn it on as needed.

  4. Note that the Restoration configuration is turned off by default, and whether to enable restoration is controlled by a separate switch. You need to enable it yourself in the Sensitive Content or Custom Data Masking configuration item.

API description

Common invocation conventions

For the API signature, common request parameters, and invocation method, see the Multimodal API integration guide.

How it works

The typical call chain for masking and restoration is as follows:

  1. Inbound masking: Submit the user input text to the masking operation, and include the mapping identifier desensitizeId and the request action parameter maskingAction in the request. The operation returns the masked text.

  2. Call the LLM: Send the masked text to the large language model for processing.

  3. Outbound restoration: Submit the model output to the restoration operation, and pass back the same desensitizeId and the request action parameter maskingAction used in the masking request. Based on the mapping relationship, the operation restores the masked content to the original information.

Request parameters

ServiceParameters contains the following fields related to custom data masking and restoration.

Name Type Required Example Description
desensitizeId String Yes (business-unique ID) biz-2026-05-27-uuid-xxxx A business-unique ID. Inbound masking and outbound restoration must use the same value. CIP associates the masking and restoration mapping based on this identifier.
maskingAction String Yes desensitize or restore Required when you use custom data masking; otherwise, the action cannot be triggered. desensitize indicates masking, and restore indicates restoration.

Inbound masking request example

{
  "Service": "query_security_check",
  "ServiceParameters": {
    "content": "My name is Zhang San, my ID card number is 110101199001011234, and my company is Alibaba Cloud.",
    "desensitizeId": "biz-2026-05-27-uuid-xxxx",
    "sessionId": "sess-xxxx",
    "accountId": "uid-xxxx",
    "maskingAction": "desensitize"
  }
}

Outbound restoration request example

{
  "Service": "response_security_check",
  "ServiceParameters": {
    "content": "<model response, which may contain placeholders>",
    "desensitizeId": "biz-2026-05-27-uuid-xxxx",
    "maskingAction": "restore"
  }
}

Response parameters

Top-level fields

Field

Type

Description

Code

Integer

200 indicates business success, and other values indicate business errors.

Message

String

The status message.

RequestId

String

The Alibaba Cloud request-tracing ID. You must provide this value when you submit a ticket.

Data

Object

The moderation result.

Aggregated determination

Field

Type

Description

Suggestion

String

The aggregated recommendation. Valid values: pass, block, watch, and mask. The aggregation priority, from high to low, is block > mask > watch > pass.

Detail

Array

The per-item results of each protection dimension.

Detail[i]: results for each dimension

Field

Description

Type

The protection dimension. For example, sensitiveData indicates sensitive data detection. Custom data masking and restoration are at the same dimension level as sensitive data detection.

Suggestion

The determination result for the current dimension. When custom data masking is matched, the value is mask.

Level

The risk level. When custom data masking is matched, the value is medium.

Result

An array of match details.

Result[i].Ext: dimension-specific extension fields

Field

Applicable scenario

Description

Desensitization

Inbound masking

Returns the masked text.

Restoration

Outbound restoration

Returns the restored text.

Examples

Masking response example

{
  "Code": 200,
  "Data": {
    "desensitizeId": "biz-conversation-001",
    "Suggestion": "mask",
    "Detail": [
      {
        "Type": "customDesensitization",
        "Level": "high",
        "Suggestion": "mask",
        "Result": [
          {
            "Label": "customDesensitization",
            "Level": "high",
            "Description": "Custom data masking",
            "Ext": {
              "Desensitization": "Zhang San [Mobile phone number (Chinese mainland)#4c01], my company is [Company-ASDDSNC#4c01]"
            }
          }
        ]
      },
      {
        "Type": "sensitiveData",
        "Suggestion": "mask",
        "Result": [
          {
            "Label": "1814",
            "Level": "S2",
            "Description": "Mobile phone number (Chinese mainland)",
            "Ext": {
              "Desensitization": "Zhang San [Mobile phone number (Chinese mainland)], my company is Alibaba Cloud"
            }
          }
        ]
      }
    ]
  }
}

[Mobile phone number (Chinese mainland)#4c01] indicates a sensitive content match, and [Company-ASDDSNC#4c01] indicates a custom sensitive data match.

Restoration response example

{
  "Code": 200,
  "Data": {
    "desensitizeId": "biz-conversation-001",
    "Suggestion": "pass",
    "Detail": [
      {
        "Type": "customDesensitization",
        "Level": "none",
        "Suggestion": "pass",
        "Result": [
          {
            "Label": "customDesensitization",
            "Level": "none",
            "Description": "Custom data masking restoration",
            "Ext": {
              "Restoration": "Zhang San 18293921392, my company is Alibaba Cloud"
            }
          }
        ]
      }
    ]
  }
}

In the restoration response, the results of other compliance detection dimensions retain their original response structure.

Code description

Code = 200 indicates business success, and other values indicate business errors. For general descriptions of status codes, see the Code description in the API integration guide.

Usage notes

  1. The caller must cache the desensitizeId used for inbound masking on the business side, and must pass the same value for outbound restoration. Otherwise, restoration cannot be performed.

  2. Before you call restoration, turn on the restoration switch of the corresponding protection dimensions (Sensitive Content and Custom Data Masking) in the console. A dimension whose restoration switch is not turned on does not perform restoration.

  3. Do not modify the placeholders in Desensitization. Do not truncate them, add prefixes or suffixes, or change their letter case. Otherwise, the placeholders may become invalid.

  4. The content value must not exceed 2,000 characters per request. For oversized content, the caller must split it into chunks and send them concurrently. For the same logical request, we recommend that all chunks share the same desensitizeId so that the content can be restored as a whole.

  5. The masking information of a desensitizeId is currently valid for one month. After this period, restoration cannot be performed.

  6. Cross-service restoration is supported: masking and restoration can call services with different detection capabilities enabled.

  7. Custom Data Masking is currently in public preview and is available for free during the public preview period. After you turn on the switch, the service takes effect within about 5 minutes.

  8. Custom Data Masking currently supports only text detection scenarios. Detection modalities such as images, files, multi-modal, and audio and video are not supported yet.

  9. Currently, only one masking keyword library can be configured, and only batch text entry is supported: separate each keyword with a line break, keep each keyword within 50 characters, and enter up to 1,000 lines. File import is not supported yet.

  10. The Custom Data Masking check item and the "masking" handling method of Sensitive Content detection are different features: the former performs targeted masking on specified content based on the associated masking keyword library, and the latter is a handling action after sensitive content is detected. Do not confuse them.

References

  • Configure custom data masking and restoration: the complete operation steps for enabling custom data masking, creating and associating a masking keyword library, and configuring masked data restoration in the console.

  • Configure check items: the entry point for enabling and configuring check items, and the description of the handling methods for sensitive content detection.

  • API integration guide: the integration method of the basic text detection API. The integration steps in this topic are consistent with those in that document.

  • SDK reference: the installation and integration of the SDK for AI Guardrails.