Get Failing Resources for a Test

Step 1: Get list of failing tests

Get the list of monitored tests in Drata and filter for the ones that with a failed result. Take note of the test ID you’d like to look into.

import fetch from 'node-fetch';

async function run() {
  const query = new URLSearchParams({
    page: '1',
    limit: '20',
    q: 'string',
    checkResultStatus: 'READY',
    checkResultStatuses: 'READY',
    checkStatus: 'UNUSED',
    source: 'DRATA',
    type: 'POLICY',
    controlOwner: '0',
    getAll: 'true',
    includeArchivedControls: 'true',
    controlId: '0',
    excludeControlId: '0',
    excludeTestIds: '0',
    sortByName: 'true',
    reportInterval: 'WEEKLY',
    drafts: 'true'
  }).toString();

  const resp = await fetch(
    `https://public-api.drata.com/public/monitors?${query}`,
    {
      method: 'GET',
      headers: {
        Authorization: 'Bearer <YOUR_API_KEY_HERE>'
      }
    }
  );

  const data = await resp.text();
  console.log(data);
}

run();
Loading...

Step 2: Run the test again

Run the test again to get the latest result using the test ID.

import fetch from 'node-fetch';

async function run() {
  const testId = 'YOUR_testId_PARAMETER';
  const workspaceId = 'YOUR_workspaceId_PARAMETER';
  const resp = await fetch(
    `https://public-api.drata.com/public/workspaces/${workspaceId}/autopilot/${testId}/retest`,
    {
      method: 'POST',
      headers: {
        Authorization: 'Bearer <YOUR_API_KEY_HERE>'
      }
    }
  );

  const data = await resp.text();
  console.log(data);
}

run();
Loading...

Step 3: Get the test details

Get the test details using the test ID to get the failing resources for that test.

import fetch from 'node-fetch';

async function run() {
  const testId = 'YOUR_testId_PARAMETER';
  const workspaceId = 'YOUR_workspaceId_PARAMETER';
  const resp = await fetch(
    `https://public-api.drata.com/public/workspaces/${workspaceId}/monitors/${testId}/details`,
    {
      method: 'GET',
      headers: {
        Authorization: 'Bearer <YOUR_API_KEY_HERE>'
      }
    }
  );

  const data = await resp.text();
  console.log(data);
}

run();
Loading...

Step 4: Repeat for each test you want to get data for

Repeat the above steps with all tests with a failed result you identified in step 1.