Back to Blog

How to Build a Keyword Rank Tracker in Python Using a SERP API

Step-by-step guide to building an automated keyword rank tracker with Python and SerpBase. Track your Google rankings daily without manual effort.

April 8, 2026
By SerpBase Teampythonrank-trackingtutorialseo

Why Build a Rank Tracker?

Manual rank checking is slow, error-prone, and impossible to do at scale. A simple Python script that runs daily and logs your positions takes less than 50 lines of code -- and SerpBase makes it affordable enough to run thousands of checks per month for a few dollars.


What You Need

  • Python 3.8+
  • A SerpBase API key (get 100 free searches at serpbase.dev)
  • A list of keywords to track

Install the only dependency:

pip install requests

Step 1: Fetch Rankings for a Keyword

import requests

SERPBASE_KEY = "YOUR_API_KEY"
YOUR_DOMAIN = "yourdomain.com"

def get_rank(keyword: str, domain: str) -> int | None:
    response = requests.post(
        "https://api.serpbase.dev/google/search",
        headers={"X-API-Key": SERPBASE_KEY},
        json={"q": keyword, "gl": "us", "hl": "en", "page": 1},
        timeout=15,
    )
    response.raise_for_status()
    results = response.json().get("organic", [])

    for result in results:
        if domain in result.get("link", ""):
            return result["position"]

    return None  # not in top 10

Step 2: Track Multiple Keywords

import csv
import time
from datetime import date

KEYWORDS = [
    "serp api",
    "google search api",
    "rank tracking tool",
    "seo monitoring python",
]

def track_all(keywords: list[str], domain: str) -> list[dict]:
    rows = []
    for kw in keywords:
        rank = get_rank(kw, domain)
        rows.append({
            "date": date.today().isoformat(),
            "keyword": kw,
            "rank": rank if rank else "not found",
        })
        print(f"{kw}: {rank}")
        time.sleep(0.5)  # stay within rate limits
    return rows

Step 3: Save Results to CSV

def save_to_csv(rows: list[dict], path: str = "rankings.csv") -> None:
    with open(path, "a", newline="") as f:
        writer = csv.DictWriter(f, fieldnames=["date", "keyword", "rank"])
        if f.tell() == 0:
            writer.writeheader()
        writer.writerows(rows)

Step 4: Put It All Together

if __name__ == "__main__":
    data = track_all(KEYWORDS, YOUR_DOMAIN)
    save_to_csv(data)
    print(f"Saved {len(data)} keyword rankings to rankings.csv")

Step 5: Automate with Cron

Run daily at 8 AM:

crontab -e
# Add this line:
0 8 * * * /usr/bin/python3 /path/to/rank_tracker.py >> /var/log/rank_tracker.log 2>&1

Cost Estimate

At $0.30 per 1,000 requests:

KeywordsFrequencyMonthly requestsMonthly cost
10Daily300$0.09
100Daily3,000$0.90
1,000Daily30,000$9.00

Rank tracking at scale costs less than a cup of coffee per month.


Next Steps

  • Multi-page tracking -- set page: 2 to check positions 11-20
  • Location-specific rankings -- use different gl values per market
  • Alerting -- send a Slack or email notification when a ranking drops more than 3 positions
  • Dashboard -- pipe results into Google Sheets or a SQLite database for trend visualization

Get your API key at serpbase.dev and start tracking in minutes.