The aim of this guide is to demonstrate some basic functionality of the Netping API using the Python language. This is by no means a full revision of the Netping API. To learn all the methods and parameters of the Netping API, please visit the API Reference document.
This guide will takeIn this guide, we will use Python 3 in addition to the HTTP requests library.
To install the requests library, enter the pip command below:
> pip install requests
After installation, import the request library in your Python code:
import requests
To operate most of the Netping API, you will need an API token. The following code sample creates a Netping user and generates an API token to use for the remainder of this guide. It's also possible to register an account manually and then visit the “Settings > API” page.
Please note that all Netping accounts need to be validated by clicking the activation link sent via email.
For the full specs, read about Users in the API reference.
# URL to netping api
URL = "https://netping.com/api/v1/users"
# defining parameters
PARAMS = {
'email': 'john@gmail.com',
'password': 'johns new password 123',
'create_token': 1
}
try:
# sending get request and saving the response
r = requests.post(url = URL, json = PARAMS)
# extracting response in json format
data = r.json()
# data will contain new api_token
print(data)
except requests.exceptions.RequestException as e:
raise SystemExit(e)
Nodes are what we call the servers that connect to check our customers services. To create a Check, we need to first select a Node. The following sample fetches a list of all nodes from the Nodes API. Use the API token acquired in step 2 above.
For the full specs, read about Nodes in the API reference.
# URL to netping api
URL = "https://netping.com/api/v1/nodes"
# set api_token in header
HEADERS = {'Authorization': 'Bearer [your-api-token]'} # enter your api_token here
try:
# sending get request and saving the response
r = requests.get(url = URL, headers = HEADERS)
# extracting response in json format
data = r.json()
# print json object of Nodes
print(data)
except requests.exceptions.RequestException as e:
raise SystemExit(e)
A Check is what we call the action that connects to your web service to check it. The Check defines the URL, HTTP method, and other parameters used when auditing your service. The following code sample creates a Check using the node_id acquired in step 3 above.
For the full specs, read about Checks in the API reference.
# URL to netping api
URL = "https://netping.com/api/v1/checks"
# defining parameters
PARAMS = {
'name': 'My website',
'interval': '60', # one time per minute
'type': 'http',
'active': 1,
'method': 'get',
'url': 'https://www.my-website.com',
'node_id': 123 # get an id from the Node API
}
# set api_token in header
HEADERS = {'Authorization': 'Bearer [your-api-token]'} # enter your api_token here
try:
# sending get request and saving the response
r = requests.post(url = URL, json = PARAMS, headers = HEADERS)
# extracting response in json format
data = r.json()
# print response
print(data)
except requests.exceptions.RequestException as e:
raise SystemExit(e)
A Recipient is an end-point for alerts. In the sample below, we will create an SMS recipient called “Johns Phone”.
There are all kinds of simple and complex recipient types. For the full specs, read about Recipients in the API reference.
# URL to netping api
URL = "https://netping.com/api/v1/recipients"
# defining parameters
PARAMS = {
'name': 'Johns phone',
'type': 'sms',
'msisdn': '46701234567'
}
# set api_token in header
HEADERS = {'Authorization': 'Bearer [your-api-token]'} # enter your api_token here
try:
# sending get request and saving the response
r = requests.post(url = URL, json = PARAMS, headers = HEADERS)
# extracting response in json format
data = r.json()
# print response
print(data)
except requests.exceptions.RequestException as e:
raise SystemExit(e)
For the final step, we will define when to send an alarm by creating a Trigger. The Trigger created below fires when the response time of the check exceeds five seconds. An alert will be sent to the recipient ("Johns Phone") created in step 5 above.
For the full specs, read about Triggers in the API reference.
# URL to netping api
URL = "https://netping.com/api/v1/checktriggers"
# defining parameters
PARAMS = {
'check_id': '45a6490a-7354-47d9-aec3-658c6dec548b',
'name': 'My Trigger',
'type': 'http',
'value': 5000, # 5 seconds
'minimum_duration': 0,
'recipients': [
'8fffd18d-002b-48bc-96aa-558c50c2db56' # johns phone
]
}
# set api_token in header
HEADERS = {'Authorization': 'Bearer [your-api-token]'} # enter your api_token here
try:
# sending get request and saving the response
r = requests.post(url = URL, json = PARAMS, headers = HEADERS)
# extracting response in json format
data = r.json()
# print response
print(data)
except requests.exceptions.RequestException as e:
raise SystemExit(e)
You have reached the end of the guide. At this point, we have created a fully functional Check using Python. If we fetch it using the Checks API, we will receive the following Check Object as a result. As you can see, the below results contain the Triggers and Recipient we created above.
Thanks for trying Netping!
{
"id": "45a6490a-7354-47d9-aec3-658c6dec548b",
"name": "My website",
"interval": 60,
"active": true,
"type": "http",
"node_id": 123,
"url": "https://www.my-website.com",
"method": "get",
"triggers": {
"data": [
{
"id": "9d1e416b-9057-4d9d-bc68-3d6f1c416d83",
"name": "My Trigger",
"type": "http",
"check_id": "45a6490a-7354-47d9-aec3-658c6dec548b",
"value": 5000,
"minimum_duration": 0,
"days_warning": null,
"recipients": {
"data": [
{
"id": "8fffd18d-002b-48bc-96aa-558c50c2db56",
"name": "John phone",
"type": "sms",
"msisdn": "46701234567"
}
]
}
}
]
}
}