This document specifies an Application Programming Interface (API) that is RECOMMENDED for developers of the [WAKU2](https://github.com/vacp2p/rfc-index/blob/7b443c1aab627894e3f22f5adfbb93f4c4eac4f6/waku/standards/core/10/waku2.md) clients to implement,
and for consumers to use as a single entry point to its functionalities.
This API extends the core protocols with additional functionality
required for peer-to-peer messaging applications, including:
- Defines a set of events which MAY be used for subscription to track message propagation, node state, and error handling.
- Defines a set of method calls which MAY be used for invocation of protocol operations.
- Abstracts new primitives to be accessed in a RESTful manner via [Waku REST API](https://waku-org.github.io/waku-rest-api/).
This document defines the specifications and guidelines necessary for implementing the API,
ensuring interoperability and consistency across the Waku protocol family.
## Design Requirements
The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”,
“RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in [RFC2119](https://www.ietf.org/rfc/rfc2119.txt).
### Definitions
This section defines key terms and concepts used throughout this document.
Each term is detailed in dedicated subsections to ensure clarity and consistency in interpretation.
`Multiaddr` - a self-describing format for encoding network address of a remote peer as per [libp2p addressing](https://github.com/libp2p/specs/blob/6d38f88f7b2d16b0e4489298bcd0737a6d704f7e/addressing/README.md) specification.
### Motivation
Real-world application development has exposed challenges in achieving comprehensive usability guarantees for core Waku protocols.
Although recommendations such as [P2P-RELIABILITY](./p2p-reliability.md) have been introduced to enhance protocol robustness,
the fragmented landscape of guidelines and the requirement to re-implement reliability strategies across diverse use cases remain evident.
A significant challenge has been the lack of a unified abstraction for message exchange.
Developers require a straightforward mechanism to send,
receive, and track messages—whether originating from a node or propagated throughout its connected network.
## API design
### Requirements
This API is designed for generic use and ease of implementation across `nwaku`, `js-waku`,
and traditional `REST` architectures.
From this point forward,
relevant code blocks will be provided in `JSON` format, `HTTP` format or by using `TypeScript` syntax.
### Initial configuration
The `Messaging API` is an abstraction layer built upon the basic functionality provided by a node.
The configuration definitions that follow omit implementation-specific details,
focusing exclusively on settings explicitly required by the `Messaging API`.
This property defines behavior of the node and MUST be specified.
If `edge` selected the node MUST use [LIGHTPUSH](../standards/core/lightpush.md) for sending messages,
and [FILTER](https://github.com/vacp2p/rfc-index/blob/7b443c1aab627894e3f22f5adfbb93f4c4eac4f6/waku/standards/core/12/filter.md) for receiving messages.
- Host endpoint for [LIGHTPUSH](../standards/core/lightpush.md) and [FILTER](https://github.com/vacp2p/rfc-index/blob/7b443c1aab627894e3f22f5adfbb93f4c4eac4f6/waku/standards/core/12/filter.md).
It signifies which cluster a node MUST be operating at as per [RELAY-SHARDING](https://github.com/waku-org/specs/blob/186ce335667bdfdb6b2ce69ad7b2a3a3791b1ba6/standards/core/relay-sharding.md).
##### `shards`
This property MUST be provided.
An array of shard under a specified cluster that node MUST operate at as per [RELAY-SHARDING](https://github.com/waku-org/specs/blob/186ce335667bdfdb6b2ce69ad7b2a3a3791b1ba6/standards/core/relay-sharding.md).
##### `storeNodes`
A list of `Multiaddr` addresses to remote peers that SHOULD be used for retrieving past messages or performing infrequent queries using the [STORE](https://github.com/vacp2p/rfc-index/blob/8ee2a6d6b232838d83374c35e2413f84436ecf64/waku/standards/core/13/store.md) protocol.
If not provided, nodes discovered through the network SHOULD be used.
##### `serviceNodes`
A list of `Multiaddr` addresses to remote peers that SHOULD be used for obtaining various network resources.
- a [LIGHTPUSH](../standards/core/lightpush.md) endpoint for broadcasting data;
- a [FILTER](https://github.com/vacp2p/rfc-index/blob/7b443c1aab627894e3f22f5adfbb93f4c4eac4f6/waku/standards/core/12/filter.md) endpoint for receiving circulating data.
If set to `true` and `confirmContentTopics` are provided, a recurring background [STORE](../standards/core/store.md) query (as described in the `Message Storage API` section) MUST be initiated.
If set to `true` without `confirmContentTopics`, the background [STORE](../standards/core/store.md) query (as described in the `Messaging Storage API` section) MUST only be initiated after the `Subscribe API` is called.
If set to `false`, the `stored` property on the `MessageRecord` (defined in the `Message Storage API`) MUST NOT be populated, unless `History API` is triggered.
If set to `true`, the node MUST initiate a network listener for messages circulated under `confirmContentTopics` via either [FILTER](https://github.com/vacp2p/rfc-index/blob/7b443c1aab627894e3f22f5adfbb93f4c4eac4f6/waku/standards/core/12/filter.md) or [RELAY](https://github.com/vacp2p/rfc-index/blob/0277fd0c4dbd907dfb2f0c28b6cde94a335e1fae/waku/standards/core/11/relay.md) using the `Subscribe API`.
If set to `true` but `confirmContentTopics` are not provided, the received property on the `MessageRecord` (as defined in the `Message Storage API`) MUST be populated only after the `Subscribe API` is called.
If set to `false`, the received property MUST NOT be populated, unless `Subscribe API` is called.
##### `confirmContentTopics`
An optional property that SHOULD be provided in conjunction with either `confirmStored` or `confirmReceived`.
It includes an array of `contentTopics` that the node MUST start monitoring in the background upon initialization,
as defined in the [TOPICS](https://github.com/vacp2p/rfc-index/blob/8ee2a6d6b232838d83374c35e2413f84436ecf64/waku/informational/23/topics.md#content-topic-format) specification.
#### Programmatic API / REST API
No methods or `REST` endpoints are provided for initial configuration.
Instead, the initial configuration MUST be supplied at node creation time according to the guidelines of the chosen implementation.
### Send
The `Send API` is responsible for broadcasting messages across the network using the configured protocol.
The node SHOULD select the appropriate protocol based on its configuration and the protocols that are mounted:
- If the initial configuration specifies mode: "relay", then [RELAY](https://github.com/vacp2p/rfc-index/blob/0277fd0c4dbd907dfb2f0c28b6cde94a335e1fae/waku/standards/core/11/relay.md) MUST be used to send messages.
- The message is not successfully sent using [LIGHTPUSH](../standards/core/lightpush.md) or [RELAY](https://github.com/vacp2p/rfc-index/blob/0277fd0c4dbd907dfb2f0c28b6cde94a335e1fae/waku/standards/core/11/relay.md).
- The message is not acknowledged by `Message Storage API` described below.
- Retry attempts MUST NOT continue if the node’s health state changes to unhealthy (as defined in the `Health API` described below).
- Retry attempts MUST resume when the node’s health state transitions to either `minimally healthy` or `healthy` defined in the `Health API` below.
Retry attempts MUST occur in the background using a fixed retry count and exponential backoff, as follows:
- A total of 3 retry attempts MUST be made following the initial send attempt.
- The retry intervals MUST be 1 second, then 2 seconds, and finally 4 seconds, respectively.
- The decision to retry MUST be based on the underlying protocol’s response from a remote peer.
#### Request prioritization
Send requests MUST be prioritized based on their submission time and retry status.
Freshly scheduled requests MUST be served before those undergoing retry attempts.
Among retry attempts, processing MUST occur in descending order of recency—that is,
from the most recently scheduled or retried request to the oldest.
#### Programmatic API
```typescript
type RequestID = string;
type Message = {
meta?: UInt8Array;
timestamp?: number;
ephemeral?: boolean;
payload: UInt8Array;
};
interface ISend {
public send(encoder: Encoder, message: Message): RequestID;
public cancelRequest(requestId: RequestID): void;
public getPendingRequests(): RequestID[];
};
```
##### `Encoder`
The `Encoder` object encapsulates the logic for message handling.
It MUST ensure that messages are encoded into the appropriate format for transmission.
Additionally, it MUST provide information regarding the `pubsubTopic` and `contentTopic` to which messages SHOULD be sent.
The specific types of encoders, their extended functionalities, and the mechanisms for their instantiation are considered out of scope for this document.
A `Message` object MUST conform to the [MESSAGE](https://github.com/vacp2p/rfc-index/blob/8ee2a6d6b232838d83374c35e2413f84436ecf64/waku/standards/core/14/message.md) specification.
When invoked, the method schedules the message to be sent via [LIGHTPUSH](../standards/core/lightpush.md) or [RELAY](https://github.com/vacp2p/rfc-index/blob/0277fd0c4dbd907dfb2f0c28b6cde94a335e1fae/waku/standards/core/11/relay.md) based on the node’s configuration.
The method MUST throw an error if the `pubsubTopic` provided by the `Encoder` is not supported by the underlying node.
The method MUST return the associated `RequestID` for the scheduled send attempt.
If the send operation fails, it MAY be retried from the `Message Storage API` using the `RequestID`.
Additionally, send operation can be monitored by subscribing to `message:sent` or `message:error` events described in `Event Source API` below.
##### `cancelRequest`
Cancels a scheduled send attempt associated with a given `RequestID`.
A call to this method MUST be ignored if the request has already been fulfilled (i.e., if it has succeeded, failed, or been canceled previously) or if the provided `RequestID` is invalid.
##### `getPendingRequests`
Returns an array of unique `RequestID` values corresponding to send operations that are still pending.
A request is considered pending if it has not yet succeeded,
and it may be actively retrying or scheduled for sending but has not yet been served.
No duplicate `RequestID` values MUST appear in the returned array.
The `Subscribe API` SHOULD be used to initiate a subscription over [FILTER](https://github.com/vacp2p/rfc-index/blob/7b443c1aab627894e3f22f5adfbb93f4c4eac4f6/waku/standards/core/12/filter.md) or [RELAY](https://github.com/vacp2p/rfc-index/blob/0277fd0c4dbd907dfb2f0c28b6cde94a335e1fae/waku/standards/core/11/relay.md) for continuous message retrieval.
A subscription is established via [FILTER](https://github.com/vacp2p/rfc-index/blob/7b443c1aab627894e3f22f5adfbb93f4c4eac4f6/waku/standards/core/12/filter.md) or [RELAY](https://github.com/vacp2p/rfc-index/blob/0277fd0c4dbd907dfb2f0c28b6cde94a335e1fae/waku/standards/core/11/relay.md) based on the node’s configuration for ongoing message reception.
a recurring [STORE](../standards/core/store.md) query MUST be initiated for the specified `contentTopics` to continuously update the stored property in the `MessageRecord` (as defined in the `Message Storage API`) for messages circulating in the network after the `Subscribe API` call.
The `Message Storage API` SHOULD be used to retrieve information about messages that have been sent,
received, or stored.
Message storage is populated by:
- Messages sent by the underlying node via the `Send API`.
- Messages received through an active subscription initiated by the `Subscribe API`.
- Messages fetched using the `History API`.
Messages can be identified by:
- The `message hash` as described in the [MESSAGE](https://github.com/vacp2p/rfc-index/blob/8ee2a6d6b232838d83374c35e2413f84436ecf64/waku/standards/core/14/message.md#deterministic-message-hashing) specification.
- The `contentTopic` associated with the message, as defined in the [TOPICS](https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md#content-topic-format) specification.
- The `RequestID` described in the `Send API` section.
- The `SubscriptionID` described in the `Subscribe API` section.
These identifiers SHOULD be passed to the appropriate methods described below to retrieve or manage messages.
Depending on the presence of the `confirmStored` or `confirmReceived` options in the `Initial configuration`, the `Message Storage API` will trigger either the `Subscribe API` or the periodic [STORE](../standards/core/store.md) query as described in the `Acknowledgements` section below.
Once a message is added to the `Message Storage API`, or if any details about it change,
the appropriate events from the `message:*` event family (as described in the `Event Source API` section) MUST be invoked after the message is added or updated in the underlying storage.
#### Acknowledgements
Message acknowledgement is a background operation that performs the following functions:
- It subscribes to [FILTER](https://github.com/vacp2p/rfc-index/blob/7b443c1aab627894e3f22f5adfbb93f4c4eac4f6/waku/standards/core/12/filter.md) or [RELAY](https://github.com/vacp2p/rfc-index/blob/0277fd0c4dbd907dfb2f0c28b6cde94a335e1fae/waku/standards/core/11/relay.md) and marks messages as `received` when detected.
- It periodically queries [STORE](../standards/core/store.md) and marks messages as `stored` when available.
The recurring [STORE](../standards/core/store.md) query MUST prioritize the `storeNodes` specified in the initial configuration before using other available nodes.
Recurring [STORE](../standards/core/store.md) query and background subscription to `confirmContentTopics` is a subscription defined in the `Subscribe API` and can be handled the same way.
Indicates whether a message has been confirmed to be present in the underlying [STORE](../standards/core/store.md) protocol.
This property is populated based on a recurring [STORE](../standards/core/store.md) query described in `Acknowledgements` section or via the `History API`.
Once a message is confirmed as stored, the `stored` property MUST be updated to `true` as a final state.
If an error occurs during reception, the `error` field MUST be populated with implementation-specific details.
##### `received`
Defaults to `false`.
Indicates whether a message was successfully received from the network via `Subscribe API`.
Once a message is received, the `received` property MUST be updated to `true` as a final state.
If an error occurs during reception, the `error` field MUST be populated with implementation-specific details.
##### `requestId`
Defaults to `null`.
An optional property applicable only to messages sent by the underlying node via the `Send API`.
When present, it corresponds to the `RequestID` from the `Send API`, remains constant once set.
##### `message`
This includes, but is not limited to, the following attributes:
-`payload`: MUST contain the message data payload.
-`contentTopic`: MUST specify a string identifier used for content-based filtering.
-`meta`: If present, contains an arbitrary application-specific variable-length byte array (maximum 64 bytes) for supplementary details.
-`version`: If present, contains a version number to distinguish different types of payload encryption; if omitted, it SHOULD be interpreted as version 0.
-`timestamp`: If present, indicates the time the message was generated (in Unix epoch nanoseconds); if omitted, it SHOULD be interpreted as 0.
-`ephemeral`: If present and `true`, the message is considered transient and SHOULD not be persisted; if omitted or `false`, the message is considered non-ephemeral.
For a complete description of the message attributes,
refer to the [MESSAGE](https://github.com/vacp2p/rfc-index/blob/8ee2a6d6b232838d83374c35e2413f84436ecf64/waku/standards/core/14/message.md#message-attributes) specification.
##### `error`
An optional property that, if present, indicates that an error occurred during the processing of the message—whether while sending or upon receiving.
This may include network, protocol, decoding errors, or other implementation-specific issues.
Once set, the error persists; however, if a message that initially encountered an error (e.g., associated with a `RequestID`) is later successfully transmitted, the error information MUST be cleared.
#### Methods
##### `getByRequestId`
Retrieves the `MessageRecord` corresponding to the specified `RequestID` associated with a send request for a particular message.
If the provided `RequestID` is invalid or no associated `MessageRecord` exists,
the method MUST return nothing.
##### `getByHash`
Retrieves the `MessageRecord` corresponding to the provided `message hash`.
The hash is computed deterministically based on the message content,
as described in the [MESSAGE](https://github.com/vacp2p/rfc-index/blob/8ee2a6d6b232838d83374c35e2413f84436ecf64/waku/standards/core/14/message.md#deterministic-message-hashing) specification.
If the provided hash is invalid or no associated `MessageRecord` exists,
the method MUST return nothing.
##### `getByContentTopic`
Retrieves an array of `MessageRecord` objects corresponding to messages that were circulated under the specified `contentTopic`.
The method accepts two optional parameters for pagination:
-`skip`: A non-negative integer specifying the number of initial matching records to bypass. If `skip` exceeds the total number of matching records, the method MUST return an empty array.
-`take`: A non-negative integer specifying the maximum number of records to return. If `take` is 0, the method MUST return an empty array. If `take` is greater than the number of available records, all remaining matching records are returned.
If both `skip``and` take are omitted, the method MUST return all matching records.
If no records match the specified `contentTopic`, an empty array MUST be returned.
##### `getBySubscriptionId`
Retrieves an array of `MessageRecord` objects corresponding to messages received under the specified `SubscriptionID` (as defined in the `Subscribe API` section).
The method accepts two optional parameters for pagination:
-`skip`: A non-negative integer specifying the number of initial matching records to bypass. If `skip` exceeds the total number of matching records, the method MUST return an empty array.
-`take`: A non-negative integer specifying the maximum number of records to return. If `take` is 0, the method MUST return an empty array. If `take` is greater than the number of available records, all remaining matching records are returned.
If both `skip` and `take` are omitted, the method MUST return all matching records.
If no records match the specified `SubscriptionID`, an empty array MUST be returned.
#### REST API
##### By `RequestID`
Request:
```http
GET /message?requestId=xyz789 HTTP/1.1
Accept: application/json
```
Response:
```http
HTTP/1.1 200 OK
Content-Type: application/json
{
"sending": true,
"sent": false,
"stored": false,
"received": false,
"requestId": "xyz789",
"message": {
"payload": "U29tZSBvdGhlciBkYXRh",
"contentTopic": "/example/another-topic"
}
}
```
Error for `404`:
```http
HTTP/1.1 404 Not Found
Content-Type: application/json
{
"error": "Message with requestId 'xyz789' not found"
}
```
##### By hash
Request:
```http
GET /message?hash=abc123 HTTP/1.1
Accept: application/json
```
Response:
```http
HTTP/1.1 200 OK
Content-Type: application/json
{
"sending": false,
"sent": false,
"stored": true,
"received": false,
"message": {
"payload": "SGVsbG8gd29ybGQ=",
"contentTopic": "/example/topic"
}
}
```
Error for `404`:
```http
HTTP/1.1 404 Not Found
Content-Type: application/json
{
"error": "Message with hash 'abc123' not found"
}
```
##### By `contentTopic`
Request:
```http
GET /messages?contentTopic=/messaging/2/api/utf8&skip=0&take=10 HTTP/1.1
Accept: application/json
```
Note: If skip and take are omitted, the request URL would be:
```http
GET /messages?contentTopic=/messaging/2/api/utf8 HTTP/1.1
```
Response:
```http
HTTP/1.1 200 OK
Content-Type: application/json
[
{
"sent": false,
"stored": false,
"received": true,
"message": {
"payload": "SW5pdGlhbCBtZXNzYWdl",
"contentTopic": "/messaging/2/api/utf8"
}
}
]
```
Error for `404`:
```http
HTTP/1.1 404 Not Found
Content-Type: application/json
{
"error": "No messages found for contentTopic '/messaging/2/api/utf8'"
}
```
##### By `subscriptionId`
Request:
```http
GET /messages?subscriptionId=1692854c-cbde-4afe-901f-1c86ecbd9ea1&skip=0&take=10 HTTP/1.1
Accept: application/json
```
Note: When omitting pagination parameters, the URL would be:
```http
GET /messages?subscriptionId=1692854c-cbde-4afe-901f-1c86ecbd9ea1 HTTP/1.1
```
Response:
```http
HTTP/1.1 200 OK
Content-Type: application/json
[
{
"sent": false,
"stored": false,
"received": true,
"message": {
"payload": "SW5pdGlhbCBtZXNzYWdl",
"contentTopic": "/messaging/2/api/utf8"
}
}
]
```
Error for `404`:
```http
HTTP/1.1 404 Not Found
Content-Type: application/json
{
"error": "No messages found for subscriptionId '1692854c-cbde-4afe-901f-1c86ecbd9ea1'"
}
```
### Health Indicator
The `Health Indicator API` SHOULD be used to monitor the health of a node operating under the `Messaging API`.
The specific criteria for determining the health state MAY vary depending on the protocols the node utilizes.
We define three health states:
- unhealthy;
- minimally healthy;
- healthy;
The `Health Indicator API` SHOULD be used to monitor the operational health of a node operating under the `Messaging API`.
The criteria for determining a node’s health state MAY vary depending on the protocols in use and the node’s configuration.
Detailed criteria for each health state are provided in the subsequent sections.
For the purposes of this specification, three health states are defined:
-`unhealthy`: Indicates that the node is in a degraded state and may not reliably process or transmit messages.
-`minimally healthy`: Indicates that the node meets the minimum operational requirements, although performance or reliability may be impacted.
-`healthy`: Indicates that the node is operating optimally, with full support for message processing and transmission.
Furthermore, any change in the node’s health state MUST trigger a `health:change` event via the `Event Source API` immediately after the state transition occurs.
Refer to `Event Source API` for more details.
#### Health states
##### `unhealthy`
Indicates that the node’s connectivity is insufficient for reliable operation.
In this state:
- No connections to service nodes are available, regardless of protocol.
- No peers are present in the [RELAY](https://github.com/vacp2p/rfc-index/blob/0277fd0c4dbd907dfb2f0c28b6cde94a335e1fae/waku/standards/core/11/relay.md) mesh.
- The node has at least one connection to a service node implementing [FILTER](https://github.com/vacp2p/rfc-index/blob/7b443c1aab627894e3f22f5adfbb93f4c4eac4f6/waku/standards/core/12/filter.md) and one connection to a service node implementing [LIGHTPUSH](../standards/core/lightpush.md).
- The [RELAY](https://github.com/vacp2p/rfc-index/blob/0277fd0c4dbd907dfb2f0c28b6cde94a335e1fae/waku/standards/core/11/relay.md) mesh includes connections to at least four other peers.
- The node has at least two connections to service nodes implementing [FILTER](https://github.com/vacp2p/rfc-index/blob/7b443c1aab627894e3f22f5adfbb93f4c4eac4f6/waku/standards/core/12/filter.md) and at least two connections to service nodes implementing [LIGHTPUSH](../standards/core/lightpush.md).
- The [RELAY](https://github.com/vacp2p/rfc-index/blob/0277fd0c4dbd907dfb2f0c28b6cde94a335e1fae/waku/standards/core/11/relay.md) mesh includes connections to at least six other peers.
For the `REST API`, long polling over the `Message Store API` SHOULD be used.
##### `message:error`
This event MUST be dispatched when an error occurs during the processing of a message whether while sending or receiving.
In such cases, the `error` property of the corresponding `MessageRecord` is populated with implementation-specific error information.
The event payload is the `MessageRecord` containing the error details.
Once a previously errored message is successfully transmitted, the error information MUST be cleared and an updated event (such as `message:sent` or `message:change`) should be dispatched.
This event MUST be dispatched when a message is confirmed to be `stored` in the underlying [STORE](../standards/core/store.md) protocol or via the `History API`.