Quickstart: Using the command-line tool

This page shows you how to perform basic tasks in the Cloud Data Loss Prevention API using a command-line interface.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the GCP Console, on the project selector page, select or create a GCP project.

    Go to the project selector page

  3. Make sure that billing is enabled for your Google Cloud Platform project. Learn how to enable billing.

  4. Enable the DLP API.

    Enable the API

  5. Set up authentication:
    1. In the GCP Console, go to the Create service account key page.

      Go to the Create Service Account Key page
    2. From the Service account list, select New service account.
    3. In the Service account name field, enter a name.
    4. From the Role list, select Project > Owner.

      Note: The Role field authorizes your service account to access resources. You can view and change this field later by using the GCP Console. If you are developing a production app, specify more granular permissions than Project > Owner. For more information, see granting roles to service accounts.
    5. Click Create. A JSON file that contains your key downloads to your computer.
  6. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

  7. Download and install Node.js and NPM.


Inspecting content requires the serviceusage.services.use permission for the project that's specified in parent. The roles/editor, roles/owner, and roles.dlp.user roles contain the required permission or you can define your own custom role.

To give your user the dlp.admin role at the project level:

Web UI

  1. Open the Cloud Identity and Access Management page in the Google Cloud Platform Console.

    Open the IAM page

  2. If a project hasn't already been selected, click Select a project.

  3. Select your project and click Open.

  4. On the Cloud Identity and Access Management page, click Add.

  5. In the Add members dialog:

    • For Members type the user email: test@example.com.
    • For Roles, click Select a role and choose Cloud DLP > DLP User.

  6. Click Add.


  1. To add a single binding to the project's Cloud IAM policy, type the following command. Replace [PROJECT_ID] with your project ID and '[SERVICE_ID]' with the service account to be used.

    gcloud projects add-iam-policy-binding [PROJECT_ID] --member serviceAccount:[SERVICE_ID] --role roles/dlp.user

  2. The command writes the updated policy to the console window:

- members:
  - group:test@example.com
    role: roles/dlp.user

Set up Cloud DLP CLI app

  1. Clone or download a ZIP file of the Node.js DLP client library.

  2. Open a command-line tool and navigate to the samples directory.

  3. Install the app dependencies by running npm install while in the samples directory.

  4. If you haven't done so already, create the GCLOUD_PROJECT environment variable and set it to the project ID of the Google Cloud Platform project you set up to use with Cloud DLP.

Inspect a string for sensitive information

This section shows you how to ask the service to scan sample text using the inspect string command.

  1. Open a command-line tool and navigate to the samples folder of the Node.js samples repository.

  2. Run the following command:

    node inspect.js string "My email address is joe@example.com."

You should receive the following output:


Congratulations! You've sent your first request to the Cloud Data Loss Prevention API!

What's next?

Was this page helpful? Let us know how we did:

Send feedback about...

Data Loss Prevention Documentation