Nightfall Documentation
  • Data Detection and Response
  • Posture Management
  • Data Exfiltration Prevention
  • Data Encryption
  • Firewall for AI
  • Data Classification and Discovery
  • Welcome
  • Introduction to Firewall for AI
    • Overview
    • Quickstart
    • Use Cases
    • Authentication and Security
  • Key Concepts
    • Entities and Terms to Know
    • Setting Up Nightfall
      • Creating API Key
      • Creating Detectors
      • Creating Detection Rules
      • Creating Policies
    • Alerting
    • Scanning Text
    • Scanning Files
      • Supported File Types
      • File Scanning and Webhooks
      • Uploading and Scanning API Calls
      • Special File Types
      • Specialized File Detectors
      • Webhooks and Asynchronous Notifications
        • Accessing Your Webhook Signing Key
        • Creating a Webhook Server
    • Scanning Features
      • Using Pre-Configured Detection Rules
        • Scanning Images for patterns using Custom Regex Detectors
      • Creating an Inline Detection Rule
      • Using Exclusion Rules
      • Using Context Rules
      • Using Redaction
      • Using Policies to Send Alerts
      • Detecting Secrets
      • PHI Detection Rules
    • Detector Glossary
    • Test Datasets
    • Errors
    • Nightfall Playground
  • Nightfall APIs
    • DLP APIs - Firewall for AI Platform
      • Rate Limits for Firewall APIs
    • DLP APIs - Native SaaS Apps
      • Policy User Scope Update API
      • Rate Limits for Native SaaS app APIs
  • Exfiltration Prevention APIs
    • Default
    • Models
  • Posture Management APIs
    • Default
    • Models
  • Nightfall Software Development Kit (SDK)
    • Overview
    • Java SDK
    • Python SDK
    • Go SDK
    • Node.JS SDK
  • Language Specific Guides
    • Overview
    • Python
    • Ruby
    • Java
  • Tutorials
    • GenAI Protection
      • OpenAI Prompt Sanitization Tutorial
      • Anthropic Prompt Sanitization Tutorial
      • LangChain Prompt Sanitization Tutorial
    • SaaS Protection
      • HubSpot DLP Tutorial
      • Zendesk DLP Tutorial
    • Observability Protection
      • Datadog DLP Tutorial
      • New Relic DLP Tutorial
    • Datastore Protection
      • Airtable DLP Tutorial
      • Amazon Kinesis DLP Tutorial
      • Amazon RDS DLP Tutorial
      • Amazon RDS DLP Tutorial - Full Scan
      • Amazon S3 DLP Tutorial
      • Elasticsearch DLP Tutorial
      • Snowflake DLP Tutorial
  • Nightfall Use Cases
    • Overview
    • GenAI Content Filtering-How to prevent exposure of sensitive data
    • Redacting Sensitive Data in 4 Lines of Code
    • Detecting Sensitive Data in SMS Automations
    • Building Endpoint DLP to Detect PII on Your Machine in Real-Time
    • Deploy a File Scanner for Sensitive Data in 40 Lines of Code
    • Using Scan API (with Python)
  • FAQs
    • What Can I do with the Firewall for AI
    • How quickly can I get started with Firewall for AI?
    • What types of data can I scan with API?
    • What types of detectors are supported out of the box?
    • Can I customize or bring my own detectors?
    • What is the pricing model?
    • How do I know my data is secure?
    • How do I get in touch with you?
    • Can I test out the detection and my own detection rules before writing any code?
    • How does Nightfall support custom data types?
    • How does Nightfall's Firewall for AI differs from other solutions?
  • Nightfall Playground
  • Login to Nightfall
  • Contact Us
Powered by GitBook
On this page
  • Usage
  • Complete Sample Code

Was this helpful?

Export as PDF
  1. Language Specific Guides

Ruby

This guide describes how to use Nightfall with the Ruby 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 Python SDK.

To follow along, you will need:

  • A Nightfall API Key

  • An existing Detection Rule

  • Data to scan. Note that the API interprets data as plaintext, so you may pass it in any structured or unstructured format.

  • A local Ruby 2.6 or greater environment.

Start by creating a new file called nightfall_demo.rb

Now we will walk through the code step by step. If you'd like to skip ahead you can see the complete code sample at the bottom of this page.

We will be using a few built-in Ruby libraries to run this sample API script.

# Load dependencies
require 'open-uri'
require 'net/http'
require 'json'

First, we will load some environment variables that will be used to interact with the Nightfall API. NIGHTFALL_API_KEY should be your Nightfall API Key, and NIGHTFALL_DETECTION_RULE_UUID should be the UUID for your existing Nightfall condition set.

