Using the API to Check Malicious IPs
Our API allows you to programmatically query the database of known malicious IPs. This guide explains how to integrate the API into your application to stop scrapers and hackers.
Contents
API Overview
To check if an IP address is flagged in our database, you can send a simple POST request to the API. Below is an example using curl:
curl -X POST https://ip64.org/api/check_ip -H "Content-Type: application/json" -d '{"ip": "192.0.2.1"}'
Using the API in Python
Here’s how you can query the API using Python:
import requests
ip_address = "192.0.2.1"
url = "https://ip64.org/api/check_ip"
response = requests.post(url, json={"ip": ip_address})
if response.status_code == 200:
data = response.json()
cname = data.get("cname")
print(f"{ip_address} is flagged as malicious (cname: {cname}).")
elif response.status_code == 404:
print(f"{ip_address} is not flagged.")
else:
print(f"Unexpected response: {response.status_code} {response.text}")
Using the API in JavaScript
Here’s how you can query the API using JavaScript:
fetch('https://ip64.org/api/check_ip', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ ip: '192.0.2.1' })
})
.then(async response => {
if (response.status === 200) {
const data = await response.json();
console.log('IP is flagged as malicious (cname: ' + data.cname + ')');
} else if (response.status === 404) {
console.log('IP is not flagged');
} else {
console.log('Unexpected response:', response.status, await response.text());
}
});
Using the API in Go
Here’s how you can query the API using Go:
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"io/ioutil"
"log"
)
func main() {
url := "https://ip64.org/api/check_ip"
ipData := map[string]string{"ip": "192.0.2.1"}
jsonData, err := json.Marshal(ipData)
if err != nil {
log.Fatal(err)
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode == 200 {
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
log.Fatal(err)
}
fmt.Printf("IP is flagged as malicious (cname: %v)\n", result["cname"])
} else if resp.StatusCode == 404 {
fmt.Println("IP is not flagged")
} else {
fmt.Printf("Unexpected response: %d %s\n", resp.StatusCode, string(body))
}
}
Using the API in PHP
Here’s how you can query the API using PHP:
<?php
$ip = "192.0.2.1";
$url = "https://ip64.org/api/check_ip";
$data = json_encode(array("ip" => $ip));
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => $data,
'ignore_errors' => true // This allows you to capture 404 responses too
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
// Get the HTTP response code
$http_response = $http_response_header[0] ?? '';
if (strpos($http_response, "200") !== false) {
$response = json_decode($result, true);
$cname = $response['cname'] ?? 'unknown';
echo "$ip is flagged as malicious (cname: $cname).";
} else if (strpos($http_response, "404") !== false) {
echo "$ip is not flagged.";
} else {
echo "Unexpected response: $http_response\n$result";
}
?>