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.

Protocol

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]"
    }
  ]
}

Java

For more on installing and creating a DLP API client, refer to DLP API Client Libraries.

// 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());
  }
}

Node.js

For more on installing and creating a DLP API client, refer to DLP API Client Libraries.

// 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}`);
  });

PHP

For more on installing and creating a DLP API client, refer to DLP API Client Libraries.

use Google\Cloud\Dlp\V2beta1\DlpServiceClient;
use Google\Privacy\Dlp\V2beta1\ContentItem;
use Google\Privacy\Dlp\V2beta1\InfoType;
use Google\Privacy\Dlp\V2beta1\InspectConfig;
use Google\Privacy\Dlp\V2beta1\RedactContentRequest_ReplaceConfig;
use Google\Privacy\Dlp\V2beta1\Likelihood;

/**
 * Redact a sensitive string using the Data Loss Prevention (DLP) API.
 *
 * @param string $string The text to inspect
 */
function redact_string(
    $string,
    $replaceString,
    $minLikelihood = likelihood::LIKELIHOOD_UNSPECIFIED,
    $maxFindings = 0)
{
    // Instantiate a client.
    $dlp = new DlpServiceClient();

    // The infoTypes of information to match
    $usMaleNameInfoType = new InfoType();
    $usMaleNameInfoType->setName('US_MALE_NAME');
    $usFemaleNameInfoType = new InfoType();
    $usFemaleNameInfoType->setName('US_FEMALE_NAME');
    $infoTypes = [$usMaleNameInfoType, $usFemaleNameInfoType];

    // Whether to include the matching string in the response
    $includeQuote = true;

    // Create the configuration object
    $inspectConfig = new InspectConfig();
    $inspectConfig->setMinLikelihood($minLikelihood);
    $inspectConfig->setMaxFindings($maxFindings);
    $inspectConfig->setInfoTypes($infoTypes);
    $inspectConfig->setIncludeQuote($includeQuote);

    $content = new ContentItem();
    $content->setType('text/plain');
    $content->setValue($string);

    $redactConfigs = [];
    foreach ($infoTypes as $infoType) {
        $redactConfig = new RedactContentRequest_ReplaceConfig();
        $redactConfig->setInfoType($infoType);
        $redactConfig->setReplaceWith($replaceString);
        $redactConfigs[] = $redactConfig;
    }

    // Run request
    $response = $dlp->redactContent($inspectConfig, [$content], $redactConfigs);
    $content = $response->getItems()[0];

    // Print the results
    print('Redacted String: ' . $content->getValue() . PHP_EOL);
}

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Data Loss Prevention API