46. Appendix 5 – Using the REST API

The REST API calls can be consulted at www.andrisoft.com/wanguard-api-ui. To be able to use the REST API, 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.

API calls are grouped similarly to the navigation structure of Wanguard Console. When you click a resource, you’ll see the required parameters, you can test the call, and even copy/paste the command for use in scripts or from the CLI. Each REST API call uses one of the following HTTP methods:

● GET → Retrieve data from the server
● POST → Create a resource
● PUT → Update a resource
● DELETE → Remove a resource

46.1. CLI Example

As an example, if you click the first resource (GET /wanguard-api/v1/anomalies) and set the Status parameter to Active, then click [Try it out!], you’ll see a cURL command you can run from the CLI or any script to fetch a JSON list of active anomalies, along with href links for additional details.
Below is an example script (/opt/andrisoft/bin/active_anomalies_id.sh) that does exactly that:
#!/bin/sh

curl -X GET --header 'Accept: application/json' --header 'Authorization: Basic XXXXXXX==' 'https://console/wanguard-api/v1/anomalies?status=Active'

Make the script executable by running:

chmod +x /opt/andrisoft/bin/active_anomalies_id.sh

46.2. PHP Example

The code below (/opt/andrisoft/bin/clear_orphaned_prefixes.php) simulates clicking the Clear option from Reports » Tools » Routing » Active BGP Announcements » Batch Actions:

#!/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);
?>

This PHP script can be executed from the CLI after running:

chmod +x /opt/andrisoft/bin/clear_orphaned_prefixes.php