⛓️Onchain Events

Learn how to fetch Hub events data using Airstack Hubs API.

Get Onchain Events By FID

You can get all Farcaster onchain events by a specific FID by using Airstack Hubs API with the code below:

import {
  Metadata,
  getSSLHubRpcClient,
  OnChainEventType,
} from "@farcaster/hub-nodejs";
import { config } from "dotenv";

config();

const client = getSSLHubRpcClient("hubs-grpc.airstack.xyz");

client.$.waitForReady(Date.now() + 5000, async (e) => {
  if (e) {
    console.error(`Failed to connect to the gRPC server:`, e);
    process.exit(1);
  } else {
    console.log(`Connected to the gRPC server`);
    const metadata = new Metadata();
    // Provide API key here
    metadata.add("x-airstack-hubs", process.env.AIRSTACK_API_KEY as string);

    // Fetch onchain event data with `getOnChainEvents`
    const eventRes = await client.getOnChainEvents(
      {
        fid: 3,
        eventType: OnChainEventType.EVENT_TYPE_SIGNER,
      },
      metadata
    );
    console.log(eventRes.value);
    // After everything, close the RPC connection
    client.close();
  }
});

Get Onchain Signers By FID

You can get all signers by a specific FID by using Airstack Hubs API with the code below:

import {
  Metadata,
  getSSLHubRpcClient,
} from "@farcaster/hub-nodejs";
import { config } from "dotenv";

config();

const client = getSSLHubRpcClient("hubs-grpc.airstack.xyz");

client.$.waitForReady(Date.now() + 5000, async (e) => {
  if (e) {
    console.error(`Failed to connect to the gRPC server:`, e);
    process.exit(1);
  } else {
    console.log(`Connected to the gRPC server`);
    const metadata = new Metadata();
    // Provide API key here
    metadata.add("x-airstack-hubs", process.env.AIRSTACK_API_KEY as string);

    // Fetch onchain event data with `getOnChainSignersByFid`
    const eventRes = await client.getOnChainSignersByFid(
      {
        fid: 6833,
      },
      metadata
    );
    console.log(eventRes.value);
    // After everything, close the RPC connection
    client.close();
  }
});

Get Onchain Id Registry Event By Address

You can get all onchain events for a given address by using Airstack Hubs API with the code below:

import {
  Metadata,
  getSSLHubRpcClient,
} from "@farcaster/hub-nodejs";
import { config } from "dotenv";

config();

const client = getSSLHubRpcClient("hubs-grpc.airstack.xyz");

client.$.waitForReady(Date.now() + 5000, async (e) => {
  if (e) {
    console.error(`Failed to connect to the gRPC server:`, e);
    process.exit(1);
  } else {
    console.log(`Connected to the gRPC server`);
    const metadata = new Metadata();
    // Provide API key here
    metadata.add("x-airstack-hubs", process.env.AIRSTACK_API_KEY as string);

    const addressHex = "0xbfe1716f8d0fd949ca399a8a8a98b80d17e44372";
    const addressBytes = hexStringToBytes(addressHex)._unsafeUnwrap();

    // Fetch onchain event data with `getOnChainSignersByFid`
    const eventRes = await client.getIdRegistryOnChainEventByAddress(
      {
        address: addressBytes,
      },
      metadata
    );
    console.log(eventRes.value);
    // After everything, close the RPC connection
    client.close();
  }
});

Developer Support

If you have any questions or need help regarding integrating Farcaster onchain events data using AIrstack Hubs API into your Farcaster app, please join our Airstack's Telegram group.

More Resources

Last updated