import { Credentials, Env, LogLevel, Skyflow, SkyflowConfig, VaultConfig, SkyflowError, DeidentifyTextRequest, DeidentifyTextOptions, TokenFormat, TokenType, Transformations, DetectEntities, DeidentifyTextResponse } from 'skyflow-node'; /** * Skyflow Deidentify Text Example * * This example demonstrates how to: * 1. Configure credentials * 2. Set up vault configuration * 3. Create a deidentify text request * 4. Use all available options for de-identification * 5. Handle response and errors */ async function performDeidentifyText() { try { // Step 1: Configure Credentials const credentials: Credentials = { path: 'path-to-credentials-json', // Path to credentials file }; // Step 2: Configure Vault const primaryVaultConfig: VaultConfig = { vaultId: '', // Unique vault identifier clusterId: '', // From vault URL env: Env.PROD, // Deployment environment credentials: credentials // Authentication method }; // Step 3: Configure Skyflow Client const skyflowConfig: SkyflowConfig = { vaultConfigs: [primaryVaultConfig], logLevel: LogLevel.INFO, // Recommended to use LogLevel.ERROR in production environment. }; // Initialize Skyflow Client const skyflowClient: Skyflow = new Skyflow(skyflowConfig); // Step 4: Prepare Deidentify Text Request const deidentifyTextRequest = new DeidentifyTextRequest( 'My SSN is 123-45-6789 and my card is 4111 1111 1111 1111.', // Text to be deidentified ); // Step 5: Configure DeidentifyTextOptions const options = new DeidentifyTextOptions(); // setEntities: Specify which entities to deidentify options.setEntities([DetectEntities.CREDIT_CARD, DetectEntities.SSN]); // Allowlist: regex patterns whose matches will NOT be de-identified // options.setAllowRegexList(['']); // Denylist: restrict de-identification to only entities matching these patterns // options.setRestrictRegexList(['']); // setTokenFormat: choose how de-identified entities are represented as tokens const tokenFormat = new TokenFormat(); // Apply one token type as the default for all entity types tokenFormat.setDefault(TokenType.VAULT_TOKEN); // --- Per-entity-type token format overrides (optional) --- // Use vault tokens only for specific entity types // tokenFormat.setVaultToken([DetectEntities.SSN, DetectEntities.CREDIT_CARD]); // Use an entity-unique counter token for specific entity types // tokenFormat.setEntityUniqueCounter([DetectEntities.NAME]); // Use entity-only (no token) for specific entity types // tokenFormat.setEntityOnly([DetectEntities.DATE_OF_BIRTH]); options.setTokenFormat(tokenFormat); // setTransformations: Specify custom transformations for entities const transformations = new Transformations(); transformations.setShiftDays({ max: 30, // Maximum shift days min: 30, // Minimum shift days entities: [DetectEntities.DOB], // Entities to apply the shift }); options.setTransformations(transformations); // Step 6: Call deidentifyText API const response: DeidentifyTextResponse = await skyflowClient .detect(primaryVaultConfig.vaultId) .deidentifyText(deidentifyTextRequest, options); // Handle Successful Response console.log('Deidentify Text Response:', response); } catch (error) { // Comprehensive Error Handling if (error instanceof SkyflowError) { console.error('Skyflow Specific Error:', { httpCode: error.error?.httpCode, grpcCode: error.error?.grpcCode, httpStatus: error.error?.httpStatus, message: error.message, details: error.error?.details, }); } else { console.error('Unexpected Error:', JSON.stringify(error)); } } } // Invoke the deidentify text function performDeidentifyText();