46. Appendix 5 – Using the REST API

The REST API’s calls are fully documented at www.andrisoft.com/wanguard-api-ui. To be able to use the REST API, make sure that you have the package wanrestapi installed on your Console. Then go to General Settings » User Management and enable REST API Access for one of your users. Open http://<console_ip>/wanguard-api-ui and authenticate with the user’s credentials by clicking the [Authorize] button from the top-right part of the web page.

The API calls are ordered in a way that resembles the navigation structure of Wanguard Console. If you click on any resource, you will see what parameters it accepts, you can test the call to see the results, and you can also copy/paste the exact command needed to call the method from any scripting language or from the CLI. Each REST API call uses one of the following HTTP methods:

● GET is used to retrive data from the server
● POST is used to create a resource
● PUT is used to update a resource
● DELETE is used to remove a resource
As an example, if you click the first resource (GET /wanguard-api/v1/anomalies) and set the Status parameter to Active, and then click the [Try it out!] button, you will see a CURL command that can be executed from the CLI or from any script in order to get a JSON list with active anomalies and href links with further details about each anomaly.
/opt/andrisoft/bin/active_anomalies_id.sh
#!/bin/sh

curl -X GET --header 'Accept: application/json' --header 'Authorization: Basic XXXXXXX==' 'https://console/wanguard-api/v1/anomalies?status=Active'
The following PHP code can be executed from the CLI after setting the execute bit with “chmod +x /opt/andrisoft/bin/clear_orphaned_prefixes.php”. It simulates selecting the Clear option from Reports » Tools » Routing » Active BGP Announcements » Batch Actions.
/opt/andrisoft/bin/clear_orphaned_prefixes.php
#!/usr/bin/php
<?php
       error_reporting(E_ALL);
       $wgUser     = 'api_user';
       $wgPass     = 'api_pass';
       $wgUrl      = 'https://wanguard_console/wanguard-api/v1/bgp_announcements_actions';


       $payload = json_encode([ 'batch_action'   => "Clear" ]);
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $wgUrl);
       curl_setopt($ch, CURLOPT_TIMEOUT, 30);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
       curl_setopt($ch, CURLOPT_USERPWD, "$wgUser:$wgPass");
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
       curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);

       $result = curl_exec($ch);

       if (curl_errno($ch)) {
           echo sprintf("Error: %s\n", curl_error($ch));
       } else {
           echo $result;
       }

       curl_close($ch);
?>