# Java

This guide describes how to use Nightfall with the Java programming language.

The example below will demonstrate how to use Nightfall’s text scanning functionality to verify whether a string contains sensitive PII using the Nightfall Java SDK.

In this tutorial, we will be downloading, setting up, and using the Java SDK provided by Nightfall.

To make a request to the Nightfall API you will need:

* A Nightfall API key
* Plaintext data to scan.

You can read more about obtaining [API key](https://help.nightfall.ai/nightfall-firewall-for-ai/key-concepts/setting-up-nightfall/creating-api-key) or about our available [data detectors](https://help.nightfall.ai/nightfall-ai/detection-engine/nightfall-detector-glossary) from the linked reference guides.

You can add the Nightfall package to your project by adding a dependency to your `pom.xml`:

```xml
<!--pom.xml-->

<?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>

     <groupId>com.foo</groupId>
     <artifactId>my-artifact</artifactId>
     <version>1.0.0</version>

     <name>${project.groupId}:${project.artifactId}</name>
     <packaging>jar</packaging>

     <dependencies>
         <dependency>
             <groupId>ai.nightfall</groupId>
             <artifactId>scan-api</artifactId>
             <version>1.0.1</version>
         </dependency>
     </dependencies>
 </project>
```

First add the required imports to the top of the file.

These are the objects we will use from the Nightfall SDK, as well as some collection classes for data handling.

```java
//List of imports

import ai.nightfall.scan.NightfallClient;
 import ai.nightfall.scan.model.Confidence;
 import ai.nightfall.scan.model.DetectionRule;
 import ai.nightfall.scan.model.Detector;
 import ai.nightfall.scan.model.LogicalOp;
 import ai.nightfall.scan.model.NightfallAPIException;
 import ai.nightfall.scan.model.ScanTextConfig;
 import ai.nightfall.scan.model.ScanTextRequest;
 import ai.nightfall.scan.model.ScanTextResponse;

 import java.util.Arrays;
 import java.util.List;
```

We can then declare some data to scan in a `List`:

<pre class="language-java"><code class="lang-java"><strong>//Sample Payload
</strong><strong>
</strong><strong>List&#x3C;String> payload = Arrays.asList(
</strong>  "hello", 
  "world", 
  "my data is 4242-4242-4242-4242 but shhhh 🙊 ", 
  "my ssn is 678-99-8212"
);
</code></pre>

Create a `ScanTextRequest` to scan the payload with. First create a new instance of the credit card detector, and set to trigger if there are any findings that are confidence `LIKELY` or above.

Add a second detector, looking for social security numbers. Set it to be triggered if there is at least a possible finding.

Combine these detectors into a detection rule, which will return findings if either of these detectors are triggered.

Finally, combine the payload and configuration together as a new `ScanTextRequest`, and return it.

```java
//Build the Scan Request

public static ScanTextRequest buildScanTextRequest() {
  	// Define some detectors to use to scan your data
  	Detector creditCard = new Detector("CREDIT_CARD_NUMBER");
  	creditCard.setMinConfidence(Confidence.LIKELY);
  	creditCard.setMinNumFindings(1);

    Detector ssn = new Detector("US_SOCIAL_SECURITY_NUMBER");
    ssn.setMinConfidence(Confidence.POSSIBLE);
    ssn.setMinNumFindings(1);
    DetectionRule rule = new DetectionRule(Arrays.asList(creditCard, ssn), LogicalOp.ANY);
    ScanTextConfig config = ScanTextConfig.fromDetectionRules(Arrays.asList(rule), 20);

    return new ScanTextRequest(payload, config);
}
```

Use the `ScanTextRequest` instance with a NightfallClient to send your request to Nightfall.

The resulting `ScanTextResponse` may be used to print out the results:

```java
//Run the Scan Request

public class Runner {
     public static void main(String[] args) {
         try (NightfallClient c = NightfallClient.Builder.defaultClient()) {
             try {
                 ScanTextResponse response = c.scanText(buildScanTextRequest());
                 System.out.println("response: " + response.getFindings());
             } catch (NightfallAPIException e) {
                 // not a checked exception, just for illustrative purposes
                 System.out.println("got error: " + e);
             }
         }
     }
 }
```

And that's it :tada:

You are now ready to use the Java SDK for other scenarios.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.nightfall.ai/developer-api/language_specific_guides/java.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
