# hummingbot/hummingbot-api architecture

> Architecture diagram of the GitHub repository hummingbot/hummingbot-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hummingbot/hummingbot-api
- Repository: https://github.com/hummingbot/hummingbot-api

## Overview

#### 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`](https://github.com/hummingbot/hummingbot-api/blob/main/main.py) (FastAPI application), [`services/bots_orchestrator.py`](https://github.com/hummingbot/hummingbot-api/blob/main/services/bots_orchestrator.py) (Bot orchestrator), [`services/trading_service.py`](https://github.com/hummingbot/hummingbot-api/blob/main/services/trading_service.py) (Spot & perpetual trading), [`services/unified_connector_service.py`](https://github.com/hummingbot/hummingbot-api/blob/main/services/unified_connector_service.py) (Unified connector access)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API Runtime"]
  node_app["FastAPI application<br/>[main.py]"]
  node_runtime_wiring["Runtime wiring<br/>configuration<br/>[deps.py]"]
  node_bot_routes["Bot routes<br/>router"]
  node_trading_routes["Trading routes<br/>router<br/>[trading.py]"]
  node_gateway_routes["Gateway routes<br/>router<br/>[gateway.py]"]
  node_admin_routes["Account &amp; connector routes<br/>router<br/>[accounts.py]"]
  node_websocket_routes["WebSocket delivery<br/>router<br/>[websocket.py]"]
end

subgraph group_services["Control Services"]
  node_bot_orchestrator{{"Bot orchestrator"}}
  node_trading_services["Spot &amp; perpetual trading<br/>services<br/>[trading_service.py]"]
  node_connector_service["Unified connector access"]
  node_executor_events["Executor event coordination"]
  node_recorders_analytics["Recorders &amp; portfolio views<br/>services<br/>[orders_recorder.py]"]
  node_gateway_service{{"Gateway control<br/>[gateway_service.py]"}}
  node_market_data["Market data sources"]
end

subgraph group_state["State &amp; Bots"]
  node_postgres[("PostgreSQL<br/>database<br/>[connection.py]")]
  node_repositories["Domain repositories<br/>persistence"]
  node_bot_script["Hummingbot bot script<br/>bot runtime"]
  node_managed_bot_state["Managed bot state<br/>filesystem boundary"]
end

subgraph group_external["External Systems"]
  node_hummingbot{{"Hummingbot &amp; exchanges<br/>external trading runtime"}}
  node_gateway{{"Hummingbot Gateway<br/>external DEX runtime"}}
  node_mqtt["EMQX MQTT<br/>message broker"]
  node_market_providers["Ticker &amp; price providers<br/>external data source"]
end

node_runtime_wiring -->|"configures"| node_app
node_app -->|"serves"| node_bot_routes
node_app -->|"serves"| node_trading_routes
node_app -->|"serves"| node_gateway_routes
node_app -->|"serves"| node_admin_routes
node_app -->|"serves"| node_websocket_routes
node_bot_routes -->|"controls bots"| node_bot_orchestrator
node_trading_routes -->|"places trades"| node_trading_services
node_admin_routes -->|"manages connectors"| node_connector_service
node_gateway_routes -->|"controls DEX actions"| node_gateway_service
node_bot_orchestrator -->|"launches"| node_bot_script
node_bot_orchestrator -->|"manages"| node_managed_bot_state
node_bot_script -->|"runs on"| node_hummingbot
node_trading_services -->|"uses"| node_connector_service
node_connector_service -->|"accesses exchanges"| node_hummingbot
node_executor_events -->|"publishes status"| node_websocket_routes
node_recorders_analytics -->|"persists and queries"| node_repositories
node_repositories -->|"stores records"| node_postgres
node_trading_services -->|"records activity"| node_recorders_analytics
node_gateway_service -->|"invokes"| node_gateway
node_market_data -->|"fetches prices"| node_market_providers
node_app -.->|"messaging"| node_mqtt

