Python SDK
Official Python SDK for Serpex
The serpex package is the official Python SDK for the Serpex API. Requires Python 3.8+.
Package: serpex on PyPI — v2.6.0
Installation
pip install serpexOr with Poetry:
poetry add serpexInitialize the Client
from serpex import SerpexClient
client = SerpexClient("sk_your_api_key")The constructor accepts an optional base_url argument (defaults to https://api.serpex.dev).
Search
from serpex import SerpexClient
client = SerpexClient("sk_your_api_key")
# Auto-routing — Serpex picks the best engine
results = client.search({
"q": "python tutorial",
"engine": "auto",
})
print(results.results[0].title)
print(results.results[0].url)
print(results.metadata.credits_used)Using SearchParams for Type Safety
from serpex import SerpexClient, SearchParams
client = SerpexClient("sk_your_api_key")
params = SearchParams(
q="machine learning",
engine="auto",
time_range="month",
category="web",
)
results = client.search(params)
print(results.results[0].title)Search Parameters
from dataclasses import dataclass
from typing import Optional
@dataclass
class SearchParams:
q: str # Required
engine: Optional[str] = "auto" # "auto" | "google" | "bing" | "duckduckgo"
# | "brave" | "yahoo" | "yandex"
category: Optional[str] = "web" # "web" | "news"
time_range: Optional[str] = "all" # "all" | "day" | "week" | "month" | "year"Extract
from serpex import SerpexClient
client = SerpexClient("sk_your_api_key")
# Extract as Markdown — ideal for LLM processing
result = client.extract({
"urls": ["https://example.com", "https://httpbin.org"],
})
for r in result.results:
if r.success:
print(f"{r.url}: {len(r.markdown)} characters")
else:
print(f"{r.url}: failed — {r.error}")
print(f"Credits used: {result.metadata.credits_used}")Extract with Stealth Mode
# Stealth mode for sites with bot protection — costs 5 credits per URL
stealth_result = client.extract({
"urls": ["https://protected-site.com/page"],
"stealth": True,
"format": "html",
})
print(stealth_result.results[0].html)Using ExtractParams for Type Safety
from serpex import SerpexClient, ExtractParams
client = SerpexClient("sk_your_api_key")
params = ExtractParams(
urls=["https://example.com", "https://httpbin.org"],
stealth=False,
format="markdown",
)
result = client.extract(params)Extract Parameters
@dataclass
class ExtractParams:
urls: list[str] # Required. Max 10 URLs per request.
stealth: bool = False # 5 credits/URL when True.
format: str = "markdown" # "markdown" | "html"Error Handling
from serpex import SerpexClient, SerpApiException
client = SerpexClient("sk_your_api_key")
try:
results = client.search({"q": "test query"})
except SerpApiException as e:
print(f"API error: {e}")
print(f"Status code: {e.status_code}")
print(f"Details: {e.details}")Requirements
- Python 3.8+
requests