How hummingbot/hummingbot-api works
Purpose and Runtime Entry Points
Hummingbot API is a FastAPI-style control plane for Hummingbot trading: accounts, exchange connectors, orders, portfolio data, bot lifecycle, backtesting, and DEX Gateway operations. The application entry point is main.py; dependency wiring and runtime settings are centralized in deps.py and config.py. REST routes are organized under routers/, with interactive API docs served by the application runtime.
HTTP API and Request Boundary
Routers expose domain-specific REST and WebSocket interfaces, delegating business work to services rather than directly managing infrastructure. Key boundaries include bot orchestration (routers/bot_orchestration.py), trading (routers/trading.py), Gateway (routers/gateway.py), and account/connector administration (routers/accounts.py, routers/connectors.py). Real-time delivery is represented by routers/websocket.py.
Service Layer and Trading Control Flow
Services implement operational workflows: validate request/domain models, communicate with bot or exchange-facing systems, and record resulting activity. services/bots_orchestrator.py owns bot deployment/control; services/trading_service.py and services/perpetual_trading_service.py manage spot and perpetual trading; services/unified_connector_service.py abstracts connector access. Executor status/events are coordinated through services/executor_service.py and services/executor_ws_manager.py.
Persistence and Historical State
PostgreSQL is the durable system of record, accessed through database/connection.py, ORM/schema definitions in database/models.py, and domain repositories such as database/repositories/order_repository.py, database/repositories/trade_repository.py, and database/repositories/bot_run_repository.py. Services record orders, funding, and executions via services/orders_recorder.py and services/funding_recorder.py; portfolio and history APIs derive views through services/portfolio_analytics_service.py and services/trading_history_service.py.
Bot Strategies and Hummingbot Integration
Bot-specific assets live under bots/: reusable controller implementations include directional strategies (bots/controllers/directional_trading/) and market-making strategies (bots/controllers/market_making/). bots/scripts/v2_with_controllers.py is the Hummingbot-side script entry for controller-based bot runs. Instance, credential, and generated runtime assets are intentionally separated under bots/instances/, bots/credentials/, and bots/conf/, establishing a filesystem boundary between API code and managed bot state.
Gateway and External Connectivity
The API controls Hummingbot Gateway for DEX/AMM, swap, and concentrated-liquidity workflows through services/gateway_service.py, services/gateway_client.py, and services/gateway_transaction_poller.py; routes are split by Gateway capability in routers/gateway_amm.py, routers/gateway_swap.py, and routers/gateway_clmm.py. Gateway runs with TLS/mTLS and generated certificates, supported by utils/gateway_certs.py. Market data can use external ticker/price sources via services/market_data_service.py, services/ticker_sources.py, and services/gecko_price_source.py.
Deployment, Messaging, and Security
Docker Compose deploys the API alongside PostgreSQL and the EMQX MQTT broker using docker-compose.yml; Dockerfile defines the API image. MQTT support is encapsulated in utils/mqtt_manager.py. Optional private-network exposure uses a Tailscale sidecar in docker-compose.tailscale.yml. API authentication and secret handling are supported by utils/security.py; configuration-password-protected bot credentials and Gateway wallets are material security boundaries.
Main entry points: main.py FastAPI application · bots_orchestrator.py Bot orchestrator · trading_service.py Spot & perpetual trading · unified_connector_service.py Unified connector access
Components 22
API Runtime
- FastAPI application · API entry point with REST, WebSocket, and interactive docs.
main.py - Runtime wiring · Dependencies and runtime settings.
deps.py - Bot routes
routers/bot_orchestration.py - Trading routes
routers/trading.py - Gateway routes · AMM, swap, and CLMM endpoints.
routers/gateway.py - Account & connector routes
routers/accounts.py - WebSocket delivery
routers/websocket.py
Control Services
- Bot orchestrator · Deploys and controls bot runs.
services/bots_orchestrator.py - Spot & perpetual trading
services/trading_service.py - Unified connector access
services/unified_connector_service.py - Executor event coordination · Coordinates executor status and WebSocket events.
services/executor_service.py - Recorders & portfolio views · Records activity and derives portfolio/history views.
services/orders_recorder.py - Gateway control · DEX, AMM, swap, and liquidity workflows.
services/gateway_service.py - Market data sources
services/market_data_service.py
State & Bots
- PostgreSQL · Durable record for orders, trades, and bot runs.
database/connection.py - Domain repositories · Order, trade, and bot-run persistence.
database/repositories/order_repository.py - Hummingbot bot script · Controller-based Hummingbot run entry.
bots/scripts/v2_with_controllers.py - Managed bot state · Instances, credentials, and generated bot configuration.
External Systems
- Hummingbot & exchanges
- Hummingbot Gateway · TLS/mTLS-protected DEX connectivity.
- EMQX MQTT
- Ticker & price providers
Connections 22
- Runtime wiring → FastAPI application: configures
- FastAPI application → Bot routes: serves
- FastAPI application → Trading routes: serves
- FastAPI application → Gateway routes: serves
- FastAPI application → Account & connector routes: serves
- FastAPI application → WebSocket delivery: serves
- Bot routes → Bot orchestrator: controls bots
- Trading routes → Spot & perpetual trading: places trades
- Account & connector routes → Unified connector access: manages connectors
- Gateway routes → Gateway control: controls DEX actions
- Bot orchestrator → Hummingbot bot script: launches
- Bot orchestrator → Managed bot state: manages
- Hummingbot bot script → Hummingbot & exchanges: runs on
- Spot & perpetual trading → Unified connector access: uses
- Unified connector access → Hummingbot & exchanges: accesses exchanges
- Executor event coordination → WebSocket delivery: publishes status
- Recorders & portfolio views → Domain repositories: persists and queries
- Domain repositories → PostgreSQL: stores records
- Spot & perpetual trading → Recorders & portfolio views: records activity
- Gateway control → Hummingbot Gateway: invokes
- Market data sources → Ticker & price providers: fetches prices
- FastAPI application → EMQX MQTT: messaging