Quba Docs

Anonymization

Learn how to use Quba's Anonymization API to transform sensitive data in text using various techniques like replacement, masking, hashing, and encryption.

import { SensitiveDataProtectionApi } from "@quba/sensitive-data-protection"


const text = `Patient John Smith (SSN: 123-45-6789) was admitted to 
  Dubai General Hospital on 2024-01-15. Contact: john.smith@email.com`

async function protectData(text: string) {
  const api = new SensitiveDataProtectionApi()

  return await api.anonymizeText({
    text,
    transformations: [
      {
        id: "replace",
        targets: [{ id: "entity", entity_type: "person" }],
        replacement: "[PATIENT]",
      },
      {
        id: "mask",
        targets: [{ id: "regex", pattern: "\\d{3}-\\d{2}-\\d{4}" }],
        masking_char: "*",
        chars_to_mask: 7,
        from_end: true,
      },
      {
        id: "redact",
        targets: [{ id: "entity", entity_type: "location" }],
      },
      {
        id: "sha256",
        targets: [{ id: "entity", entity_type: "email" }]
      },
    ],
  })
}

const response = await protectData(text)
console.log(response.transformed_text)
  // Output: Patient [PATIENT] (SSN: ***-**-6789) was admitted to
  // [REDACTED] on 2024-01-15. Contact: a1b2c3d4...

Supported Target Types

The API supports detecting these target types:

TargetDescription
entityModel based detection
regexCustom regex pattern matching

Transformation Types

Replace

Replace detected entities with a custom string:

{
  id: "replace",
  targets: [{ id: "entity", entity_type: "person" }],
  replacement: "[REDACTED]"
}

Redact

Remove detected entities entirely:

{
  id: "redact",
  targets: [{ id: "entity", entity_type: "email" }]
}

Mask

Mask part of the detected entity:

{
  id: "mask",
  targets: [{ id: "entity", entity_type: "phone" }],
  masking_char: "*",
  chars_to_mask: 8,
  from_end: true
}
// +1-555-123-4567 → +1-555-***-****

SHA256 / SHA512

Hash detected entities:

{
  id: "sha256",
  targets: [{ id: "entity", entity_type: "id" }]
}

Encrypt

Encrypt detected entities with a key:

{
  id: "encrypt",
  targets: [{ id: "entity", entity_type: "person" }],
  key: "your-encryption-key"
}

Regex Patterns

You can also target text using regex patterns:

{
  id: "replace",
  targets: [{ id: "regex", pattern: "\\d{4}-\\d{2}-\\d{2}" }],
  replacement: "[DATE]"
}