Skip to content
EchoraAPI-data

Search across the Echora integration guides.

Documentation · API v1esc to close
Start building

Python

This example uses Python’s standard library. It selects an eligible keyword, reads two date filters, follows cursor-only pages and writes JSON Lines. Set ECHORA_BASE_URL and ECHORA_API_KEY in your backend environment.

import json
import os
from urllib.parse import urlencode, quote
from urllib.request import Request, urlopen
from urllib.error import HTTPError
BASE_URL = os.environ["ECHORA_BASE_URL"].rstrip("/")
API_KEY = os.environ["ECHORA_API_KEY"]
def api_get(path, params=None):
query = urlencode(params or {})
url = BASE_URL + path + ("?" + query if query else "")
request = Request(url, headers={
"X-API-Key": API_KEY,
"Accept": "application/json",
})
try:
with urlopen(request, timeout=90) as response:
return json.load(response)
except HTTPError as error:
try:
code = json.loads(error.read()).get("error", {}).get("code")
except (ValueError, UnicodeDecodeError):
code = "http_error"
# Do not expose request URLs, tokens or keys in public errors.
raise RuntimeError(f"Echora HTTP {error.code}: {code}") from None
def choose_keyword(source):
grouped = api_get("/v1/keywords")["data"]
return next((slot for slot in grouped[source]
if slot["is_active"] and slot["within_quota"]
and slot["path"]), None)
def iter_data(source, keyword_path, start, end, limit=100):
path = "/v1/data/" + quote(source, safe="")
path += "/" + quote(keyword_path, safe="")
params = {"start": start, "end": end, "limit": limit}
while True:
page = api_get(path, params)
yield from page["data"]
pagination = page["pagination"]
if not pagination["has_more"]:
return
cursor = pagination["next_cursor"]
if not cursor:
raise RuntimeError("Missing next_cursor")
params = {"cursor": cursor}
if __name__ == "__main__":
source = "news"
keyword = choose_keyword(source)
if keyword is None:
raise RuntimeError("No eligible keyword for this source")
seen = set()
with open("echora-data.jsonl", "w", encoding="utf-8") as output:
for item in iter_data(source, keyword["path"],
"2026-11-12", "2026-11-18", 100):
identity = (item["source_key"], item["id"])
if identity in seen:
continue
seen.add(identity)
output.write(json.dumps(item, ensure_ascii=False) + "\n")

Python preserves JSON integer precision. For large jobs, use database upserts keyed by source_key + id instead of keeping all seen IDs in memory. Choose your own historical period and timeout. The example performs no automatic retries.

After a keyword change, start a fresh query with its new path. Recheck keyword state before retrying timed-out writes. See pagination and errors.