# hikari-py/hikari architecture

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

- Interactive diagram: https://gitdiagram.com/hikari-py/hikari
- Repository: https://github.com/hikari-py/hikari

## Overview

#### Purpose and Public Entry Points
Hikari is a statically typed, asyncio-based Python microframework for Discord API v10. Users primarily import `hikari` and instantiate either a gateway-connected bot, an HTTP interaction endpoint bot, or a reusable REST application. Public API aggregation and CLI/module entry points are in `hikari/__init__.py`, `hikari/__main__.py`, and `hikari/cli.py`.

#### API/Implementation Boundary
Abstract protocols define substitutable service contracts, while `impl` provides default concrete implementations. This separates user-facing type/API dependencies from HTTP, gateway, cache, and event machinery.
- Contracts: `hikari/api/rest.py`, `hikari/api/event_manager.py`, `hikari/api/shard.py`
- Implementations: `hikari/impl/rest.py`, `hikari/impl/event_manager.py`, `hikari/impl/shard.py`

#### Gateway Bot Lifecycle
`GatewayBot` is the stateful orchestration entry point for long-lived Discord gateway clients. It composes REST access, shard management, event dispatch, optional caching, configuration, startup/shutdown handling, and the asyncio run lifecycle.
- `hikari/impl/gateway_bot.py`
- `hikari/impl/config.py`
- `hikari/impl/cache.py`

Control flow: start bot services → establish/manage one or more Gateway WebSocket shards → receive gateway payloads → deserialize to events/entities → update cache and dispatch listeners → handlers may call REST APIs.

#### Gateway Transport and Event Pipeline
Shard instances implement Discord Gateway connectivity, session/heartbeat/reconnect behavior, and payload intake. Event factories translate gateway payloads into typed event objects; the event manager routes those objects to registered listeners, including `GatewayBot.listen()` registrations.
- `hikari/impl/shard.py`
- `hikari/impl/event_factory.py`
- `hikari/impl/event_manager_base.py`

Typed event families are organized under `hikari/events/`, while interaction event payloads and models are represented under `hikari/interactions/`.

#### REST Client, Routes, and Rate Limits
The REST layer exposes Discord resources and endpoint builders, serializes requests, manages pooled HTTP access, and turns responses into library entities. Route definitions are kept internal; rate-limit buckets coordinate request execution against Discord limits.
- `hikari/impl/rest.py`
- `hikari/internal/routes.py`
- `hikari/impl/rate_limits.py`

`RESTApp` supports REST-only integrations and token-scoped acquired clients; `RESTBot` layers application startup and interaction serving over REST services.
- `hikari/impl/rest_bot.py`
- `hikari/api/special_endpoints.py`

#### HTTP Interaction Server Path
`RESTBot` hosts an interaction endpoint for Discord-delivered commands, components, and modals. The server validates/parses inbound interaction requests, uses the event factory/model layer, and dispatches handlers registered by interaction type. Response builders and REST follow-ups form the outbound boundary.
- `hikari/impl/interaction_server.py`
- `hikari/impl/rest_bot.py`
- `hikari/interactions/command_interactions.py`

This runtime requires externally reachable hosting and Discord application endpoint configuration; tunneling services such as ngrok are development-only deployment aids.

#### Domain Model and Entity Construction
Top-level modules define immutable/typed Discord resource models—users, guilds, channels, messages, commands, webhooks, permissions, and more. Entity factories centrally map REST and Gateway JSON into these models, insulating transports from domain representation.
- `hikari/channels.py`
- `hikari/messages.py`
- `hikari/impl/entity_factory.py`

#### Runtime and External Dependencies
The primary runtime is Python 3.10–3.14 with `asyncio`; Discord’s REST and Gateway APIs are the core external services. HTTP/proxy/timeout behavior is configured through `hikari/impl/config.py`, networking support lives in `hikari/internal/net.py`, and optional performance deployments use aiohttp speedups, `orjson`, `ciso8601`, and Unix `uvloop`.

