WebSocket API
Connect to the Blocknative service over WebSocket protocol

Creating a Socket Connection

A WebSocket connection should be created with the Blocknative service at wss://api.blocknative.com/v0. A successful connection will receive a response object that contains a status parameter with a value of 'ok'.

Required Parameters

The following parameters are required for all messages that are sent to the Blocknative service:
parameter
type
description
timeStamp
string
an ISO datetime string formatted YYYY-MM-DDThh:mm:ss.sTZD (see example for usage and https://www.w3.org/TR/NOTE-datetime for more details)
dappId
string
your API key
version
string
a version string usually used by the Blocknative SDK, but any version string can be used
blockchain
object
object specifying a the system and network of the blockchain (see next)
system
string
blockchain name, valid values are ethereum and bitcoin
network
string
blockchain network, valid values for support systems are:
Ethereum (and EVM compatible) - main, ropsten, rinkeby, goerli, kovan, xdai, bsc-main
Bitcoin - main, test
1
{
2
"timeStamp": "2021-01-11T06:21:40.197Z",
3
"dappId": "<YOUR_API_KEY>",
4
"version": "1",
5
"blockchain": {
6
"system": "ethereum"
7
"network": "main"
8
}
9
// ...rest of the message specific parameter
10
}
Copied!

Initialization

Once a connection has been created with the Blocknative WebSocket service, an initialization message must be sent before any other messages so that the API key can be validated.
You API Key is created/managed from the Mempool Explorer account management page at https://explorer.blocknative.com/account. Note that API Key is called dappId in the Blocknative websocket protocol.
1
{
2
"categoryCode": "initialize",
3
"eventCode": "checkDappId",
4
// ...rest of the required parameters
5
}
6
Copied!

Subscriptions

You can subscribe to receive transaction state change events by either watching a transaction hash/id or an address.

Ethereum Transaction Hash

1
// subscribe
2
{
3
"categoryCode": "activeTransaction",
4
"eventCode": "txSent",
5
"transaction": {
6
"hash": "0x0b4c94c414f71ddd5e7a625fcaa83ff1f93e9a7ca37e0f577b488ac8fd786655"
7
}
8
// ...rest of the required parameters
9
}
10
11
// unsubscribe
12
{
13
"categoryCode": "activeTransaction",
14
"eventCode": "unwatch",
15
"transaction": {
16
"hash": "0x0b4c94c414f71ddd5e7a625fcaa83ff1f93e9a7ca37e0f577b488ac8fd786655"
17
}
18
// ...rest of the required parameters
19
}
Copied!

Bitcoin Transaction Id

1
// subscribe
2
{
3
"categoryCode": "activeTransaction",
4
"eventCode": "txSent",
5
"transaction": {
6
"txid": "edbd572eb93f54099c0c64781e5fb09b8b4d89f80229b1af3c293d3356ca9f71"
7
}
8
// ...rest of the required parameters
9
}
10
11
// unsubscribe
12
{
13
"categoryCode": "activeTransaction",
14
"eventCode": "unwatch",
15
"transaction": {
16
"txid": "edbd572eb93f54099c0c64781e5fb09b8b4d89f80229b1af3c293d3356ca9f71"
17
}
18
// ...rest of the required parameters
19
}
Copied!

Address (Ethereum or Bitcoin)

1
// subscribe
2
{
3
"categoryCode": "accountAddress",
4
"eventCode": "watch",
5
"account": {
6
"address": "0xf5b5bd85157587771db6d49315b1712201569bdd"
7
}
8
// ...rest of the required parameters
9
}
10
11
// unsubscribe
12
{
13
"categoryCode": "accountAddress",
14
"eventCode": "unwatch",
15
"account": {
16
"address": "0xf5b5bd85157587771db6d49315b1712201569bdd"
17
}
18
// ...rest of the required parameters
19
}
Copied!

Configurations

A configuration can be sent to the service to filter and/or decode transactions.
Transaction decoding is only available for Ethereum transactions
1
{
2
"categoryCode": "configs",
3
"eventCode": "put",
4
"config": {
5
"scope": "0xf5b5bd85157587771db6d49315b1712201569bdd",
6
"filters": [{"contractCall.methodName":"transfer"}],
7
"abi": [{
8
"constant": false,
9
"inputs": [{ "name": "newOwner", "type": "address" }],
10
"name": "setOwner",
11
"outputs": [],
12
"payable": false,
13
"stateMutability": "nonpayable",
14
"type": "function"
15
}],
16
"watchAddress": true
17
}
18
// ...rest of the required parameters
19
}
Copied!
parameter
type
description
scope
string
A valid Ethereum address or 'global'
filters
string[]
An array of valid filters. The Blocknative service uses jsql, a JavaScript query language to filter events. Documentation for how to create filter queries can be found here
abi
string[]
A valid JSON ABI that will be used to decode input data for transactions that occur on the contract address defined in scope
watchAddress
boolean
defines whether the service should automatically watch the address as defined in scope
scope establishes the context in which a filter is applied. The global scope applies the filter to all transactions/addresses watched. Therefore a global scope only supports generic transaction elements. A scope set to an address can include filters, such as methodName,that are specific to the contract at that address. Similarly, the abi field only works for a contract specific scope.

Connection Drops

In the event of a WebSocket connection drop, no state is persisted between connections, so upon reconnection there will need to be logic defined that will:
    1.
    Send an initialization message
    2.
    Send all previously subscribed addresses, transactions and configs

Transaction Events

Each message sent to the service will receive an echo of that message back, this echo may be ignored for most purposes.
Every payload contains an eventCode parameter in the event object which indicates the type of event. Detailed documentation for event codes can be found in the here.

Ethereum Transaction Payload

1
{
2
"version":0,
3
"serverVersion":"0.85.8",
4
"timeStamp":"2021-01-06T01:25:24.716Z",
5
"connectionId":"49babf8c03a18ca8577b8dc42e4ab9e8cbdf3148d4b4b1e311f54f24c19e0e57",
6
"status":"ok",
7
"event":{
8
"timeStamp":"2021-01-06T01:25:24.716Z",
9
"categoryCode":"activeAddress",
10
"eventCode":"txConfirmed",
11
"dappId":"<DAPP ID>",
12
"blockchain":{
13
"system":"ethereum",
14
"network":"main"
15
},
16
"transaction":{
17
"status":"confirmed",
18
"monitorId":"GETH_1_F_PROD",
19
"monitorVersion":"0.77.3",
20
"timePending":"5482902",
21
"blocksPending":410,
22
"hash":"0x045fbad4090a25fc8267cf80f0f095a202cdac465d534e4c942b2068086e9aa3",
23
"from":"0xbA99926Be550865a956febAF4A747FFA0648797A",
24
"to":"0x111111125434b319222CdBf8C261674aDB56F3ae",
25
"value":"0",
26
"gas":324499,
27
"gasPrice":"61000000000",
28
"nonce":179,
29
"blockHash":"0x543bd57151cb7ab3024a18a459844790278861c712e9e4b58e1939a207e9fb82",
30
"blockNumber":11598068,
31
"transactionIndex":307,
32
"input": "0x34b0793b000000000000000...",
33
"gasUsed":"138022",
34
"asset":"ETH",
35
"watchedAddress":"0x111111125434b319222cdbf8c261674adb56f3ae",
36
"direction":"incoming",
37
"counterparty":"0xbA99926Be550865a956febAF4A747FFA0648797A"
38
}
39
}
40
}
41
Copied!

Address subscription payload additional properties

If the transaction event is linked to an address subscription it will have the following properties that are unique to address subscriptions:
parameter
type
description
watchedAddress
string
the address subscription that this transaction is linked to, allowing you to match the transaction to a subscription
direction
string
a value of incoming or outgoing indicating whether the watched address is sending or receiving the transaction
counterparty
string
the counter-party to the watched address

Confirmed/failed transaction payload additional properties

Transactions with a status of confirmed will have the following additional properties that are not included in other statuses:
parameter
type
description
timePending
string
the time, in milliseconds, the transaction was pending before being included in a block
blocksPending
string
the number of blocks the transaction was pending before being included in a block
blockHash
string
the hash of the block containing the confirmed (or failed) transaction
blockNumber
number
the number of the block containing the confirmed (or failed) transaction
gasUsed
string
the amount of gas used to confirm (or fail) the transaction

Replaced transactions payload additional properties

Transactions that have been replaced and have a status of speedup or cancel will have the following additional properties that are not included in other statuses:
parameter
type
description
originalHash
string
the hash of the transaction that is being replaced

Bitcoin transaction payload

1
{
2
"version":0,
3
"serverVersion":"0.85.8",
4
"timeStamp":"2021-01-06T01:25:24.716Z",
5
"connectionId":"49babf8c03a18ca8577b8dc42e4ab9e8cbdf3148d4b4b1e311f54f24c19e0e57",
6
"status":"ok",
7
"event":{
8
"timeStamp":"2021-01-06T01:25:24.716Z",
9
"categoryCode":"activeAddress",
10
"eventCode":"txConfirmed",
11
"dappId":"<DAPP ID>",
12
"blockchain":{
13
"system":"ethereum",
14
"network":"main"
15
},
16
"transaction":{
17
txid: "5d7bab0b34eb623fb344264a9801c8655bbee85f7e55f2c6fcca813487d57164"
18
inputs: [
19
{address: "13kj3N13dhTLJcKfxs8KchbDN3pQBKcWLK", value: "0.0016"}
20
]
21
outputs: [
22
{address: "3LGrysb8jWcvdzQaZCVYoPbgJ1Puf2FmnJ", value: "0.0001524"},
23
{address: "1BABaiprMpm3Lmm3zuMhQs5gaGtjjguVBA", value: "0.00123649"}
24
],
25
netBalanceChanges: [
26
{address: "13kj3N13dhTLJcKfxs8KchbDN3pQBKcWLK", delta: "-0.0016"},
27
{address: "3LGrysb8jWcvdzQaZCVYoPbgJ1Puf2FmnJ", delta: "0.0001524"},
28
{address: "1BABaiprMpm3Lmm3zuMhQs5gaGtjjguVBA", delta: "0.00123649"}
29
]
30
fee: "0.00021111"
31
status: "confirmed",
32
system: "bitcoin",
33
network: "main",
34
timePending: "542538",
35
timeStamp: "2021-01-07T01:03:34.521Z",
36
txid: "5d7bab0b34eb623fb344264a9801c8655bbee85f7e55f2c6fcca813487d57164",
37
watchedAddress: "13kj3N13dhTLJcKfxs8KchbDN3pQBKcWLK",
38
rawTransaction: <RAW BITCOIN TRANSACTION>,
39
timePending: "542538",
40
blockHeight: "664885",
41
blockHash: "5d7bab0b34eb623fb344264a9801c8655bbee85f7e55f2c6fcca813487d57164",
42
}
43
}
44
}
Copied!

Errors

Errors from the service are returned as messages with a status = 'error' and a reason string explaining the error.
Websocket payload is too large (60,000 bytes):
1
{
2
//...other params
3
status: 'error',
4
reason: 'message too large'
5
}
Copied!
API key (dappId) parameter is missing:
1
{
2
//...other params
3
status: 'error',
4
reason: 'missing dappId'
5
}
Copied!
API key (dappId) is invalid:
1
{
2
//...other params
3
status: 'error',
4
reason: '<DAPP-ID> is not a valid API key'
5
}
Copied!
Attempt to watch an invalid address:
Address validity is based on the specific blockchain system used (bitcoin or ethereum).
1
{
2
//...other params
3
"status":"error",
4
"reason":"(string `address`) 0 is an invalid address in object `account` in object `websocket-event`"
5
}
Copied!
Amount of messages has exceeded limit, see Rate Limits section below.
1
{
2
//...other params
3
status: 'error',
4
reason: 'ratelimit',
5
retryMs: 1000,
6
blockedMsg: <Message that was blocked>
7
}
Copied!
Exceeded daily event limit for your API key, see Rate Limits section below.
1
{
2
//...other params
3
status: 'error',
4
reason: 'You have reached your event rate limit for today. See account.blocknative.com for details.'
5
}
Copied!
IP has been rate limited, see Rate Limits section below.
1
{
2
//...other params
3
status: 'error',
4
reason: 'IP ratelimited**'**
5
}
Copied!
Error received for using incorrect API version.
1
{
2
//...other params
3
status: 'error',
4
message: 'api version not supported',
5
}
Copied!

Rate Limits

Daily per API key event rate limits that apply to Webhook events also apply to WebSocket events. Details on those limits can be found here.
The Blocknative service also limits how many messages can be sent over a duration of time, limiting to 50 messages per second.
A strategy that the Blocknative SDK uses to handle these limits is to create a message queue that processes messages and ensures that messages are spaced adequately. This can be especially important to implement when re-sending addresses to be subscribed after a connection dropout.
If a rate limit error message is received you can use the retryMs parameter to wait for that time period to continue sending messages. If a message was blocked due to a rate limit it will be included in the payload in the blockedMsg parameter so you can re-send that message after the retry delay, ensuring that no messages are dropped.
Last modified 5mo ago