API Curl Guide

Overview

MyWhistleBox provides developers a path to integrate their applications to the MyWhistleBox service through an Application Programming Interface (API). This is a great way to extend the MyWhistleBox service using an existing workflow.

Here are a just a few examples of things you can do with the REST API:
  • Schedule batch uploads of documents to user accounts
  • Integrate Signature and Upload Requests into an existing CRM workflow
  • Develop a custom user interface to the WhistleBox server
  • Schedule the downloading of reports to a Sharepoint server
The MyWhistleBox API is implemented using a REST model. The REST model uses standard HTTP requests to communicate with the MyWhistleBox API service. Developers can use any programming language that supports standard HTTP requests calls. This guide provides Curl examples to access the API. It will primarily focus on general techniques to connect and call an end point. All endpoints are documented in the API Guide.

Prerequisites

In the following examples, we will be using PHP along with the curl library. PHP comes with curl. The PHP Curl extension must be enabled on your web server to run these examples. Curl is a free http request library that has been ported to many programming languages. We will demonstrate the API calls using PHP CURL but other programming languages can be used using similar techniques. The php curl man page can be located at

https://www.php.net/manual/en/book.curl.php

Obtain an API Key

Let's first create an API key so we can access the api. Sign into your MyWhistleBox account and from Account Settings, generate an API key for your app using the Api Key tab (Configuration Guide provides more details). You will need to acquire an API Key and plug it into the following source code examples.

Examples

These script examples demonstrate how to call the API endpoints as described in the API Devloper Guide. The scripts provide a zero dependency method using curl. You can also use other libraries such as guzzle to make the REST calls. You can run these scripts via a browser or command line. Simply copy and paste the script contents to a file with a php extension then copy the file to the web server and run the script from your browser.

We have provided one GET example and one POST example. These 2 examples will account for the vast majority of the endpoints.

This script demonstrates how to test your MyWhistleBox REST Api connection using PHP. Using he Ping endpoint, you can test to make sure you are able to authenticate and connect to the API server. If the request is successful, it simply returns “ok”. Inline notes will provide you with explanations of the code.

 
<?php                                        
// Define a variable for your private API key
define("APIKEY", "<obtain your API Key from your user Account Settings>");

// Variable to hold the root url to REST server
define("INSTANCE_URL", "https://mywhistlebox.com/api/rest/v1.0/");

// HELPER FUNCTIONS

