|
1 | | -## Nightfall Node.js SDK |
| 1 | +# Nightfall Node.js SDK |
2 | 2 |
|
3 | | -A Node.js SDK to call the Nightfall API. |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +**Embed Nightfall scanning and detection functionality into Node.js applications** |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +This SDK provides JavaScript/TypeScript bindings for the Nightfall API. It allows you to add functionality to your applications to |
| 11 | +scan plain text and files in order to detect different categories of information. You can leverage any of |
| 12 | +the detectors in Nightfall's pre-built library, or you may programmatically define your own custom detectors. |
| 13 | + |
| 14 | +Additionally, this library provides convenience features such as encapsulating the steps to chunk and upload files. |
| 15 | + |
| 16 | +To obtain an API Key, login to the [Nightfall dashboard](https://app.nightfall.ai/) and click the section |
| 17 | +titled "Manage API Keys". |
| 18 | + |
| 19 | +See our [developer documentation](https://docs.nightfall.ai/docs/entities-and-terms-to-know) for more details about |
| 20 | +integrating with the Nightfall API. |
| 21 | + |
| 22 | +## Dependencies |
| 23 | + |
| 24 | +The Nightfall Node.js SDK is compiled to ES2018 and supports Node.js v10.0.0 or later. |
| 25 | + |
| 26 | +For a full list of external dependencies please consult `package.json`. |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +### NPM |
| 31 | + |
| 32 | +`npm install nightfall-node-sdk` |
| 33 | + |
| 34 | +### Yarn |
| 35 | + |
| 36 | +`yarn add nightfall-node-sdk` |
| 37 | + |
| 38 | +### Building Locally |
| 39 | + |
| 40 | +Alternatively, if you would like to build the project yourself: |
| 41 | + |
| 42 | +1. Clone this git repository |
| 43 | +2. Run `npm install` from the top-level directory |
| 44 | +3. Run `npm run build` to compile the TypeScript codebase into the ./dist folder (in some cases, you may be required to manually create the ./dist folder before you run this command) |
| 45 | +4. Import the client like so: `import { Nightfall } from './dist'` |
| 46 | + |
| 47 | +## Usage |
| 48 | + |
| 49 | +### Scanning Plain Text |
| 50 | + |
| 51 | +Nightfall provides pre-built detector types, covering data types ranging from PII to PHI to credentials. The following snippet shows an example of how to scan using pre-built detectors. |
| 52 | + |
| 53 | +#### Sample Code |
| 54 | + |
| 55 | +```typescript |
| 56 | +// By default, the client reads your API key from the environment variable NIGHTFALL_API_KEY |
| 57 | +const nfClient = new Nightfall(); |
| 58 | + |
| 59 | +const response = await nfClient.scanText(['My credit card number is 4242-4242-4242-4242'], { |
| 60 | + detectionRules: [ |
| 61 | + { |
| 62 | + name: 'Secrets Scanner', |
| 63 | + logicalOp: 'ANY', |
| 64 | + detectors: [ |
| 65 | + { |
| 66 | + minNumFindings: 1, |
| 67 | + minConfidence: Detector.Confidence.Possible, |
| 68 | + displayName: 'Credit Card Number', |
| 69 | + detectorType: Detector.Type.Nightfall, |
| 70 | + nightfallDetector: 'CREDIT_CARD_NUMBER', |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + ], |
| 75 | +}); |
| 76 | + |
| 77 | +if (response.isError) { |
| 78 | + console.log(response.getError()); |
| 79 | +} else { |
| 80 | + response.data.findings.forEach((finding) => { |
| 81 | + if (finding.length > 0) { |
| 82 | + finding.forEach((result) => { |
| 83 | + console.log(`Finding: ${result.finding}, Confidence: ${result.confidence}`); |
| 84 | + }); |
| 85 | + } |
| 86 | + }); |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +### Scanning Files |
| 91 | + |
| 92 | +Scanning common file types like PDF's or office documents typically requires cumbersome text |
| 93 | +extraction methods like OCR. |
| 94 | + |
| 95 | +Rather than implementing this functionality yourself, the Nightfall API allows you to upload the |
| 96 | +original files, and then we'll handle the heavy lifting. |
| 97 | + |
| 98 | +The file upload process is implemented as a series of requests to upload the file in chunks. This library |
| 99 | +provides a single method that wraps the steps required to upload your file. Please refer to the |
| 100 | +[API Reference](https://docs.nightfall.ai/reference) for more details. |
| 101 | + |
| 102 | +The file is uploaded synchronously, but as files can be arbitrarily large, the scan itself is conducted asynchronously. |
| 103 | +The results from the scan are delivered by webhook; for more information about setting up a webhook server, refer to |
| 104 | +[the docs](https://docs.nightfall.ai/docs/creating-a-webhook-server). |
| 105 | + |
| 106 | +#### Sample Code |
| 107 | + |
| 108 | +```typescript |
| 109 | +// By default, the client reads your API key from the environment variable NIGHTFALL_API_KEY |
| 110 | +const nfClient = new Nightfall(); |
| 111 | + |
| 112 | +const response = await nfClient.scanFile('./customer-details.txt', { |
| 113 | + detectionRules: [ |
| 114 | + { |
| 115 | + name: 'Secrets Scanner', |
| 116 | + logicalOp: 'ANY', |
| 117 | + detectors: [ |
| 118 | + { |
| 119 | + minNumFindings: 1, |
| 120 | + minConfidence: Detector.Confidence.Possible, |
| 121 | + displayName: 'Credit Card Number', |
| 122 | + detectorType: Detector.Type.Nightfall, |
| 123 | + nightfallDetector: 'CREDIT_CARD_NUMBER', |
| 124 | + }, |
| 125 | + ], |
| 126 | + }, |
| 127 | + ], |
| 128 | + webhookURL: 'https://my-service.com/nightfall/listener', |
| 129 | +}); |
| 130 | + |
| 131 | +if (response.isError) { |
| 132 | + console.log(response.getError()); |
| 133 | +} |
| 134 | + |
| 135 | +// Save this ID to check for findings when you receive a webhook event from us |
| 136 | +console.log(response.data.id); |
| 137 | +``` |
| 138 | + |
| 139 | +## Contributing |
| 140 | + |
| 141 | +Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature |
| 142 | +or change. |
| 143 | + |
| 144 | +Refer to `CONTRIBUTING.md` for the full details. |
| 145 | + |
| 146 | +## License |
| 147 | + |
| 148 | +This code is licensed under the terms of the MIT License. See [here](https://opensource.org/licenses/MIT) |
| 149 | +for more information. |
0 commit comments