Network API
The network API module provides a template for spanning a REST API that acts as a bridge between your core P2P Node and separate local or remote processes.
It is designed for cases where you might have heavy workloads, such as AI inference tasks, external logic servers, or monitoring dashboards, that operate outside of the node but still need to invoke P2P protocols, query peer statuses, or publish consensus/gossip messages.
The Network API enables you to call an API endpoint hosted on the P2P process from a separate local or remote process to access and interact with the P2P layer of your peer.
For example, external processes can call this API endpoint on the P2P process to trigger a gossipsub publish, call the server to retrieve data from the database, call a peer, or call to execute on a blockchain extrinsic. This can also be used to fully compartmentalize the node's business logic from the P2P layer, where that separate process can trigger all communication between peers.

Network API + P2P Communication
The Network API can also be used to port a previously developed distributed AI application that primarily uses an API to communicate between servers and turn them into a decentralized AI application by communicating with peers using the GossipSub or the API Protocol.
The Network API can effectively be used as a middleman between the previously built application and the new P2P layer, where it can safely communicate with peers using built-in encrypted messaging.
Configuration
Similar to the API Protocol, it's suggested to use a JSON file for the Network APIs configuration so it can be updated without restarting the peer and reconnecting to the network.
JSON
listen_host: Interface address the server binds to, such as127.0.0.1or0.0.0.0.port: TCP port the server listens on.whitelist_ips: Client IPs allowed to call the API; an empty list allows all IPs.enable_api: Whether the API server should start.
Configuration Class
Initiate the Server
The NetworkApi class can be initialized with everything from the peer host, DHT, gossipsub, pubsub, database, etc., allowing access to anything in the P2P layer from another process.
Add Custom Logic
Within the NetworkApi class, add functions that the API can call to access operations or data.
In the following example, the API can be called from a separate process to publish a message to the pubsub for all other peers to receive.
Customize Endpoints
By default, the template includes a single demonstration route: [POST] /v1/publish that expects a topic and message payload (this is just an example endpoint).
To expand this to handle custom requirements (like pinging specific tasks or triggering DHT lookups), modify the _setup_template_routes() method in network_api.py.
Alternatively, inject routes dynamically from another location dynamically:
Stopping Operations
Always ensure await api_server.stop() is captured on process teardown to prevent Uvicorn from abruptly terminating long-running external hook executions.
Last updated