DocumentationBlog

@apibara/protocol

This package contains the client used to stream data from Apibara.

Example#

This example shows how to connect to the StarkNet Goerli stream and print all messages received. To learn more how you can parse the StarkNet-specific data, look at the @apibara/starknet package documentation.

import { NodeClient, credentials } from '@apibara/protocol'
async function main() {
const node = new NodeClient('goerli.starknet.stream.apibara.com:443', credentials.createSsl())
const messages = node.streamMessages({})
// return a promise that resolves only when the
// stream finishes or it receives an error.
return new Promise((resolve, reject) => {
messages.on('end', resolve)
messages.on('error', reject)
messages.on('data', (data) => {
// handle message from server.
console.log(data)
})
})
}

NodeClient#

The NodeClient connects to the Apibara gRPC server and streams data from it.

constructor#

constructor(
address: string,
credentials: ChannelCredentials,
options?: ClientOptions
)

Creates a new NodeClient.

Parameters

  • address: the stream address, for example goerli.starknet.stream.apibara.com:443.
  • credentials: credentials used to connect to the server. Usually credentials.createSsl().
  • options: gRPC client options. See @grpc/grpc-js for a list of options.

streamMessages#

public streamMessages(
{ startingSequence }: StreamMessagesRequest
): ClientReadableStream<StreamMessagesResponse__Output>

Start streaming messages from Apibara.

Parameters

  • startingSequence: the sequence number of the first block to stream. Use this to resume streaming after disconnect.

status#

async status(): Promise<StatusResponse__Output | undefined>

Returns the indexer status. This is used to check the latest block indexed by the stream.