Skip to main content

API Usage Examples

The following API usage examples make use of the following generated API key: UjlMS+0m1l9IUZjpjWyJG8gbnv2Mta4T.

C# Example

In the following example, we will create a company using C#.

/** This example makes use of the json-rpc-csharp project:
 *  https://github.com/adamashton/json-rpc-csharp
 *

/String apiURL =
  "https://cloud.gravityzone.bitdefender.com/api/v1.0/jsonrpc/";

// Make a request on the companies API.
Client rpcClient = new Client(apiURL + "companies");
String apiKey = "UjlMS+0m1l9IUZjpjWyJG8gbnv2Mta4T";
String userPassString = apiKey + ":";
String authorizationHeader = System.Convert.ToBase64String(
  System.Text.Encoding.UTF8.GetBytes(userPassString));

rpcClient.Headers.Add("Authorization",
  "Basic " + authorizationHeader);

JToken parameters = new JObject();
parameters["type"] = 1;
parameters["name"] = "Test Company";
parameters["address"] = @"23rd Fictional Street,
  Hidden Town, Invisible Land";
parameters["phone"] = "+99000000000";
parameters["canBeManagedByAbove"] = true;

Request request = rpcClient.NewRequest(
  "createCompany", parameters);

Response response = rpcClient.Rpc(request);

if (response.Result != null) {
    JToken result = response.Result;
    Console.WriteLine(response.ToString());
}

// Output:
// 54b646a0b1a43dbd197b23c6    

curl Example

In the following example, we get information about all our managed companies using curl.

curl -i \
-H "Authorization: \
Basic VWpsTVMrMG0xbDlJVVpqcGpXeUpHOGdibnYyTXRhNFQ6" \
-H "Content-Type: application/json" \
-d '{"id": "123456789", "jsonrpc": "2.0",
"method": "getCompaniesList", "params": []}' \
-X POST \
https://cloud.gravityzone.bitdefender.com/api/v1.0/jsonrpc/network

HTTP/1.1 200 OK
Date: Wed, 10 Jan 2015 13:25:30 GMT
Content-Length: 103
Content-Type: application/json; charset=utf-8

{"id":"123456789","jsonrpc":"2.0","result":  
[{"id":"54b646a0b1a43dbd197b23c6","name":"Test Company"}]}    

Python Example

Now, we will request a list of endpoints.

For Python 2.7
#for non-EU
import base64
import requests
import json

apiKey = "UjlMS+0m1l9IUZjpjWyJG8gbnv2Mta4T"

encodedUserPassSequence = base64.b64encode(apiKey + ":")
authorizationHeader = "Basic " + encodedUserPassSequence

apiEndpoint_Url = "https://cloud.gravityzone.bitdefender.com/api/v1.0/jsonrpc/network"

request = '{"params": {},"jsonrpc": "2.0","method": "getEndpointsList","id": "301f7b05-ec02-481b-9ed6-c07b97de2b7b"}'

result = requests.post(apiEndpoint_Url,data=request,verify=False,headers= {"Content-Type":"application/json","Authorization":authorizationHeader})

print(result.json())
#For EU
import base64
import requests
import json

apiKey = "UjlMS+0m1l9IUZjpjWyJG8gbnv2Mta4T"

encodedUserPassSequence = base64.b64encode(apiKey + ":")
authorizationHeader = "Basic " + encodedUserPassSequence

apiEndpoint_Url = "https://cloudgz.gravityzone.bitdefender.com/api/v1.0/jsonrpc/network"

request = '{"params": {},"jsonrpc": "2.0","method": "getEndpointsList","id": "301f7b05-ec02-481b-9ed6-c07b97de2b7b"}'

result = requests.post(apiEndpoint_Url,data=request,verify=False,headers= {"Content-Type":"application/json","Authorization":authorizationHeader})

print(result.json())
For Python 3
#for non-EU
import base64
import requests
import json

apiKey = "UjlMS+0m1l9IUZjpjWyJG8gbnv2Mta4T"
loginString = apiKey + ":"
encodedBytes = base64.b64encode(loginString.encode())
encodedUserPassSequence = str(encodedBytes,'utf-8')
authorizationHeader = "Basic " + encodedUserPassSequence

apiEndpoint_Url = "https://cloud.gravityzone.bitdefender.com/api/v1.0/jsonrpc/network"


request = '{"params": {},"jsonrpc": "2.0","method": "getEndpointsList","id": "301f7b05-ec02-481b-9ed6-c07b97de2b7b"}'

result = requests.post(apiEndpoint_Url,data=request,verify=False,headers= {"Content-Type":"application/json","Authorization":authorizationHeader})

print(result.json())
#for EU
import base64
import requests
import json

apiKey = "UjlMS+0m1l9IUZjpjWyJG8gbnv2Mta4T"
loginString = apiKey + ":"
encodedBytes = base64.b64encode(loginString.encode())
encodedUserPassSequence = str(encodedBytes,'utf-8')
authorizationHeader = "Basic " + encodedUserPassSequence

apiEndpoint_Url = "https://cloudgz.gravityzone.bitdefender.com/api/v1.0/jsonrpc/network"


request = '{"params": {},"jsonrpc": "2.0","method": "getEndpointsList","id": "301f7b05-ec02-481b-9ed6-c07b97de2b7b"}'

result = requests.post(apiEndpoint_Url,data=request,verify=False,headers= {"Content-Type":"application/json","Authorization":authorizationHeader})

print(result.json())

Node.js Example

In this example, we will make the exact previous call, only this time we will use Node.js

// Using the request module:
// npm install requestvar 
request = require('request');

request({  uri: "https://cloud.gravityzone.bitdefender.com/ \
    api/v1.0/jsonrpc/companies",
  method: "POST",
  headers: {
    'Authorization':
      "Basic VWpsTVMrMG0xbDlJVVpqcGpXeUpHOGdibnYyTXRhNFQ6"
  },
  json: {
    "id": "123456789",
    "jsonrpc": "2.0",
    "method": "getCompanyDetails",
    "params": [
  "companyId": "54b646a0b1a43dbd197b23c6"
 ]
  }
}, function(response, body) {
  console.log(body);});

// Output:

// { id: '123456789', jsonrpc: '2.0',
//  result: { 
//  id: '54b646a0b1a43dbd197b23c6',
//  isSuspended: False,
//   name: 'Test Company',
//  phone: '+9900000000',
//  address: '23rd Fictional Street, Fantasy City, Dream land',
//  canBeManagedByAbove: True,
//  type: 1} }    

PowerShell Example

This is an example PowerShell script. It provides the basics to make an API call to a GravityZone API endpoint.

Note

We wrote the operations in this script explicitly for didactic purposes. Feel free to optimize them for your practical use cases, should you feel it necessary.

# Store the API token (change this to your API key)
# For details, refer to the "API Keys" section of this guide.

$api_key = 'UjlMS+0m1l9IUZjpjWyJG8gbnv2Mta4T'

# build the login string (pass is an empty string)

$user = $api_key
$pass = ""
$login = $user + ":" + $pass

# encode the login string to base64

$bytes= [System.Text.Encoding]::UTF8.GetBytes($login)
$encodedlogin=[Convert]::ToBase64String($bytes)

# prepend "Basic " to the encoded login string to obtain
# the auth header

$authheader = "Basic " + 
$encodedlogin

# Replace the base_uri string with the correct one# for your console

$base_uri = "https://cloud.gravityzone.bitdefender.com/api"

# Replace the api_endpoint string with the correct one for
# the method used in the request_data# For details, defer to the "API Requests" section
# of this guide.

$api_endpoint = "/v1.0/jsonrpc/network"# Build the request URI
$request_uri = $base_uri + $api_endpoint

# Store the request body in a JSON variable.
# Define the API call method and its parameteres.
# For more details on each API method, refer to the "Reference"
# chapter of this guide.

$request_data = '{
 "id":"123456789",
 "jsonrpc":"2.0",
 "method":"getEndpointsList",
 "params":
  {
   "page":1,
   "perPage":3
  }
}'

# All required resources are now set.
# You have two options to make the API call.
# First option:
# Add all call parameters in one structure, then call
# Invoke-RestMethod with it.

$params = @{
    Uri         = $request_uri
    Headers     = @{
   'Authorization' = "$authheader"
  'Content-Type' = "application/json"
 }
    Method      = 'POST'
    Body        = $request_data
    ContentType = 'application/json'}

$response = Invoke-RestMethod @params

# Second option:
# Build the headers structure, but specify the
# Invoke-RestMethod parameters inline.

$headers = New-Object `
"System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization",
$authheader)
$headers.Add("Content-Type","application/json")

$response2 = Invoke-RestMethod -Uri $request_uri `
-Headers $headers -ContentType 'application/json' `
-Method Post -Body $request_data

# Random examples of how to address/display the obtained
# call results from the $response and $response2 variables

Write-Output '$response'
Write-Output "~~~~~~~~~~~~~~~~~~~~~~~~~~~"
$response
Write-Output '$response |ConvertTo-Json'
Write-Output "~~~~~~~~~~~~~~~~~~~~~~~~~~~"
$response |ConvertTo-Json
Write-Output '$response.result | ConvertTo-Json'
Write-Output "~~~~~~~~~~~~~~~~~~~~~~~~~~~
"$response.result | ConvertTo-Json
Write-Output '$response.result.items |ConvertTo-Json'
Write-Output "~~~~~~~~~~~~~~~~~~~~~~~~~~~"
$response.result.items |ConvertTo-Json
Write-Output '$response2'
Write-Output "~~~~~~~~~~~~~~~~~~~~~~~~~~~"

$response2
Write-Output '$response2.result'
Write-Output "~~~~~~~~~~~~~~~~~~~~~~~~~~~"
$response2.result
Write-Output '$response2.result.items'
Write-Output "~~~~~~~~~~~~~~~~~~~~~~~~~~~"
$response2.result.items
Write-Output '$response2.result.items.id[0]'
Write-Output "~~~~~~~~~~~~~~~~~~~~~~~~~~~"
$response2.result.items.id[0]
Write-Output '$response2.result.items.name[1]'
Write-Output "~~~~~~~~~~~~~~~~~~~~~~~~~~~"
$response2.result.items.name[1]
Write-Output '$response2.result.items[1] |ConvertTo-Json'
Write-Output "~~~~~~~~~~~~~~~~~~~~~~~~~~~"
$response2.result.items[1] |ConvertTo-Json         

VBScript Example

This is a VBScript example. It provides the basics to make an API call to a GravityZone API endpoint.

Note

We wrote the operations in this script explicitly for didactic purposes. Feel free to optimize them for your practical use cases, should you feel it necessary.

{{'These are for displaying the results of the call.

Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream (1)
Set stderr = fso.GetStandardStream (2)


'These are some helping funtions used for base64 encoding
'of the authorization header.

Private Function Stream_StringToBinary(Text)
    Const adTypeText = 2
    Const adTypeBinary = 1
    Dim BinaryStream 'As New Stream
    Set BinaryStream = CreateObject("ADODB.Stream")
    BinaryStream.Type = adTypeText
    BinaryStream.CharSet = "us-ascii"
    BinaryStream.Open
    BinaryStream.WriteText Text
    BinaryStream.Position = 0
    BinaryStream.Type = adTypeBinary
    BinaryStream.Position = 0
    Stream_StringToBinary = BinaryStream.Read
    Set BinaryStream = Nothing
End Function

Function Base64Encode(sText)
    Dim oXML, oNode
    Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
    Set oNode = oXML.CreateElement("base64")
    oNode.dataType = "bin.base64"
    oNode.nodeTypedValue = Stream_StringToBinary(sText)
    Base64Encode = Replace(oNode.text, chr(10), "")
    Set oNode = Nothing
    Set oXML = Nothing
End Function


'Store the API token.
'Make sure to change the string with your actual API key.
'For more information, refer to the "API Keys" section
'of this guide.

api_key = "UjlMS+0m1l9IUZjpjWyJG8gbnv2Mta4T"


'Build the login string.
'Note: pass is an empty string.

user = api_key
pass = ""
login = user & ":" & pass


'Encode the login string to base64.

encodedlogin = Base64Encode(login)


'Prepend "Basic " to the encoded login string to obtain
'the auth header.

authheader = "Basic " & encodedlogin


'Change the base_uri string with the correct one for your console.

base_uri = "https://cloud.gravityzone.bitdefender.com/api"


'Change the api_endpoint string with the correct one
'for the method used in the request_data.
'For details, refer to "API Requests" section of this guide.

api_endpoint = "/v1.0/jsonrpc/network"


'Build the request URI.

request_uri = base_uri & api_endpoint


'Create the body of the request.
'Define the API call method and its parameteres.
'For more information, refer to the "Reference" chapter
'of this guide.
'Note: Due to limited page width, the strJSONRequest string
'is on multiple lines. You need to put it on one line.

strJSONRequest = "{""id"":""123456789"",
 ""jsonrpc"":""2.0"",
 ""method"":""getEndpointsList"",
 ""params"":{""page"":1,""perPage"":3}}"


'All required resources are set.

'Make the API call.

Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.Open "POST", request_uri, False
objHTTP.setRequestHeader "Authorization", authheader
objHTTP.setRequestHeader "Content-Type", "application/json"
objHTTP.send (strJSONRequest)


'Examples of how to display call reponse:

stdout.WriteLine "Response Code: " & objHTTP.status 
stdout.WriteLine "Response Headers: " & objHTTP.getAllResponseHeaders
stdout.WriteLine "Response Data: " & objHTTP.ResponseText}