Api
The Api is a continuous service that provides a read-only REST API built with FastAPI. It exposes all BigBrotr tables and materialized views over HTTP with automatic schema discovery, filtering, sorting, and pagination.
Purpose
Section titled “Purpose”The Api answers: How can HTTP clients access BigBrotr data? It dynamically generates routes from the PostgreSQL catalog, so any new table or materialized view is automatically available without code changes.
How It Works
Section titled “How It Works”- On startup, queries the PostgreSQL catalog to discover available tables and views.
- Generates REST endpoints for each enabled table/view.
- Serves HTTP requests with filtering, sorting, and pagination.
- Runs continuously as a FastAPI/uvicorn server.
Endpoints
Section titled “Endpoints”| Method | Path | Description |
|---|---|---|
GET | /health | Health check ({"status": "ok"}) |
GET | {prefix}/schema | List all enabled tables with column metadata |
GET | {prefix}/schema/{table} | Table detail (columns, types, primary key) |
GET | {prefix}/{table} | List rows with pagination, filtering, and sorting |
GET | {prefix}/{table}/{pk} | Single row by primary key |
Query Parameters
Section titled “Query Parameters”Data endpoints support:
limit— Maximum rows to return (default configurable).offset— Skip rows for pagination.sort— Sort by column with direction:sort=col:ascorsort=col:desc.- Column filters — Filter by column value:
col=valueor with operators:col=op:value.
Supported filter operators: =, >, <, >=, <=, ILIKE.
Cross-Origin Resource Sharing is configurable via cors_origins. When set, the Api adds appropriate CORS headers to allow browser-based clients.
Configuration
Section titled “Configuration”title: "BigBrotr API"host: 0.0.0.0port: 8080route_prefix: /v1
cors_origins: - "https://bigbrotr.com"
request_timeout: 30.0 # per-request timeout in secondsmax_page_size: 1000 # hard ceiling on limit parameterdefault_page_size: 100 # default if limit not provided
tables: relay: enabled: true event: enabled: true metadata: enabled: true relay_metadata: enabled: true relay_stats: enabled: true event_stats: enabled: true kind_counts: enabled: true network_stats: enabled: true relay_software_counts: enabled: true supported_nip_counts: enabled: true# Run continuouslypython -m bigbrotr api
# Example queriescurl http://localhost:8080/v1/relay?limit=10&sort=discovered_at:desccurl http://localhost:8080/v1/relay_stats?network=clearnetcurl http://localhost:8080/v1/schemaShared Infrastructure
Section titled “Shared Infrastructure”The Api uses CatalogAccessMixin which provides the Catalog class from services/common/catalog.py as a database query facade. Catalog discovers the schema from PostgreSQL’s information_schema and pg_catalog at startup, then translates HTTP query parameters into safe parameterized SQL queries. It raises CatalogError (instead of raw database exceptions) to prevent leaking database internals to clients.
Error responses: 400 (invalid params), 404 (not found/disabled table), 504 (query timeout).