Redacting Sensitive Data From Text Content

You can use the code samples below to redact sensitive data from a string of text. You can also feed information to the API using JSON over HTTP.

The API takes the following as arguments:

  • A string of text
  • The placeholder text that will replace sensitive data (in this example, "REDACTED")
  • A list of one or more InfoTypes that you want to redact

It returns the string with any sensitive data replaced by your chosen placeholder.

Node.js

This sample uses the Google Cloud Client Library for Node.js. See the Node.js setup guide for information on setting up Node.js.

// Imports the Google Cloud Data Loss Prevention library
const DLP = require('@google-cloud/dlp');

// Instantiates a client
const dlp = DLP();

// The string to inspect
// const string = 'My name is Gary and my email is gary@example.com';

// The string to replace sensitive data with
// const replaceString = 'REDACTED';

// The minimum likelihood required before redacting a match
// const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';

// The infoTypes of information to redact
// const infoTypes = [{ name: 'US_MALE_NAME' }, { name: 'US_FEMALE_NAME' }];

const items = [{ type: 'text/plain', value: string }];

const replaceConfigs = infoTypes.map((infoType) => {
  return {
    infoType: infoType,
    replaceWith: replaceString
  };
});

const request = {
  inspectConfig: {
    infoTypes: infoTypes,
    minLikelihood: minLikelihood
  },
  items: items,
  replaceConfigs: replaceConfigs
};

dlp.redactContent(request)
  .then((body) => {
    const results = body[0].items[0].value;
    console.log(results);
  })
  .catch((err) => {
    console.log(`Error in redactString: ${err.message || err}`);
  });

Java

This sample uses the Google Cloud Client Library for Java.

// Instantiate the DLP client
try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
  // The minimum likelihood required before returning a match
  // eg.minLikelihood = LIKELIHOOD_VERY_LIKELY;
  InspectConfig inspectConfig =
      InspectConfig.newBuilder()
          .addAllInfoTypes(infoTypes)
          .setMinLikelihood(minLikelihood)
          .build();

  ContentItem contentItem =
      ContentItem.newBuilder()
          .setType("text/plain")
          .setData(ByteString.copyFrom(string.getBytes()))
          .build();

  List<ReplaceConfig> replaceConfigs = new ArrayList<>();

  if (infoTypes.isEmpty()) {
    // replace all detected sensitive elements with replacement string
    replaceConfigs.add(ReplaceConfig.newBuilder().setReplaceWith(replacement).build());
  } else {
    // Replace select info types with chosen replacement string
    for (InfoType infoType : infoTypes) {
      replaceConfigs.add(
          ReplaceConfig.newBuilder().setInfoType(infoType).setReplaceWith(replacement).build());
    }
  }

  RedactContentResponse contentResponse =
      dlpClient.redactContent(
          inspectConfig, Collections.singletonList(contentItem), replaceConfigs);
  for (ContentItem responseItem : contentResponse.getItemsList()) {
    // print out string with redacted content
    System.out.println(responseItem.getData().toStringUtf8());
  }
}

Protocol

See the JSON quickstart for more information on using JSON.

Sample Input:

{
        "items":
        [
          {
            "value": "My phone number is (123) 456-7890",
            "type": "text/plain"
          }
        ],
        "replaceConfigs":
        [
          {
            "replaceWith": "[REDACTED PHONE NUMBER]",
            "infoType":
            {
              "name": "PHONE_NUMBER"
            }
          }
        ]
      }
URL:
  POST https://dlp.googleapis.com/v2beta1/content:redact
Sample Output:
{
       "items": [
        {
         "type": "text/plain",
         "value": "My phone number is [REDACTED PHONE NUMBER]"
        }
       ]
      }

Send feedback about...

Data Loss Prevention API