Signals. Infra
Recipes

Download a workbook

Get a short-lived link to a company's formatted, source-linked Excel workbook.

GET /v1/companies/{symbol}/workbook returns a signed link to the same .xlsx the Signals app exports: KPIs and the three statements, every value linked to its source. The link is the credential, so it needs no header, and it expires after 15 minutes. Available on Starter and above.

import os
import pathlib
import time

import httpx

BASE_URL = os.environ.get("SIGNALS_BASE_URL", "https://api.signals.ai")
API_KEY = os.environ["SIGNALS_API_KEY"]


def get(client, url, **kwargs):
    """GET with the rate limit honoured: a 429 waits for its Retry-After."""
    for _attempt in range(6):
        r = client.get(url, **kwargs)
        if r.status_code != 429:
            r.raise_for_status()
            return r
        time.sleep(float(r.headers.get("Retry-After", "1")))
    r.raise_for_status()
    return r


def download_workbook(client, ticker, *, metric_keys=(), periods=12, out_dir="."):
    """Mint a link, download the workbook, return the file's path."""
    r = get(client, f"/v1/companies/{ticker}/workbook",
            params={"periods": periods, "metric_keys": ",".join(metric_keys)})
    link = r.json()["url"]
    wb = httpx.get(link, timeout=120)       # the signed link is the credential
    wb.raise_for_status()
    path = pathlib.Path(out_dir) / f"Signals-{ticker}-Workbook.xlsx"
    path.write_bytes(wb.content)
    return path


if __name__ == "__main__":
    headers = {"Authorization": f"Bearer {API_KEY}"}
    with httpx.Client(base_url=BASE_URL, headers=headers, timeout=60) as client:
        print(download_workbook(client, "UBER", periods=8))

An expired or altered link returns 403. Mint a new one rather than storing it.