// Helper function to make an HTTP request call using curl
// supports methods POST, PUT, DELETE and GET
function restCall($method, $url, $apikey, $data=array())
{
    $curl = curl_init();
    switch (strtoupper($method)) {
        case "POST":         
            curl_setopt($curl, CURLOPT_POST, true);
            if (count($data)) {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            break;
        case "PUT":         
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
            if (count($data)) {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            break;
        case "DELETE":         
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
            if (count($data)) {
                $url = sprintf("%s?%s", $url, http_build_query($data));
            }
            break;
        default:
            if (count($data)) {
                $url = sprintf("%s?%s", $url, http_build_query($data));
            }
    }

    // OPTIONS: 

    // set the request url
    curl_setopt($curl, CURLOPT_URL, $url);
    // make sure we get a json response
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    // add request header with apikey
    curl_setopt(
        $curl, 
        CURLOPT_HTTPHEADER, 
        ["Authorization: $apikey"]
    );

    // EXECUTE the request and check for errors
    $result = curl_exec($curl);
    if(curl_errno($curl)){
        echo 'Request Error:' . curl_error($curl);
    }
    
    curl_close($curl);
    
    return $result;
} // end restCall


// OPTIONAL: let's make sure curl extension is installed
if (!function_exists('curl_init')) {
    echo "CURL Extension must be installed\n";
    exit();
} 
  
// invoke the ping endpoint

// INITIALIZATION: set the request url
$ping_url = INSTANCE_URL."/test/ping";

// ping server to see if connection is established
echo ">>> Pinging...";
$response = restCall('GET', $ping_url, APIKEY);

if ($response !== false) {
    // should receive a json response with status='ok'
    $result = json_decode($response, true);
    if ($result['status'] == 'ok') {
        echo "server responded with ok...\n";
    } else {
        echo "$response...\n";
    }
}

echo "DONE!\n";
                                
This script is an example of how to post an upload via MyWhistleBox REST Api using PHP. This script provides a no dependency method using curl. You can also use other libraries such as guzzle to make the REST calls. You can run this script via a browser or command line. Simply copy and paste these contents to a file with a php extension then copy the file to the web server and run the script from your browser.

 
<?php                                        
// Define a variable for your private API key
define("APIKEY", "<obtain your API Key from your user Account Settings>");

// Variable to hold the root url to REST server
define("INSTANCE_URL", "https://mywhistlebox.com/api/rest/v1.0/");

// HELPER FUNCTIONS

// polyfill of CURLStringFile, available in php >=8.1
// CURLStringFile is useful if you're document comes from a database as a string
// We won't be using this in this example. Included as an alternative technique.
if (!class_exists('CURLStringFile')) {
    class CURLStringFile extends CURLFile {
        public function __construct(string $file_contents, string $file_name, string $mime = "application/octet-stream")
        {
            $this->name = 
               'data://'. $mime .';base64,' . base64_encode($file_contents);
            $this->mime = $mime;
            $this->postname = $file_name;
        }
    }
}

// Helper function to make an HTTP request call using curl
// supports methods POST, PUT, DELETE and GET
function restCall($method, $url, $apikey, $data=array())
{
    $curl = curl_init();
    switch (strtoupper($method)) {
        case "POST":         
            curl_setopt($curl, CURLOPT_POST, true);
            if (count($data)) {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            break;
        case "PUT":         
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
            if (count($data)) {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            break;
        case "DELETE":         
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
            if (count($data)) {
                $url = sprintf("%s?%s", $url, http_build_query($data));
            }
            break;
        default:
            if (count($data)) {
                $url = sprintf("%s?%s", $url, http_build_query($data));
            }
    }

    // OPTIONS: 

    // set the request url
    curl_setopt($curl, CURLOPT_URL, $url);
    // make sure we get a json response
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    // add request header with apikey
    curl_setopt(
        $curl, 
        CURLOPT_HTTPHEADER, 
        ["Authorization: $apikey"]
    );

    // EXECUTE the request and check for errors
    $result = curl_exec($curl);
    if(curl_errno($curl)){
        echo 'Request Error:' . curl_error($curl);
    }
    
    curl_close($curl);
    
    return $result;
} // end restCall


// this creates a CURLFIle object for posting
// use if your file upload is a reference to the actual file path
function createCurlFile($filePath) {
   $mime = mime_content_type($filePath);    
   info = pathinfo($filePath);
   $name = $info['basename'];
   return new CURLFile($filePath, $mime, $name);
}

// this creates a CURLStringFile object for posting
// optionally use if your file upload contains the file contents
function createCurlFileString($contents, $name) {
   return new CURLStringFile($contents, $name);
}

// OPTIONAL: let's make sure curl extension is installed
if (!function_exists('curl_init')) {
    echo "CURL Extension must be installed.\n";
    exit();
} 
   
// INITIALIZATION

// file that you wish to upload (replace with your own test doc)
$upload_file = "api_test_doc.pdf";


// Enter a list of whistle box addresses to upload the above file to
// You should add your own addresses specific to your account (minimum 1)
$wbaddresses = [
   "username/box",
];

// REST upload endpoint url
$upload_url =  INSTANCE_URL."/user/file/upload";

// We will upload the file to the users account(s) using their WhistleBox Address
foreach ($wbaddresses as $wbaddr) {
	$upload_arguments = [
		"address" => $wbaddr,
		"file" =>createCurlFile($upload_file),
	];
	echo ">>> Uploading test file $upload_file to $wbaddr...";
	$response = restCall('POST', $upload_url, APIKEY, $upload_arguments);
	// we should receive a json response with status='ok'
	$result = json_decode($response, true);
	if ($result['status'] == 'ok') {
		echo "ok\n";
	} else {
		echo "$response\n";
	}
}
echo ">>> DONE\n";