click node_app "https://github.com/hummingbot/hummingbot-api/blob/main/main.py"
click node_runtime_wiring "https://github.com/hummingbot/hummingbot-api/blob/main/deps.py"
click node_bot_routes "https://github.com/hummingbot/hummingbot-api/blob/main/routers/bot_orchestration.py"
click node_trading_routes "https://github.com/hummingbot/hummingbot-api/blob/main/routers/trading.py"
click node_gateway_routes "https://github.com/hummingbot/hummingbot-api/blob/main/routers/gateway.py"
click node_admin_routes "https://github.com/hummingbot/hummingbot-api/blob/main/routers/accounts.py"
click node_websocket_routes "https://github.com/hummingbot/hummingbot-api/blob/main/routers/websocket.py"
click node_bot_orchestrator "https://github.com/hummingbot/hummingbot-api/blob/main/services/bots_orchestrator.py"
click node_trading_services "https://github.com/hummingbot/hummingbot-api/blob/main/services/trading_service.py"
click node_connector_service "https://github.com/hummingbot/hummingbot-api/blob/main/services/unified_connector_service.py"
click node_executor_events "https://github.com/hummingbot/hummingbot-api/blob/main/services/executor_service.py"
click node_recorders_analytics "https://github.com/hummingbot/hummingbot-api/blob/main/services/orders_recorder.py"
click node_gateway_service "https://github.com/hummingbot/hummingbot-api/blob/main/services/gateway_service.py"
click node_market_data "https://github.com/hummingbot/hummingbot-api/blob/main/services/market_data_service.py"
click node_postgres "https://github.com/hummingbot/hummingbot-api/blob/main/database/connection.py"
click node_repositories "https://github.com/hummingbot/hummingbot-api/blob/main/database/repositories/order_repository.py"
click node_bot_script "https://github.com/hummingbot/hummingbot-api/blob/main/bots/scripts/v2_with_controllers.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_runtime_wiring,node_bot_routes,node_trading_routes,node_gateway_routes,node_admin_routes,node_websocket_routes toneBlue
class node_bot_orchestrator,node_trading_services,node_connector_service,node_executor_events,node_recorders_analytics,node_gateway_service,node_market_data toneAmber
class node_postgres,node_repositories,node_bot_script,node_managed_bot_state toneMint
class node_hummingbot,node_gateway,node_mqtt,node_market_providers toneRose
```

## Components

### API Runtime

- [FastAPI application](https://github.com/hummingbot/hummingbot-api/blob/main/main.py): application · API entry point with REST, WebSocket, and interactive docs. · `main.py`
- [Runtime wiring](https://github.com/hummingbot/hummingbot-api/blob/main/deps.py): configuration · Dependencies and runtime settings. · `deps.py`
- [Bot routes](https://github.com/hummingbot/hummingbot-api/blob/main/routers/bot_orchestration.py): router · `routers/bot_orchestration.py`
- [Trading routes](https://github.com/hummingbot/hummingbot-api/blob/main/routers/trading.py): router · `routers/trading.py`
- [Gateway routes](https://github.com/hummingbot/hummingbot-api/blob/main/routers/gateway.py): router · AMM, swap, and CLMM endpoints. · `routers/gateway.py`
- [Account & connector routes](https://github.com/hummingbot/hummingbot-api/blob/main/routers/accounts.py): router · `routers/accounts.py`
- [WebSocket delivery](https://github.com/hummingbot/hummingbot-api/blob/main/routers/websocket.py): router · `routers/websocket.py`

### Control Services

- [Bot orchestrator](https://github.com/hummingbot/hummingbot-api/blob/main/services/bots_orchestrator.py): service · Deploys and controls bot runs. · `services/bots_orchestrator.py`
- [Spot & perpetual trading](https://github.com/hummingbot/hummingbot-api/blob/main/services/trading_service.py): services · `services/trading_service.py`
- [Unified connector access](https://github.com/hummingbot/hummingbot-api/blob/main/services/unified_connector_service.py): service · `services/unified_connector_service.py`
- [Executor event coordination](https://github.com/hummingbot/hummingbot-api/blob/main/services/executor_service.py): service · Coordinates executor status and WebSocket events. · `services/executor_service.py`
- [Recorders & portfolio views](https://github.com/hummingbot/hummingbot-api/blob/main/services/orders_recorder.py): services · Records activity and derives portfolio/history views. · `services/orders_recorder.py`
- [Gateway control](https://github.com/hummingbot/hummingbot-api/blob/main/services/gateway_service.py): service · DEX, AMM, swap, and liquidity workflows. · `services/gateway_service.py`
- [Market data sources](https://github.com/hummingbot/hummingbot-api/blob/main/services/market_data_service.py): service · `services/market_data_service.py`

### State & Bots

- [PostgreSQL](https://github.com/hummingbot/hummingbot-api/blob/main/database/connection.py): database · Durable record for orders, trades, and bot runs. · `database/connection.py`
- [Domain repositories](https://github.com/hummingbot/hummingbot-api/blob/main/database/repositories/order_repository.py): persistence · Order, trade, and bot-run persistence. · `database/repositories/order_repository.py`
- [Hummingbot bot script](https://github.com/hummingbot/hummingbot-api/blob/main/bots/scripts/v2_with_controllers.py): bot runtime · Controller-based Hummingbot run entry. · `bots/scripts/v2_with_controllers.py`
- **Managed bot state**: filesystem boundary · Instances, credentials, and generated bot configuration.

### External Systems

- **Hummingbot & exchanges**: external trading runtime
- **Hummingbot Gateway**: external DEX runtime · TLS/mTLS-protected DEX connectivity.
- **EMQX MQTT**: message broker
- **Ticker & price providers**: external data source

## Connections

- 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
