Skip to main content

bt-network-api

A FastAPI REST service exposing Bittensor network data through the Bittensor SDK.

Features

  • Chain status and network data over HTTP
  • Mock mode for offline development and testing
  • Configurable network, host, and port
  • SQLAlchemy-powered persistent cache (SQLite by default; PostgreSQL/any DB supported)
  • Request audit log of every API call
  • API-key authentication (Bearer token) with per-key query tracking
  • Admin role to issue / revoke / rotate keys
  • Auto-bootstraps a single admin API key on first start (printed once in stdout)

Endpoints

Route Auth Description
GET / optional Service index
GET /health none Liveness check
GET /me bearer Information about the calling API key
GET /block bearer Current chain block number (cached)
GET /subnets bearer All subnet info (cached)
GET /neurons/{netuid} bearer Neurons on a subnet (cached)
GET /metagraph/{netuid} bearer Metagraph of a subnet (cached)
GET /delegates bearer Delegate identities (cached)
GET /staking/{address} bearer Stake info for a coldkey address (cached)
GET /cache/history bearer Last 20 served requests (audit log)
GET /cache/refresh bearer Bypass notice (cache invalidation hook)
GET /admin/keys admin List all API keys (redacted)
POST /admin/keys?name=X admin Issue a new API key (returns raw key once)
POST /admin/keys/{id}/revoke admin Revoke a key
POST /admin/keys/{id}/rotate admin Rotate a key (returns new raw key)
GET /admin/stats admin Aggregate service statistics

Install

pip install bt-network-api

Usage

# Run the server (defaults to 0.0.0.0:8091)
bt-network-api --host 0.0.0.0 --port 8091

# Or via uvicorn (factory mode — app is created per worker)
uv run uvicorn --factory --app-dir . bt_network_api.app:create_app --host 0.0.0.0 --port 8091

On first start, a single admin API key is created and printed once to stdout — capture it for use with the admin endpoints.

Verify it is running:

curl -s http://localhost:8091/health

Use an API key:

curl -H "Authorization: Bearer <API_KEY>" http://localhost:8091/me

Configuration

Variable Default Description
BT_API_NETWORK finney Bittensor network name
BT_API_MOCK 0 Use mock data (1) instead of the live chain
BT_API_DATABASE_URL sqlite:///./bt_network_api.db SQLAlchemy database URL (any DB supported)
BT_API_REQUIRE_AUTH 0 When 1, all / non-/health routes require Authorization: Bearer <key>
PORT / BT_API_PORT 8091 Bind port (Railway sets PORT automatically)

Database

Uses SQLAlchemy 2.x. Three tables are created on first start:

  • api_keys — API key registry (prefix, hashed value, name, role, query count, timestamps)
  • cached_queries — Latest cached response per (endpoint, key) pair
  • request_logs — Audit log of every served request (per-key attribution)

Any SQLAlchemy-compatible database works:

# PostgreSQL
export BT_API_DATABASE_URL=postgresql://user:pass@host:5432/db
# MySQL
export BT_API_DATABASE_URL=mysql+pymysql://user:pass@host/db
# SQLite (default)
export BT_API_DATABASE_URL=sqlite:////data/bt_network_api.db

Development

uv run ruff format --check src tests
uv run ruff check src
uv run mypy src/
uv run pytest
uv build

Metadata

Release files for bt-network-api 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bt-network-api 0.3.0
File Size Uploaded Provenance Actions
bt_network_api-0.3.0.tar.gz 13.0 kB Without provenance Details for bt_network_api-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bt-network-api 0.3.0
File Interpreter ABI Platform Actions
bt_network_api-0.3.0-py3-none-any.whl Python 3 none any Details for bt_network_api-0.3.0-py3-none-any.whl

Total release size: 24.5 kB

Release files / bt_network_api-0.3.0.tar.gz

Download URL bt_network_api-0.3.0.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b7e5c4c9808e8c6dbfba0c12685b72b86af72e074decc87e974df0b8b9320ef1
BLAKE2b-256 checksum
How to use checksums
2cf476066a59a539a5190940a3dd5ea15359e46b79f441b7ec0cff49709d262f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.2

Release files / bt_network_api-0.3.0-py3-none-any.whl

Download URL bt_network_api-0.3.0-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b783af31d331574cb9354bab55f5c6d49da91abb39fb398fa108947f3803c46
BLAKE2b-256 checksum
How to use checksums
36d23cedd45c8d7ebe3441500cfe991c447ec3e1c376299a5ff00dd2d9e7cd00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.2

Security for bt-network-api 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page