piilite guide: install, detect, mask and restore

Everything you need to use piilite in a Node.js, browser or TypeScript project, from installing it to writing your own patterns.

How do I install piilite?

Install piilite from npm with npm install piilite. It has no dependencies and works with npm, pnpm, yarn and bun.

bash
npm install piilite
# or
pnpm add piilite
yarn add piilite
bun add piilite

How do I detect and redact PII?

Call detect(text) on a Piilite instance. It returns the redacted text, the list of detections and a map from each placeholder to its original value.

js
import { Piilite } from "piilite";

const shield = new Piilite();
const { redacted, redactionMap } = shield.detect(
  "Myself Ankit Kumar Gupta, call 9123456780 or pay ankit@okhdfcbank. PAN ABCPS1234K, OTP is 482913"
);

console.log(redacted);
// Myself [NAME_1], call [PHONE_IN_1] or pay [UPI_ID_1]. PAN [PAN_1], OTP is [OTP_1]

The result has the same shape as OpenRedaction's detect():

js
{
  original: "Myself Ankit Kumar Gupta, call 9123456780 or pay ankit@okhdfcbank.",
  redacted: "Myself [NAME_1], call [PHONE_IN_1] or pay [UPI_ID_1].",
  detections: [
    {type: "NAME", value: "Ankit Kumar Gupta", placeholder: "[NAME_1]", position: [7,24], severity: "high", confidence: 0.85},
    {type: "PHONE_IN", value: "9123456780", placeholder: "[PHONE_IN_1]", position: [31,41], severity: "high", confidence: 1},
    {type: "UPI_ID", value: "ankit@okhdfcbank", placeholder: "[UPI_ID_1]", position: [49,65], severity: "high", confidence: 0.9},
  ],
  redactionMap: {"[NAME_1]": "Ankit Kumar Gupta", "[PHONE_IN_1]": "9123456780", "[UPI_ID_1]": "ankit@okhdfcbank"},
  stats: { piiCount: 3, byType: {NAME: 1,PHONE_IN: 1,UPI_ID: 1}, processingTime: 1 },
}

Need only part of that? redact(text) returns the redacted string, scan(text) returns the matches without changing the text, and hasPII(text) returns true or false.

How do I use piilite with CommonJS or TypeScript?

piilite ships ES modules, CommonJS and TypeScript types, so import and require both work and types load automatically.

js
// ES modules / TypeScript
import { Piilite, redact, type DetectionResult } from "piilite";

// CommonJS
const { Piilite } = require("piilite");

Which masking modes are there?

piilite has five masking modes: placeholders (default), partial mask, full mask, remove, or your own function.

ModeInputOutput
placeholder (default)call 9123456780call [PHONE_IN_1]
maskcall 9123456780call XXXXXX6780
maskPAN ABCPS1234KPAN XXXXXX234K
maskrahul@gmail.comrXXXX@gmail.com
mask-all + maskChar: "*"call 9123456780call **********
removecall 9123456780 nowcall  now
functioncall 9123456780call <PHONE_IN>
js
new Piilite({ mode: "mask" }).redact("call 9123456780");                // "call XXXXXX6780"
new Piilite({ mode: (m) => `<${m.type}>` }).redact("call 9123456780");  // "call <PHONE_IN>"

How do I detect only some types?

Pass types to detect only those types, exclude to skip some, or a preset such as kyc. Opt-in types like LOCATION run only when you add them with include.

js
new Piilite({ types: ["AADHAAR", "PAN"] });          // only these
new Piilite({ exclude: ["IP_ADDRESS"] });            // everything else
new Piilite({ preset: "kyc", exclude: ["PINCODE"] }); // a preset, adjusted
new Piilite({ include: ["LOCATION"] });              // also redact cities

How do I keep some values, like my support email?

Add them to allow. Strings match exactly (case-insensitive); regular expressions match any detected value.

js
new Piilite({ allow: ["support@acme.in", /@acme\.in$/] });

How do I add my own patterns?

Pass customPatterns. Each pattern needs a type and a regex; a validator, priority and severity are optional. OpenRedaction patterns work unchanged.

js
const shield = new Piilite({
  customPatterns: [
    { type: "EMPLOYEE_ID", regex: /\bEMP-\d{5}\b/g, priority: 90, severity: "medium" },
    {
      type: "ORDER_REF",
      regex: /\bORD\d{8}\b/g,
      context: /order|booking/i,   // must appear near the match...
      requireContext: true,        // ...or the match is dropped
    },
  ],
});
shield.redact("id EMP-00421"); // "id [EMPLOYEE_ID_1]"

What options does piilite accept?

OptionDefaultWhat it does
presetnoneA ready-made type set: dpdp, kyc, fintech, pci, healthcare, hr, secrets, gdpr, hipaa, strict, precise
modeplaceholderHow PII is replaced (see above)
types / exclude / includeallChoose which types run
allow[]Values never redacted
minConfidence0.5Drop weaker matches; 0.4 catches more, 0.8 is stricter
skipFakeNumberstrueSkip test-looking numbers such as 9876543210; false masks them too
matchCasefalsetrue gives “RAHUL” and “Rahul” separate placeholders
propagatetrueMask values seen earlier wherever they appear again, incl. parts of names
keepCompanyAddressesfalsetrue leaves addresses after a company name unmasked
maskChar / keepLastX / 4Mask character and digits left visible in mask mode
placeholder[TYPE_n]Function that builds placeholder text
maxInputSize5,000,000Throws a RangeError above this length; stream larger input
onDetectnoneCalled after every detect(), for audit logs
customPatterns[]Your own patterns

What methods does a Piilite instance have?

MethodReturns
detect(text){ original, redacted, detections, redactionMap, stats }
redact(text)Redacted string
scan(text)Matches: { type, value, start, end, severity, confidence }[]
hasPII(text)boolean
redactObject(obj, { skipKeys }){ value, redactionMap } with every string redacted
restore(text, map, { fuzzy })Text with the original values back
detectAll(texts) / redactAll(texts)Several texts with shared placeholders
session()Shared placeholders across calls, plus restore and restoreStream
createStream() / redactStream() / transformStream()Streaming redaction (see LLM guide)
createRestoreStream(map) / restoreStream(iterable, map)Streaming restore that never shows half a placeholder

The module also exports redact, scan, detect, hasPII and restore with default options, plus the validators verhoeff, luhn, aadhaar, pan and gstin.

Can I use piilite in the browser?

Yes. piilite is plain JavaScript with no Node-only APIs, so bundlers like Vite, webpack and esbuild include it as is, and you can load it from a CDN as an ES module.

html
<script type="module">
  import { redact } from "https://cdn.jsdelivr.net/npm/piilite@0.3.0/src/index.js";
  console.log(redact("call 9123456780")); // "call [PHONE_IN_1]"
</script>

Last updated ยท piilite 0.3.0