Code Samples
Working examples for common integration patterns. Each sample authenticates with an API key and secret, obtains a token, and then calls one API endpoint.
Replace the placeholder API key and secret before running these examples. Tokens are passed in the request path, not as HTTP headers.
Domain Activity API: Python
Authenticates with the Domain Activity API and fetches added .com domains containing the keyword "bank".
import requests
API_KEY = "your_api_key"
API_SECRET = "your_api_secret"
BASE_URL = "https://api.codepunch.com/dnfeed/v2"
def get_token():
url = f"{BASE_URL}/auth/{API_KEY}/{API_SECRET}"
response = requests.get(url, timeout=30)
response.raise_for_status()
data = response.json()
if not data.get("status"):
raise RuntimeError(data.get("error", "Authentication failed"))
return data["token"]
def get_api_data(token, endpoint, params=None):
url = f"{BASE_URL}/{token}/{endpoint}"
response = requests.get(url, params=params or {}, timeout=60)
response.raise_for_status()
data = response.json()
if not data.get("status"):
raise RuntimeError(data.get("error", "API request failed"))
return data
token = get_token()
params = {
"kw": "bank",
"tlds": "com",
"start": 0,
"limit": 500,
}
data = get_api_data(token, "added", params)
print(f"Total records: {data['records']}")
for row in data.get("data", []):
print(row["domain"])
DNS and Subdomains API: Python
Authenticates with the DNS API and searches nameservers containing the keyword "wordpress".
import requests
API_KEY = "your_api_key"
API_SECRET = "your_secret_key"
BASE_URL = "https://api.codepunch.com/dns/v2"
def get_token():
url = f"{BASE_URL}/auth/{API_KEY}/{API_SECRET}"
response = requests.get(url, timeout=30)
response.raise_for_status()
data = response.json()
if not data.get("status"):
raise RuntimeError(data.get("error", "Authentication failed"))
return data["token"]
def get_api_data(token, endpoint, params=None):
url = f"{BASE_URL}/{token}/{endpoint}"
response = requests.get(url, params=params or {}, timeout=60)
response.raise_for_status()
data = response.json()
if not data.get("status"):
raise RuntimeError(data.get("error", "API request failed"))
return data
token = get_token()
# Find all nameservers that contain "wordpress".
params = {"kw": "wordpress"}
data = get_api_data(token, "nameservers", params)
print(data)
DNS and Subdomains API: PHP
Authenticates with the DNS API and retrieves nameserver data for the keyword "wordpress".
<?php
$api_key = "paste_your_key_here";
$api_secret = "paste_your_secret_here";
$base_url = "https://api.codepunch.com/dns/v2";
function get_json($url) {
$contents = file_get_contents($url);
if ($contents === false) {
throw new Exception("Request failed: " . $url);
}
$data = json_decode($contents, true);
if (!is_array($data)) {
throw new Exception("Invalid JSON response");
}
if (empty($data["status"])) {
$message = isset($data["error"]) ? $data["error"] : "API request failed";
throw new Exception($message);
}
return $data;
}
try {
// Authenticate and get token.
$auth_url = $base_url . "/auth/" . rawurlencode($api_key) . "/" . rawurlencode($api_secret);
$auth_data = get_json($auth_url);
$token = $auth_data["token"];
// Get all nameservers that contain "wordpress".
$params = http_build_query([
"kw" => "wordpress"
]);
$url = $base_url . "/" . rawurlencode($token) . "/nameservers/?" . $params;
$nameservers = get_json($url);
echo "<pre>";
print_r($nameservers);
echo "</pre>";
} catch (Exception $e) {
echo "Error: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, "UTF-8");
}
?>
SSL/TLS Certificates API: Python
Authenticates with the SSL/TLS Certificate API and searches yesterday's certificates containing the keyword "amazon".
import datetime
import requests
API_KEY = "your_api_key"
API_SECRET = "your_secret_key"
BASE_URL = "https://api.codepunch.com/tlscerts/v2"
def get_token():
url = f"{BASE_URL}/auth/{API_KEY}/{API_SECRET}"
response = requests.get(url, timeout=30)
response.raise_for_status()
data = response.json()
if not data.get("status"):
raise RuntimeError(data.get("error", "Authentication failed"))
return data["token"]
def get_api_data(token, endpoint, params=None):
url = f"{BASE_URL}/{token}/{endpoint}"
response = requests.get(url, params=params or {}, timeout=60)
response.raise_for_status()
data = response.json()
if not data.get("status"):
raise RuntimeError(data.get("error", "API request failed"))
return data
token = get_token()
yesterday = datetime.date.today() - datetime.timedelta(days=1)
date_code = yesterday.strftime("%Y%m%d")
# Find certificates that contain "amazon".
params = {
"date": date_code,
"limit": 50,
"kw": "amazon",
}
data = get_api_data(token, "certificates", params)
for cert in data.get("data", []):
print(cert)
SSL/TLS Certificates API: PHP
Authenticates with the SSL/TLS Certificate API and retrieves yesterday's certificate records matching "amazon".
<?php
$api_key = "paste_your_key_here";
$api_secret = "paste_your_secret_here";
$base_url = "https://api.codepunch.com/tlscerts/v2";
function get_json($url) {
$contents = file_get_contents($url);
if ($contents === false) {
throw new Exception("Request failed: " . $url);
}
$data = json_decode($contents, true);
if (!is_array($data)) {
throw new Exception("Invalid JSON response");
}
if (empty($data["status"])) {
$message = isset($data["error"]) ? $data["error"] : "API request failed";
throw new Exception($message);
}
return $data;
}
try {
// Authenticate and get token.
$auth_url = $base_url . "/auth/" . rawurlencode($api_key) . "/" . rawurlencode($api_secret);
$auth_data = get_json($auth_url);
$token = $auth_data["token"];
// Get yesterday's certificates containing "amazon".
$date_code = date("Ymd", strtotime("-1 day"));
$params = http_build_query([
"kw" => "amazon",
"date" => $date_code,
"limit" => 50
]);
$url = $base_url . "/" . rawurlencode($token) . "/certificates/?" . $params;
$certificates = get_json($url);
echo "<pre>";
print_r($certificates);
echo "</pre>";
} catch (Exception $e) {
echo "Error: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, "UTF-8");
}
?>
Need more complete scripts with full pagination or custom filters? Contact us with your subscription details and we will send them across.