DEV Community

API Serpent
API Serpent

Posted on

How to build a multi-engine AI citation tracker in Python (under 30 lines)

Most rank trackers tell you "position 3."

In 2026, that's not enough information.
Google AI Overviews now appear on ~55% of
searches and push organic results down
200-400px before users see them.

"Position 1" can be below the fold.

This tutorial shows how to build a tracker
that returns both rank position AND pixel
coordinates of every SERP element — plus
AI citation visibility across ChatGPT,
Claude, Gemini, and Perplexity.

Using Serpent API (10 free searches, no card:
apiserpent.com).

import requests
import json

API_KEY = "your_serpent_api_key"
BASE_URL = "https://apiserpent.com/api"

def check_ai_visibility(keyword: str, engines: list = None) -> dict:
    """
    Check if your brand appears in AI engine citations
    across ChatGPT, Claude, Gemini, and Perplexity.
    """
    if engines is None:
        engines = ["chatgpt", "claude", "gemini", "perplexity"]

    response = requests.get(
        f"{BASE_URL}/ai/rank",
        params={
            "keyword": keyword,
            "engines": ",".join(engines)
        },
        headers={"X-API-Key": API_KEY},
        timeout=30
    )
    response.raise_for_status()
    return response.json()

def check_serp_position(keyword: str, engine: str = "google") -> dict:
    """
    Get rank position AND pixel coordinates 
    of every SERP element.
    """
    response = requests.get(
        f"{BASE_URL}/search",
        params={"q": keyword, "engine": engine},
        headers={"X-API-Key": API_KEY},
        timeout=15
    )
    response.raise_for_status()
    data = response.json()

    # Extract pixel positions — unique feature
    positions = []
    for result in data.get("organic_results", []):
        positions.append({
            "rank": result["position"],
            "url": result["link"],
            "pixel_x": result.get("pixel_position", {}).get("x"),
            "pixel_y": result.get("pixel_position", {}).get("y"),
            "above_fold": result.get("pixel_position", {}).get("y", 999) < 600
        })
    return positions

# Example usage
if __name__ == "__main__":
    keyword = "best project management API"

    # Check AI citations
    ai_data = check_ai_visibility(keyword)
    for engine, data in ai_data["results"].items():
        score = data.get("visibility_score", 0)
        citations = len(data.get("citations", []))
        print(f"{engine}: score={score}, citations={citations}")

    # Check SERP with pixel positions
    serp = check_serp_position(keyword)
    for item in serp[:3]:
        fold_status = "above fold" if item["above_fold"] else "below fold"
        print(f"Rank {item['rank']}: {item['url']}{fold_status} (y={item['pixel_y']}px)")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)