Policies
A policy is a document that describes the intention, expectations, and overall approach that an organization uses to maintain certain processes and procedures within the organization.
Â
In this guide, we will learn how to retrieve policy information from the TrustCloud API using REST calls.Â
Retrieving an API Key
Â
To make requests to the TrustCloud API, an API key is required. To obtain an API key, follow the instructions in the Getting Started Guide to connect to the API. Â
Setting up the Request
Before we make any requests, we need to set up our HTTP headers. We need to include the API key in the “Authorization” header, and set the “x-trustcloud-api-version” header to 1 to ensure we are using the correct API version.
Here is an example of the headers we need to include in our HTTP request:
const headers = {
'Authorization': '',
'x-trustcloud-api-version': '1'
};
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
HttpGet request = new HttpGet("https://api.trustcloud.ai/policies");
request.setHeader("Authorization", "");
request.setHeader("x-trustcloud-api-version", "1");
using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Collections.Generic;
string apiKey = "";
string apiVersion = "1";
string baseUrl = "https://api.trustcloud.ai";
import requests
headers = {
'Authorization': '',
'x-trustcloud-api-version': '1'
}
package main
import (
"encoding/json"
"fmt"
"net/http"
)
apiKey := ""
url := "https://api.trustcloud.ai/policies"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Authorization", apiKey)
req.Header.Set("x-trustcloud-api-version", "1")
GET /policies HTTP/1.1
Host: api.trustcloud.ai
Authorization: Bearer
x-trustcloud-api-version: 1
Retrieving All Policies
To retrieve a list of all policies, make a GET request to the /policies endpoint using the headers set up in Step 2.
import axios from 'axios';
const headers = {
'Authorization': '',
'x-trustcloud-api-version': '1'
};
axios.get('https://api.trustcloud.ai/policies, { headers })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
HttpGet request = new HttpGet("https://api.trustcloud.ai/policies");
request.setHeader("Authorization", "");
request.setHeader("x-trustcloud-api-version", "1");
HttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(request);
try {
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody);
} finally {
response.close();
}
using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Collections.Generic;
string apiKey = "";
string apiVersion = "1";
string baseUrl = "https://api.trustcloud.ai";
var client = new RestClient(baseUrl);
var request = new RestRequest("/policies", Method.GET);
request.AddHeader("Authorization", apiKey);
request.AddHeader("x-trustcloud-api-version", apiVersion);
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
Console.WriteLine(response.Content);
}
else
{
Console.WriteLine("Error: " + response.Content);
}
import requests
headers = {
'Authorization': '',
'x-trustcloud-api-version': '1'
}
response = requests.get('https://api.trustcloud.ai/policies, headers=headers)
if response.status_code == 200:
policies = response.json()
print(policies)
else:
print('Error retrieving Policies:', response.text)
package main
import (
"encoding/json"
"fmt"
"net/http"
)
apiKey := ""
url := "https://api.trustcloud.ai/policies"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Authorization", apiKey)
req.Header.Set("x-trustcloud-api-version", "1")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
var data []interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v\n", data)
GET /policies HTTP/1.1
Host: api.trustcloud.ai
Authorization: Bearer
x-trustcloud-api-version: 1
Here is an example of the response we might receive:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 1234
[
{
"id": "550ec59e-64fd-474f-a528-9c1ba70707e3",
"catlogPolicyId": "POL-10",
"title": "Data Classification Policy",
"policyId": "30c060b0-2361-4c80-9170-384264ec110b",
"approvalStatus": "approved",
"policyVersionId": "faa8eaaa-8df6-4f86-b563-b20a7c358448",
"policyLastApprovedDate": "2023-04-19 01:10:44.239+00",
"version": "1.0.1",
"hightLights": null,
"description": "Define a data classification framework that can be used to determine the sensitivity of data and systems.",
"securityGroup": "Data Security and Privacy",
"policyState": "Unconfigured",
"policyPdfUrl": null,
"programGroupId": "d5353e36-5f14-4fe2-b203-55c6a6f863f9",
"owner": {
"id": "80b59d3d-bf45-4b3c-a005-92cafdd7009c"
},
"_metaData": {
"createdBy": "8739738d-48cc-4339-a7a3-50f28267964ba",
"createdDate": "2021-03-29 14:36:16.742+00",
"lastModifiedBy": "8739738d-48cc-4339-a7a3-50f28267964b",
"lastModifiedDate": "2023-04-19 01:10:44.249+00"
}
},...
]
Retrieving a Single Policy
To retrieve information about a single policy, include the policy ID in the URL by appending the ID to the /policies endpoint. In this example, we will retrieve the system with ID
550ec59e-64fd-474f-a528-9c1ba70707e3:
import axios from 'axios';
const headers = {
'Authorization': '',
'x-trustcloud-api-version': '1'
};
const id = '550ec59e-64fd-474f-a528-9c1ba70707e3'; // Replace with the ID of the policy you want to retrieve
axios.get(`https://api.trustcloud.ai/policies/${id}`, { headers })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
HttpGet request = new HttpGet("https://api.trustcloud.ai/policies/550ec59e-64fd-474f-a528-9c1ba70707e3");
request.setHeader("Authorization", "");
request.setHeader("x-trustcloud-api-version", "1");
HttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(request);
try {
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody);
} finally {
response.close();
}
using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Collections.Generic;
string apiKey = "";
string apiVersion = "1";
string baseUrl = "https://api.trustcloud.ai";
var client = new RestClient(baseUrl);
var request = new RestRequest("/policies/{id}", Method.GET);
request.AddHeader("Authorization", apiKey);
request.AddHeader("x-trustcloud-api-version", apiVersion);
request.AddUrlSegment("id", "550ec59e-64fd-474f-a528-9c1ba70707e3");
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
Console.WriteLine(response.Content);
}
else
{
Console.WriteLine("Error: " + response.Content);
}
import requests
headers = {
'Authorization': '',
'x-trustcloud-api-version': '1'
}
response = requests.get('https://api.trustcloud.ai/policies/550ec59e-64fd-474f-a528-9c1ba70707e3', headers=headers)
if response.status_code == 200:
system = response.json()
print(system)
else:
print('Error retrieving Policy:', response.text)
package main
import (
"encoding/json"
"fmt"
"net/http"
)
apiKey := ""
url := "https://api.trustcloud.ai/policies/550ec59e-64fd-474f-a528-9c1ba70707e3"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Authorization", apiKey)
req.Header.Set("x-trustcloud-api-version", "1")
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Authorization", apiKey)
req.Header.Set("x-trustcloud-api-version", "1")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
var data map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v\n", data)
GET /policies/550ec59e-64fd-474f-a528-9c1ba70707e3 HTTP/1.1
Host: API.trustcloud.ai
Authorization:
x-trustcloud-api-version: 1
Here is an example of the response we might receive:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 234
{
"id": "550ec59e-64fd-474f-a528-9c1ba70707e3",
"catlogPolicyId": "POL-10",
"title": "Data Classification Policy",
"policyId": "30c060b0-2361-4c80-9170-384264ec110b",
"approvalStatus": "approved",
"policyVersionId": "faa8eaaa-8df6-4f86-b563-b20a7c358448",
"policyLastApprovedDate": "2023-04-19 01:10:44.239+00",
"version": "1.0.1",
"hightLights": null,
"description": "Define a data classification framework that can be used to determine the sensitivity of data and systems.",
"securityGroup": "Data Security and Privacy",
"policyState": "Unconfigured",
"policyPdfUrl": null,
"programGroupId": "d5353e36-5f14-4fe2-b203-55c6a6f863f9",
"owner": {
"id": "80b59d3d-bf45-4b3c-a005-92cafdd7009c"
},
"_metaData": {
"createdBy": "8739738d-48cc-4339-a7a3-50f28267964ba",
"createdDate": "2021-03-29 14:36:16.742+00",
"lastModifiedBy": "8739738d-48cc-4339-a7a3-50f28267964b",
"lastModifiedDate": "2023-04-19 01:10:44.249+00"
}
}
Paging
If there are a large number of policies in a response, it is recommended to use paging to retrieve them in batches. To do this, we can use the limit and page query parameters.
Â
The limit parameter specifies the number of records to retrieve in each page. The default value is 100, but we can set it to any value up to 1000.
The page parameter specifies which page to retrieve. The first page is page 1.
For example, to retrieve the first 10 policies:
import axios from 'axios';
const headers = {
'Authorization': '',
'x-trustcloud-api-version': '1'
};
const limit = 10; // The number of Policies to retrieve per page
const page = 1; // The page number to retrieve (starts at 1)
axios.get('https://api.trustcloud.ai/policies, {
headers,
params: {
limit,
page,
}
})
.then(response => {
console.log(response.data);
console.log(response.headers.link); // log link header
})
.catch(error => {
console.log(error);
});
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
HttpGet request = new HttpGet("https://api.trustcloud.ai/policies?limit=10&page=1");
request.setHeader("Authorization", "");
request.setHeader("x-trustcloud-api-version", "1");
HttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(request);
try {
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody);
} finally {
response.close();
}
using System;
using System.Collections.Generic;
string apiKey = "";
string apiVersion = "1";
string baseUrl = "https://api.trustcloud.ai";
var client = new RestClient(baseUrl);
var request = new RestRequest("/policies", Method.GET);
request.AddHeader("Authorization", apiKey);
request.AddHeader("x-trustcloud-api-version", apiVersion);
request.AddParameter("limit", 50);
request.AddParameter("page", 1);
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
Console.WriteLine(response.Content);
}
else
{
Console.WriteLine("Error: " + response.Content);
}
import requests
headers = {
'Authorization': '',
'x-trustcloud-api-version': '1'
}
response = requests.get('https://api.trustcloud.ai/policies?limit=10&page=1', headers=headers', headers=headers)
if response.status_code == 200:
system = response.json()
print(system)
else:
print('Error retrieving policies:', response.text)
package main
import (
"encoding/json"
"fmt"
"net/http"
)
apiKey := ""
url := "https://api.trustcloud.ai/policies?page=1&limit=10"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Authorization", apiKey)
req.Header.Set("x-trustcloud-api-version", "1")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
var data []interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v\n", data)
GET /policies?limit=10&page=1 HTTP/1.1
Host: API.trustcloud.ai
Authorization:
x-trustcloud-api-version: 1
To retrieve the next set of policies, you can obtain the next URL from the link response header, or manually increment the page number. If link is null, that means there are no more pages.
import axios from 'axios';
const headers = {
'Authorization': '',
'x-trustcloud-api-version': '1'
};
const limit = 10; // The number of Policies to retrieve per page
const page = 1; // The page number to retrieve (starts at 1)
axios.get('https://api.trustcloud.ai/policies, {
headers,
params: {
limit,
page,
}
})
.then(response => {
console.log(response.data);
console.log(response.headers.link); // log link header
})
.catch(error => {
console.log(error);
});
String linkHeader = response.getFirstHeader("link").getValue();
String nextPageUrl = linkHeader.split(";")[0].replace("<", "").replace(">", "");
HttpGet nextPageRequest = new HttpGet(nextPageUrl);
nextPageRequest.setHeader("Authorization", "");
nextPageRequest.setHeader("x-trustcloud-api-version", "1");
using System;
using System.Collections.Generic;
string apiKey = "";
string apiVersion = "1";
string baseUrl = "https://api.trustcloud.ai";
var client = new RestClient(baseUrl);
var request = new RestRequest("/policies", Method.GET);
request.AddHeader("Authorization", apiKey);
request.AddHeader("x-trustcloud-api-version", apiVersion);
request.AddParameter("limit", 50);
request.AddParameter("page", 1);
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
Console.WriteLine(response.Content);
// retrieve next page
var nextPageUrl = response.Headers.FirstOrDefault(x => x.Name == "Link")?.Value?.Split(';')[0]?.Trim('<', '>');
if (!string.IsNullOrEmpty(nextPageUrl))
{
var nextPageRequest = new RestRequest(nextPageUrl, Method.GET);
nextPageRequest.AddHeader("Authorization", apiKey);
nextPageRequest.AddHeader("x-trustcloud-api-version", apiVersion);
IRestResponse nextPageResponse = client.Execute(nextPageRequest);
if (nextPageResponse.IsSuccessful)
{
Console.WriteLine(nextPageResponse.Content);
}
else
{
Console.WriteLine("Error retrieving next page: " + nextPageResponse.Content);
import requests
import re
headers = {
'Authorization': '',
'x-trustcloud-api-version': '1'
}
response = requests.get('https://api.trustcloud.ai/policies?limit=10&page=1', headers=headers)
if response.status_code == 200:
policies = response.json()
print(policies)
link_header = response.headers.get('link')
if link_header:
nextPage = re.search('<(.+)>', link_header)
else:
print('Error retrieving Policies:', response.text)
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
func main() {
apiKey := ""
url := "https://api.trustcloud.ai/policies"
// Create a new HTTP client
client := &http.Client{}
// Create a new HTTP request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
// Add the API key and version headers
req.Header.Set("Authorization", apiKey)
req.Header.Set("x-trustcloud-api-version", "1")
// Add the page and limit parameters
q := req.URL.Query()
q.Add("page", "1")
q.Add("limit", "10")
req.URL.RawQuery = q.Encode()
// Make the API call
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Check the response status code
if resp.StatusCode != http.StatusOK {
log.Fatal(resp.Status)
}
// Parse the link header to get the URL for the next page
linkHeader := resp.Header.Get("Link")
nextPageURL := ""
if linkHeader != "" {
links := strings.Split(linkHeader, ",")
for _, link := range links {
if strings.Contains(link, "rel=\"next\"") {
parts := strings.Split(link, ";")
if len(parts) > 0 {
nextPageURL = strings.Trim(parts[0], "<>")
}
}
}
}
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// Print the response body
fmt.Println(string(body))
// Make the API call for the next page if it exists
if nextPageURL != "" {
// Create a new HTTP request for the next page
nextPageReq, err := http.NewRequest("GET", nextPageURL, nil)
if err != nil {
log.Fatal(err)
}
// Add the API key and version headers to the next page request
nextPageReq.Header.Set("Authorization", apiKey)
nextPageReq.Header.Set("x-trustcloud-api-version", "1")
// Make the next page API call
nextPageResp, err := client.Do(nextPageReq)
if err != nil {
log.Fatal(err)
}
defer nextPageResp.Body.Close()
// Check the response status code for the next page
if nextPageResp.StatusCode != http.StatusOK {
log.Fatal(nextPageResp.Status)
}
// Read the response body for the next page
nextPageBody, err := ioutil.ReadAll(nextPageResp.Body)
if err != nil {
log.Fatal(err)
}
// Print the response body for the next page
fmt.Println(string(nextPageBody))
}
}
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 234
link: ; rel="next"
GET /policies?pag2&limit=10 HTTP/1.1
Host: API.trustcloud.ai
Authorization:
x-trustcloud-api-version: 1