Main entry points: [`hikari/impl/gateway_bot.py`](https://github.com/hikari-py/hikari/blob/master/hikari/impl/gateway_bot.py) (GatewayBot), [`hikari/impl/rest.py`](https://github.com/hikari-py/hikari/blob/master/hikari/impl/rest.py) (REST client), [`hikari/impl/interaction_server.py`](https://github.com/hikari-py/hikari/blob/master/hikari/impl/interaction_server.py) (Interaction server), [`hikari/impl/event_factory.py`](https://github.com/hikari-py/hikari/blob/master/hikari/impl/event_factory.py) (Event factory)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public API"]
  node_package["hikari package<br/>public API<br/>[__init__.py]"]
  node_cli["CLI and module entry<br/>entry point<br/>[cli.py]"]
end

subgraph group_gateway["Gateway runtime"]
  node_gateway_bot["GatewayBot<br/>gateway orchestrator<br/>[gateway_bot.py]"]
  node_gateway_config["Runtime config<br/>configuration runtime<br/>[config.py]"]
  node_shards["Gateway shards<br/>WebSocket transport<br/>[shard.py]"]
  node_event_factory["Event factory<br/>payload translator<br/>[event_factory.py]"]
  node_event_manager["Event manager<br/>event dispatcher<br/>[event_manager.py]"]
  node_cache[("Cache<br/>[cache.py]")]
end

subgraph group_rest["REST and interactions"]
  node_rest_client["REST client<br/>HTTP service<br/>[rest.py]"]
  node_routes["Routes<br/>endpoint builders<br/>[routes.py]"]
  node_rate_limits["Rate-limit buckets<br/>request coordinator<br/>[rate_limits.py]"]
  node_rest_bot["RESTBot<br/>interaction runtime<br/>[rest_bot.py]"]
  node_interaction_server["Interaction server<br/>HTTP endpoint"]
end

subgraph group_models["Models and events"]
  node_entity_factory["Entity factory<br/>model translator<br/>[entity_factory.py]"]
  node_domain_models["Discord entities<br/>typed domain models<br/>[channels.py]"]
  node_events["Event families<br/>typed events"]
  node_interactions["Interaction models<br/>interaction payloads"]
end

node_discord{{"Discord APIs<br/>external platform"}}

node_package -->|"exports"| node_gateway_bot
node_package -->|"exports"| node_rest_bot
node_cli -->|"starts"| node_gateway_bot
node_gateway_bot -->|"uses"| node_gateway_config
node_gateway_bot -->|"manages"| node_shards
node_gateway_bot -->|"registers listeners with"| node_event_manager
node_gateway_bot -->|"owns"| node_cache
node_gateway_bot -->|"composes"| node_rest_client
node_shards -->|"Gateway WebSocket"| node_discord
node_shards -->|"payloads"| node_event_factory
node_event_factory -->|"creates"| node_events
node_event_factory -->|"uses"| node_entity_factory
node_event_manager -->|"dispatches"| node_events
node_event_manager -->|"updates through events"| node_cache
node_rest_client -->|"builds requests from"| node_routes
node_rest_client -->|"schedules through"| node_rate_limits
node_rest_client -->|"deserializes with"| node_entity_factory
node_rest_client -->|"HTTPS API"| node_discord
node_entity_factory -->|"constructs"| node_domain_models
node_rest_bot -->|"runs"| node_rest_client
node_rest_bot -->|"hosts"| node_interaction_server
node_interaction_server -->|"parses"| node_interactions
node_interaction_server -->|"uses"| node_event_factory
node_interaction_server -->|"interaction responses"| node_discord

click node_package "https://github.com/hikari-py/hikari/blob/master/hikari/__init__.py"
click node_cli "https://github.com/hikari-py/hikari/blob/master/hikari/cli.py"
click node_gateway_bot "https://github.com/hikari-py/hikari/blob/master/hikari/impl/gateway_bot.py"
click node_gateway_config "https://github.com/hikari-py/hikari/blob/master/hikari/impl/config.py"
click node_shards "https://github.com/hikari-py/hikari/blob/master/hikari/impl/shard.py"
click node_event_factory "https://github.com/hikari-py/hikari/blob/master/hikari/impl/event_factory.py"
click node_event_manager "https://github.com/hikari-py/hikari/blob/master/hikari/impl/event_manager.py"
click node_cache "https://github.com/hikari-py/hikari/blob/master/hikari/impl/cache.py"
click node_rest_client "https://github.com/hikari-py/hikari/blob/master/hikari/impl/rest.py"
click node_routes "https://github.com/hikari-py/hikari/blob/master/hikari/internal/routes.py"
click node_rate_limits "https://github.com/hikari-py/hikari/blob/master/hikari/impl/rate_limits.py"
click node_rest_bot "https://github.com/hikari-py/hikari/blob/master/hikari/impl/rest_bot.py"
click node_interaction_server "https://github.com/hikari-py/hikari/blob/master/hikari/impl/interaction_server.py"
click node_entity_factory "https://github.com/hikari-py/hikari/blob/master/hikari/impl/entity_factory.py"
click node_domain_models "https://github.com/hikari-py/hikari/blob/master/hikari/channels.py"
click node_interactions "https://github.com/hikari-py/hikari/blob/master/hikari/interactions/command_interactions.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_package,node_cli toneBlue
class node_gateway_bot,node_gateway_config,node_shards,node_event_factory,node_event_manager,node_cache toneAmber
class node_rest_client,node_routes,node_rate_limits,node_rest_bot,node_interaction_server toneMint
class node_entity_factory,node_domain_models,node_events,node_interactions toneRose
class node_discord toneNeutral
```

## Components

### Public API

- [hikari package](https://github.com/hikari-py/hikari/blob/master/hikari/__init__.py): public API · Aggregates the user-facing library API. · `hikari/__init__.py`
- [CLI and module entry](https://github.com/hikari-py/hikari/blob/master/hikari/cli.py): entry point · `hikari/cli.py`

### Gateway runtime

- [GatewayBot](https://github.com/hikari-py/hikari/blob/master/hikari/impl/gateway_bot.py): gateway orchestrator · Owns the long-lived asyncio gateway lifecycle. · `hikari/impl/gateway_bot.py`
- [Runtime config](https://github.com/hikari-py/hikari/blob/master/hikari/impl/config.py): configuration runtime · Provides HTTP, proxy, timeout, and bot settings. · `hikari/impl/config.py`
- [Gateway shards](https://github.com/hikari-py/hikari/blob/master/hikari/impl/shard.py): WebSocket transport · Manages sessions, heartbeats, reconnects, and payload intake. · `hikari/impl/shard.py`
- [Event factory](https://github.com/hikari-py/hikari/blob/master/hikari/impl/event_factory.py): payload translator · Converts gateway payloads into typed events. · `hikari/impl/event_factory.py`
- [Event manager](https://github.com/hikari-py/hikari/blob/master/hikari/impl/event_manager.py): event dispatcher · Routes typed events to registered listeners. · `hikari/impl/event_manager.py`
- [Cache](https://github.com/hikari-py/hikari/blob/master/hikari/impl/cache.py): entity cache · `hikari/impl/cache.py`

### REST and interactions

- [REST client](https://github.com/hikari-py/hikari/blob/master/hikari/impl/rest.py): HTTP service · Serializes Discord API requests and builds response entities. · `hikari/impl/rest.py`
- [Routes](https://github.com/hikari-py/hikari/blob/master/hikari/internal/routes.py): endpoint builders · `hikari/internal/routes.py`
- [Rate-limit buckets](https://github.com/hikari-py/hikari/blob/master/hikari/impl/rate_limits.py): request coordinator · Coordinates execution against Discord limits. · `hikari/impl/rate_limits.py`
- [RESTBot](https://github.com/hikari-py/hikari/blob/master/hikari/impl/rest_bot.py): interaction runtime · Starts REST services and hosts interaction handling. · `hikari/impl/rest_bot.py`
- [Interaction server](https://github.com/hikari-py/hikari/blob/master/hikari/impl/interaction_server.py): HTTP endpoint · Validates and parses Discord interaction requests. · `hikari/impl/interaction_server.py`

### Models and events

- [Entity factory](https://github.com/hikari-py/hikari/blob/master/hikari/impl/entity_factory.py): model translator · Maps REST and gateway JSON into domain models. · `hikari/impl/entity_factory.py`
- [Discord entities](https://github.com/hikari-py/hikari/blob/master/hikari/channels.py): typed domain models · Immutable resource models such as channels and messages. · `hikari/channels.py`
- **Event families**: typed events
- [Interaction models](https://github.com/hikari-py/hikari/blob/master/hikari/interactions/command_interactions.py): interaction payloads · `hikari/interactions/command_interactions.py`

### Other components

- **Discord APIs**: external platform · Discord REST API and Gateway service.

## Connections

- hikari package → GatewayBot: exports
- hikari package → RESTBot: exports
- CLI and module entry → GatewayBot: starts
- GatewayBot → Runtime config: uses
- GatewayBot → Gateway shards: manages
- GatewayBot → Event manager: registers listeners with
- GatewayBot → Cache: owns
- GatewayBot → REST client: composes
- Gateway shards → Discord APIs: Gateway WebSocket
- Gateway shards → Event factory: payloads
- Event factory → Event families: creates
- Event factory → Entity factory: uses
- Event manager → Event families: dispatches
- Event manager → Cache: updates through events
- REST client → Routes: builds requests from
- REST client → Rate-limit buckets: schedules through
- REST client → Entity factory: deserializes with
- REST client → Discord APIs: HTTPS API
- Entity factory → Discord entities: constructs
- RESTBot → REST client: runs
- RESTBot → Interaction server: hosts
- Interaction server → Interaction models: parses
- Interaction server → Event factory: uses
- Interaction server → Discord APIs: interaction responses
