Make API Calls

The Plan

This guide describes how to use the interface provided by the Nexusguard Developer Platform to make simple calls to the protection resources provided by Nexusguard. First of all, the client should have an App of the Developer Platform platform, and then obtains the required information by obtaining the access_token from the Nexusguard Oauth Platform through the configuration information of the APP and calling the Nexusguard Open Interface with the access_token.

When the API is invoked, the response code for a successful or failed request is returned. Developers can debug the API using error code for troubleshooting. Common error code : 0 indicates successful request others indicate failed request.

Nexusguard APP API Call Sequence

https://api.nexusguard.com/oauth/connect?app_id={app_id}
&xspe={xspe}&customer_id={customer_id}&state={state}
&scope=install_login&response_type=code&redirect_uri={redirect_uri}
https://api.nexusguard.com/oauth/access_token

params: app_id , secret , code , grant_type


https://api.nexusguard.com/api/spe/customer/{customer_id}?access_token={access_token}

SPE APP API Call Sequence

https://api.nexusguard.com/oauth/credit/token

params: app_id , secret , grant_type


https://api.nexusguard.com/api/spe/customers?access_token={access_token}

You can check the documentation for the above API for more information. *

Making API Calls

Take the SPE APP API as an example. You can call this API in the following ways.

API Calls

Curl

First, send a post request to the /oauth/credit/token interface with the curl command to get the access_token:

$ curl -d
"app_id=d7690800dc5d4cf885fac35fcb0c8184&
 secret=bf20940d16578e18765eb9f93b1827e9&
 grant_type=client_credential"
"https://api.nexusguard.com/oauth/credit/token"

Next, send a get request to the /api/spe/customers interface by requesting the access_token obtained from the previous interface.

$ curl
"https://api.nexusguard.com/api/spe/customers?access_token={access_token}"

Using Sample Code

To use the sample code, let’s run the application.

You should be in the same directory you used for Setup Your Environment and Configure your Credentials.

PHP code example:

$oauth_url = 'https://api.nexusguard.com/oauth/credit/token';
$post_data['app_id']='d7690800dc5d4cf885fac35fcb0c8184';
$post_data['secret']='bf20940d16578e18765eb9f93b1827e9';
$post_data['grant_type']='client_credential';
# retreive access_token
$oauth_result = postData($oauth_url,$post_data);
$access_token = json_decode($oauth_result,true)['result']['access_token'];
# Get api call results
$api_result = file_get_contents('https://api.nexusguard.com/api/spe/customer/{customer_id}?access_token='.$access_token);
echo $api_result;
function postData($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $ret = curl_exec($ch);
    curl_close($ch);
    return $ret;
}

Python code example:

# -*- coding:utf-
import requests
import json

oauth_url = "https://api.nexusguard.com/oauth/credit/token"
post_data = {'app_id':'d7690800dc5d4cf885fac35fcb0c8184','secret':'bf20940d16578e18765eb9f93b1827e9','grant_type':'client_credential'}
oauth_response = requests.post(oauth_url, data = post_data)
oauth_ret = json.loads(oauth_response.text)
# Retreive access_token
access_token = oauth_ret['result']['access_token']
api_url = "https://api.nexusguard.com/api/spe/customer/{customer_id}"
get_data = {"access_token":access_token}
api_response = requests.get(api_url,get_data)
api_result = api_response.text
#Get API call results
print api_result

Analyzing the Results

Nexusguard APP API

First by requesting the /oauth/connect interface, it redirects to the URL of the redirect_uri, with the code and state parameters, and saves the code for the next validation.

redirect_uri?code={code}&state={state}

Second, takes the code request /oauth/access_token to get the access_token.

{
    "code": 0,
    "msg": "success",
    "result": {
        "access_token": "T2CMJBS25SCCCPRZG7S2RTSZFE",
        "expires_in": 7200,
        "refresh_token": "E2CMJBS25SCCCPRZG7S2RTSZFE",
        "nxg_account": "nxg_account",
        "scope": "install_login"
    }
}

Third, the access_token request /api/spe/customer/{customer_id} obtained in the second step is used to obtain the basic information of the customer.

{
    "code": 0,
    "msg": "success",
    "result": {
        "website": "",
        "address": "",
        "phone": "",
        "protect_services": {
            "application_protection": 1,
            "origin_protection": 1,
            "dns_protection": 1,
            "cdn_federation": 1
        },
        "company": "",
        "customer_name": "SPE-592196-evaapp",
        "email": "eva@test.com"
    }
}

SPE APP API

First get the access_token by app_id and secret requesting /oauth/credit/token.

{
    "code": 0,
    "msg": "success",
    "result": {
        "access_token": "T2CMJBS25SCCCPRZG7S2RTSZFE",
        "expires_in": 7200
    }
}

Second, obtain the access_token request /api/spe/customers obtained from the first step to obtain the customer list under the spe portal.

{
    "code": 0,
    "msg": "success",
    "result": {
        "pages": 2,
        "page": 1,
        "customer": [
            {
                "customer_name": "SPE-844326-notiappzora",
                "protect_services": {
                    "application_protection": 1,
                    "origin_protection": 1,
                    "dns_protection": 1,
                    "cdn_federation": 1
                },
                "customer_id": "39e6e435d32dffb90"
            },
            {
                "customer_name": "SPE-644008-cyapps",
                "protect_services": {
                    "application_protection": 1,
                    "origin_protection": 1,
                    "dns_protection": 1,
                    "cdn_federation": 0
                },
                "customer_id": "2346bfcf25918778c"
            }
        ]
    }
}

Congratulations!

You’ve completed your first Nexusguard OPEN API call! Now you are ready to explore more of the APIs offered in the OPEN API Catalog.