Scan Insights API Tutorial: Difference between revisions
No edit summary |
|||
(23 intermediate revisions by the same user not shown) | |||
Line 16: | Line 16: | ||
To view the '''complete code''' for all steps, see the section labelled [[#Code_Samples|'''Code Samples''']].'' | To view the '''complete code''' for all steps, see the section labelled [[#Code_Samples|'''Code Samples''']].'' | ||
For more information on the API capabilities used in this tutorial, check out [https://api.bigid.com/doc/scan-insights/ '''Scan Insights API Docs''']. | For more information on the API capabilities used in this tutorial, check out the[https://api.bigid.com/doc/scan-insights/ '''Scan Insights API Docs''']. | ||
== 1. Authenticate Using Your API Key == | == 1. Authenticate Using Your API Key == | ||
All API requests require authentication using a valid API key. Refer to BigID | All API requests require authentication using a valid API key. Refer to [https://developer.bigid.com/wiki/BigID_API/API_Tutorial '''BigID Documentation'''] to obtain your token. Then, define the Authorization header using the format `Authorization: Bearer YOUR_API_KEY`. This header must be included in every request to ensure proper authentication and access to BigID’s API endpoints. Throughout the tutorial, we will be using SAMPLE as our token. | ||
== 2. Obtain the scan ID(s) == | == 2. Obtain the scan ID(s) == | ||
Line 30: | Line 28: | ||
When making a GET request to this endpoint, you can customize the response using query parameters like '''sort''' and '''limit'''. By default, results are sorted in descending order based on '''updated_at''', meaning the most recent scans appear first. Since we're only interested in the most recent scan (this week’s scan) we’ll set limit=1 to return just the latest result. The final request will look like this: | When making a GET request to this endpoint, you can customize the response using query parameters like '''sort''' and '''limit'''. By default, results are sorted in descending order based on '''updated_at''', meaning the most recent scans appear first. Since we're only interested in the most recent scan (this week’s scan) we’ll set limit=1 to return just the latest result. The final request will look like this: | ||
<html> | |||
<iframe | |||
style="border:0px; width:100%; height:400px; border-radius:10px;" | |||
src='https://apiexplorer.bigid.tools/?url=scans/parent-scans%3Flimit%3D1&method=GET&headers=%5B%7B%22name%22%3A%22Authorization%22%2C%22value%22%3A%22SAMPLE%22%7D%5D&selectedSetting=headers'> | |||
</iframe> | |||
</html> | |||
Upon receiving a successful response, we can view various details about each scan. However, our primary focus is on obtaining the '''id''' attribute, which should be clearly labeled in the response data. | Upon receiving a successful response, we can view various details about each scan. However, our primary focus is on obtaining the '''id''' attribute, which should be clearly labeled in the response data. | ||
Focus on the `_id` attribute inside each object in the `scanChildren` array. This `_id` uniquely identifies the scan and is what you’ll use in the next steps. | |||
<syntaxhighlight lang="json"> | |||
{ | |||
"status": "string", | |||
"statusCode": 42.0, | |||
"message": "string", | |||
"data": { | |||
"totalCount": 42.0, | |||
"scanChildren": [ | |||
{ | |||
"_id": "string", <!-- **Look here!** --> | |||
"name": "string" | |||
// ... additional fields omitted for brevity | |||
} | |||
] | |||
} | |||
} | |||
</syntaxhighlight> | |||
== 3. Check Scan Status == | == 3. Check Scan Status == | ||
Once you have the scan ID for a particular scan, you can check its current '''status''' to determine whether it's still running or has completed. To do this, send a '''GET''' request to the ''/scans/{scan_id}/status'' endpoint, | Once you have the scan ID for a particular scan, you can check its current '''status''' to determine whether it's still running or has completed. To do this, send a '''GET''' request to the ''/scans/{scan_id}/status'' endpoint. In the below request, replace '''SAMPLE_SCAN_ID''' with the ID you previously retrieved. | ||
<html> | |||
<iframe | |||
style="border:0px; width:100%; height:400px; border-radius:10px;" | |||
src='https://apiexplorer.bigid.tools/?url=scans/SAMPLE_SCAN_ID/status&method=GET&headers=%5B%7B%22name%22%3A%22Authorization%22%2C%22value%22%3A%22SAMPLE%22%7D%5D&selectedSetting=headers'> | |||
</iframe> | |||
</html> | |||
=== Parse and Interpret Response === | === Parse and Interpret Response === | ||
Upon receiving a successful (200) response, the '''status''' field will indicate the scan’s current state. A value of '''true''' means the scan is currently active. A value of '''false''' means the scan has stopped, which likely means it has completed (though you’ll want to confirm completion by checking additional scan metadata if needed). | Upon receiving a successful (200) response, the '''status''' field will indicate the scan’s current state. | ||
* A value of '''true''' means the scan is currently active. | |||
* A value of '''false''' means the scan has stopped, which likely means it has completed (though you’ll want to confirm completion by checking additional scan metadata if needed). | |||
If you do not receive a successful response, check the [[#4. Troubleshooting|Troubleshooting]] section for common errors. | |||
== 4. Troubleshooting == | == 4. Troubleshooting == | ||
Line 48: | Line 77: | ||
{| class="wikitable" | {| class="wikitable" | ||
! style="font-size:95%; color:# | ! style="font-size:95%; color:#0e69b2; vertical-align:middle;" | '''Status Code''' | ||
! style="font-size:95%; color:# | ! style="font-size:95%; color:#0e69b2; vertical-align:middle;" | '''Example Response''' | ||
! style="font-size:95%; color:# | ! style="font-size:95%; color:#0e69b2; vertical-align:middle;" | '''What It Means''' | ||
! style="font-size:95%; color:# | ! style="font-size:95%; color:#0e69b2; vertical-align:middle;" | '''How to Fix It''' | ||
|- | |- | ||
| '''200''' || Successful response with scan data || Everything’s looking good! || Keep cruising. | | '''200''' || Successful response with scan data || Everything’s looking good! || Keep cruising. | ||
Line 65: | Line 94: | ||
== Code Samples == | == Code Samples == | ||
[ | <tabs> | ||
<tab name="Python"><syntaxhighlight lang="python" line> | |||
# Scan Insights API Tutorial | |||
import requests | |||
import json | |||
# Base URL of the BigID API | |||
base_url = "https://developer.bigid.com/api/v1" | |||
# Session token (replace SAMPLE with actual session token) | |||
headers = { | |||
"Authorization": "Bearer SAMPLE", | |||
"Content-Type": "application/json" | |||
} | |||
# Obtain the scan ID | |||
url_scans = f"{base_url}/scans/parent-scans" | |||
params = {"limit": 1} | |||
response = requests.get(url_scans, headers=headers, params=params) | |||
if response.status_code != 200: | |||
print(f"Failed to get parent scans: {response.status_code} - {response.text}") | |||
exit(1) | |||
data = response.json() | |||
scans = data.get("data", []) | |||
if not scans: | |||
print("No parent scans found.") | |||
exit(1) | |||
scan_id = scans[0]["id"] | |||
print(f"Latest parent scan ID: {scan_id}") | |||
# Check scan status using the scan ID | |||
url_status = f"{base_url}/scans/{scan_id}/status" | |||
response = requests.get(url_status, headers=headers) | |||
if response.status_code != 200: | |||
print(f"Failed to get scan status: {response.status_code} - {response.text}") | |||
exit(1) | |||
status_data = response.json() | |||
status = status_data.get("status") | |||
if status is True: | |||
print("Scan is currently ACTIVE.") | |||
elif status is False: | |||
print("Scan has STOPPED (likely completed).") | |||
else: | |||
print("Scan status unknown or missing.") | |||
</syntaxhighlight></tab> | |||
<tab name="JavaScript"><syntaxhighlight lang="javascript" line> | |||
// Scan Insights API Tutorial | |||
const baseUrl = "https://developer.bigid.com/api/v1"; // Base URL of the BigID API | |||
const headers = { | |||
"Authorization": "Bearer SAMPLE", // Replace SAMPLE with a actual session token | |||
"Content-Type": "application/json" | |||
}; | |||
// Obtain the scan ID | |||
async function getLatestParentScanId() { | |||
const url = `${baseUrl}/scans/parent-scans?limit=1`; | |||
const response = await fetch(url, { headers }); | |||
if (!response.ok) { | |||
throw new Error(`Failed to get parent scans: ${response.status} ${response.statusText}`); | |||
} | |||
const data = await response.json(); | |||
if (!data.data || data.data.length === 0) { | |||
throw new Error("No parent scans found."); | |||
} | |||
return data.data[0].id; | |||
} | |||
// Check Scan Status | |||
async function getScanStatus(scanId) { | |||
const url = `${baseUrl}/scans/${scanId}/status`; | |||
const response = await fetch(url, { headers }); | |||
if (!response.ok) { | |||
throw new Error(`Failed to get scan status: ${response.status} ${response.statusText}`); | |||
} | |||
const data = await response.json(); | |||
return data.status; | |||
} | |||
async function main() { | |||
try { | |||
console.log("Fetching latest parent scan ID..."); | |||
const scanId = await getLatestParentScanId(); | |||
console.log(`Latest parent scan ID: ${scanId}`); | |||
console.log("Checking scan status..."); | |||
const status = await getScanStatus(scanId); | |||
if (status === true) { | |||
console.log("Scan is currently ACTIVE."); | |||
} else if (status === false) { | |||
console.log("Scan has STOPPED (likely completed)."); | |||
} else { | |||
console.log("Scan status unknown or missing."); | |||
} | |||
} catch (error) { | |||
console.error("Oops! Something went wrong:", error.message); | |||
} | |||
} | |||
main(); | |||
</syntaxhighlight> | |||
</tab> | |||
</tabs> | |||
<html> | |||
<style> | |||
.tabs-tabbox > .tabs-container { | |||
margin-top: -1px; | |||
padding: | |||
2px 6px; | |||
border-radius: | |||
8px; | |||
position: relative; | |||
border: | |||
2px solid #848484; | |||
width: inherit; | |||
max-width: inherit; | |||
min-width: inherit; | |||
box-shadow: | |||
0px 4px 6px 1px rgba(0, 0, 0, 0.1); | |||
z-index: 1; | |||
} | |||
.tabs-tabbox > .tabs-label { | |||
margin: | |||
0 3px; | |||
border-bottom: | |||
none; | |||
border-radius: | |||
4px 4px 0 0; | |||
position: relative; | |||
display: inline-block; | |||
vertical-align: bottom; | |||
padding-left: 10px; | |||
padding-right: 10px; | |||
padding-bottom: 3px; | |||
padding-top: 3px; | |||
} | |||
.tabs-tabbox > .tabs-input:checked + .tabs-label, .tabs-input-0:checked + .tabs-input-1 + .tabs-label { | |||
background-color: #0e69b2 !important; | |||
border-color: | |||
#848484; | |||
z-index: 0; | |||
color: white; | |||
} | |||
.tabs-label { | |||
cursor: pointer; | |||
border: | |||
2px solid #848484; | |||
} | |||
.mw-body .tabs-label { | |||
background-color: #ffffff26; | |||
} | |||
</style> | |||
</html> | |||
== Summary == | == Summary == | ||
Congratulations, you can now confidently explore and evaluate the status of scans using the Scan Insights API! With these skills, you’re equipped to monitor your organization’s data scans effectively, ensuring timely detection and management of sensitive information. | Congratulations, you can now confidently explore and evaluate the status of scans using the Scan Insights API! With these skills, you’re equipped to monitor your organization’s data scans effectively, ensuring timely detection and management of sensitive information. |
Latest revision as of 15:38, 2 July 2025
- How to authenticate API requests using an API key.
- How to obtain the scan ID(s) for scans of interest.
- How to check scan status using the respective scan ID(s).
- How to parse and interpret response data to confirm scan completion.
- How to troubleshoot common issues like failed scans or missing data.
In this tutorial, we'll use SAMPLE as our session token. This is unique to the training sandbox and will not work in other environments. See BigID API/Tutorial for information on authenticating with BigID.
To view the complete code for all steps, see the section labelled Code Samples.
For more information on the API capabilities used in this tutorial, check out theScan Insights API Docs.
1. Authenticate Using Your API Key
All API requests require authentication using a valid API key. Refer to BigID Documentation to obtain your token. Then, define the Authorization header using the format `Authorization: Bearer YOUR_API_KEY`. This header must be included in every request to ensure proper authentication and access to BigID’s API endpoints. Throughout the tutorial, we will be using SAMPLE as our token.
2. Obtain the scan ID(s)
Depending on the information you're interested in, you can retrieve scan IDs from one of two endpoints. Since our goal is to check the status of this week’s scheduled scan, we’ll focus on the Parent Scan endpoint (/scans/parent-scans), which provides a high-level overview of each full scan execution.
Querying
When making a GET request to this endpoint, you can customize the response using query parameters like sort and limit. By default, results are sorted in descending order based on updated_at, meaning the most recent scans appear first. Since we're only interested in the most recent scan (this week’s scan) we’ll set limit=1 to return just the latest result. The final request will look like this:
Upon receiving a successful response, we can view various details about each scan. However, our primary focus is on obtaining the id attribute, which should be clearly labeled in the response data.
Focus on the `_id` attribute inside each object in the `scanChildren` array. This `_id` uniquely identifies the scan and is what you’ll use in the next steps.
{
"status": "string",
"statusCode": 42.0,
"message": "string",
"data": {
"totalCount": 42.0,
"scanChildren": [
{
"_id": "string", <!-- **Look here!** -->
"name": "string"
// ... additional fields omitted for brevity
}
]
}
}
3. Check Scan Status
Once you have the scan ID for a particular scan, you can check its current status to determine whether it's still running or has completed. To do this, send a GET request to the /scans/{scan_id}/status endpoint. In the below request, replace SAMPLE_SCAN_ID with the ID you previously retrieved.
Parse and Interpret Response
Upon receiving a successful (200) response, the status field will indicate the scan’s current state.
- A value of true means the scan is currently active.
- A value of false means the scan has stopped, which likely means it has completed (though you’ll want to confirm completion by checking additional scan metadata if needed).
If you do not receive a successful response, check the Troubleshooting section for common errors.
4. Troubleshooting
If your request fails, here’s what the server might tell you, and how to fix it:
Status Code | Example Response | What It Means | How to Fix It |
---|---|---|---|
200 | Successful response with scan data | Everything’s looking good! | Keep cruising. |
400 | { "error": "Scan ID is invalid" } |
Bad or malformed scan ID provided | Double-check the scan ID you’re using. |
404 | { "error": "Scan 1234 was not found" } |
Scan ID doesn’t exist | Make sure the ID is valid and fetched from the parent scans endpoint. |
401 | Unauthorized | API key missing or invalid | Verify your API key and authorization header. |
500 | { "status": "error", "message": "Server error", "errors": [{}] } |
BigID server hit a snag (internal error) | Wait a moment and retry. If it persists, reach out to support. |
Code Samples
# Scan Insights API Tutorial
import requests
import json
# Base URL of the BigID API
base_url = "https://developer.bigid.com/api/v1"
# Session token (replace SAMPLE with actual session token)
headers = {
"Authorization": "Bearer SAMPLE",
"Content-Type": "application/json"
}
# Obtain the scan ID
url_scans = f"{base_url}/scans/parent-scans"
params = {"limit": 1}
response = requests.get(url_scans, headers=headers, params=params)
if response.status_code != 200:
print(f"Failed to get parent scans: {response.status_code} - {response.text}")
exit(1)
data = response.json()
scans = data.get("data", [])
if not scans:
print("No parent scans found.")
exit(1)
scan_id = scans[0]["id"]
print(f"Latest parent scan ID: {scan_id}")
# Check scan status using the scan ID
url_status = f"{base_url}/scans/{scan_id}/status"
response = requests.get(url_status, headers=headers)
if response.status_code != 200:
print(f"Failed to get scan status: {response.status_code} - {response.text}")
exit(1)
status_data = response.json()
status = status_data.get("status")
if status is True:
print("Scan is currently ACTIVE.")
elif status is False:
print("Scan has STOPPED (likely completed).")
else:
print("Scan status unknown or missing.")
// Scan Insights API Tutorial
const baseUrl = "https://developer.bigid.com/api/v1"; // Base URL of the BigID API
const headers = {
"Authorization": "Bearer SAMPLE", // Replace SAMPLE with a actual session token
"Content-Type": "application/json"
};
// Obtain the scan ID
async function getLatestParentScanId() {
const url = `${baseUrl}/scans/parent-scans?limit=1`;
const response = await fetch(url, { headers });
if (!response.ok) {
throw new Error(`Failed to get parent scans: ${response.status} ${response.statusText}`);
}
const data = await response.json();
if (!data.data || data.data.length === 0) {
throw new Error("No parent scans found.");
}
return data.data[0].id;
}
// Check Scan Status
async function getScanStatus(scanId) {
const url = `${baseUrl}/scans/${scanId}/status`;
const response = await fetch(url, { headers });
if (!response.ok) {
throw new Error(`Failed to get scan status: ${response.status} ${response.statusText}`);
}
const data = await response.json();
return data.status;
}
async function main() {
try {
console.log("Fetching latest parent scan ID...");
const scanId = await getLatestParentScanId();
console.log(`Latest parent scan ID: ${scanId}`);
console.log("Checking scan status...");
const status = await getScanStatus(scanId);
if (status === true) {
console.log("Scan is currently ACTIVE.");
} else if (status === false) {
console.log("Scan has STOPPED (likely completed).");
} else {
console.log("Scan status unknown or missing.");
}
} catch (error) {
console.error("Oops! Something went wrong:", error.message);
}
}
main();
Summary
Congratulations, you can now confidently explore and evaluate the status of scans using the Scan Insights API! With these skills, you’re equipped to monitor your organization’s data scans effectively, ensuring timely detection and management of sensitive information.