# Load environment variables for Nightfall API
nightfall_api_key = ENV['NIGHTFALL_API_KEY']
detection_rule_uuid = ENV['NIGHTFALL_DETECTION_RULE_UUID']

Next, we will construct our payload to scan as an array. You can replace this with any data you'd like, or read plaintext from a file.

# Text data to scan
payload = [
    "The customer social security number is 458-02-6124",
    "No PII in this string",
    "My credit card number is 4916-6734-7572-5015"
]

Next, we build the HTTP request headers and body using the environment variables that we previously defined.

# Configure detection settings
config = { 
	"config": {
		"detectionRuleUUIDs": [detection_rule_uuid]
	},
	"payload": payload
}

Next, we build the HTTP object and make a request to the Nightfall API.

# Build API request
url = URI("https://api.nightfall.ai/v3/scan")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Accept"] = 'application/json'
request["Content-Type"] = 'application/json'
request["Authorization"] = "Bearer #{nightfall_api_key}"
request.body = config.to_json

Lastly, we make the API request and process the response from Nightfall. If there are sensitive findings in the response we pretty-print them to the console. If there are no findings, we print a message to the console. Otherwise, if there is a problem with the HTTP request we print the status code and message to the console.

# Make API request
response = http.request(request)

# Parse response
if response.code.to_i == 200 and response.body['findings']
    puts "This text contains sensitive data.\n\n"
    puts JSON.pretty_generate(JSON.parse(response.body))
elsif response.code.to_i == 200
    puts "No sensitive data found. Hooray!"
else
    puts "Something went wrong -- Response #{response.code}."
end

Usage

Now we can run our script:

ruby nightfall_demo.rb

If there are sensitive findings based on your Nightfall detection rule, you should see output similar to this in your console, corresponding to each of the 3 items inputted to scan in the payload.

This text contains sensitive data.

{
  "findings": [
    [
      {
        "finding": "458-02-6124",
        "detector": {
          "name": "US social security number (SSN)",
          "uuid": "e30d9a87-f6c7-46b9-a8f4-16547901e069"
        },
        "confidence": "VERY_LIKELY",
        "location": {
          "byteRange": {
            "start": 39,
            "end": 50
          },
          "codepointRange": {
            "start": 39,
            "end": 50
          }
        },
        "matchedDetectionRuleUUIDs": [
          "996a3c12-35d1-48cb-b858-5ee0841c652d"
        ],
        "matchedDetectionRules": [

        ]
      }
    ],
    [

    ],
    [
      {
        "finding": "4916-6734-7572-5015",
        "detector": {
          "name": "Credit card number",
          "uuid": "74c1815e-c0c3-4df5-8b1e-6cf98864a454"
        },
        "confidence": "VERY_LIKELY",
        "location": {
          "byteRange": {
            "start": 25,
            "end": 44
          },
          "codepointRange": {
            "start": 25,
            "end": 44
          }
        },
        "matchedDetectionRuleUUIDs": [
          "996a3c12-35d1-48cb-b858-5ee0841c652d"
        ],
        "matchedDetectionRules": [

        ]
      }
    ]
  ],
  "redactedPayload": [
    "",
    "",
    ""
  ]
}

Complete Sample Code

For your convenience, the complete Ruby code sample is shown below.


# nightfall_demo.rb

# Load dependencies
require 'open-uri'
require 'net/http'
require 'json'

# Load environment variables for Nightfall API
nightfall_api_key = ENV['NIGHTFALL_API_KEY']
detection_rule_uuid = ENV['NIGHTFALL_DETECTION_RULE_UUID']

# Text data to scan
payload = [
    "The customer social security number is 458-02-6124",
    "No PII in this string",
    "My credit card number is 4916-6734-7572-5015"
]

# Configure detection settings
config = { 
	"config": {
		"detectionRuleUUIDs": [detection_rule_uuid]
	},
	"payload": payload
}

# Build API request
url = URI("https://api.nightfall.ai/v3/scan")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Accept"] = 'application/json'
request["Content-Type"] = 'application/json'
request["Authorization"] = "Bearer #{nightfall_api_key}"
request.body = config.to_json

# Make API request
response = http.request(request)

# Parse response
if response.code.to_i == 200 and response.body['findings']
    puts "This text contains sensitive data.\n\n"
    puts JSON.pretty_generate(JSON.parse(response.body))
elsif response.code.to_i == 200
    puts "No sensitive data found. Hooray!"
else
    puts "Something went wrong -- Response #{response.code}."
end

PreviousPythonNextJava

Last updated 5 months ago

Was this helpful?

Congrats . You've successfully scanned text for sensitive data with Ruby using the Nightfall API.

🎉