👍Reactions

Learn how to fetch Reactions data using Airstack Hubs API.

Write Reactions

You can create a new reactions on an existing cast to the Hub by using Airstack Hubs API with the code below:

For this, you will first need to create your own signer to call the submit message API.

import {
  hexStringToBytes,
  getSSLHubRpcClient,
  Metadata,
  makeCastAdd,
  FarcasterNetwork,
} from "@farcaster/hub-nodejs";
import {
  ReactionType,
  makeReactionAdd,
} from "@farcaster/core";
import { config } from "dotenv";

config();

const SIGNER_PRIVATE_KEY = '0x...'; // Your signer's private key
const FID = 1; // Your fid
const ed25519Signer = new NobleEd25519Signer(SIGNER_PRIVATE_KEY);
const dataOptions = {
  fid: FID,
  network: FC_NETWORK,
};
const FC_NETWORK = FarcasterNetwork.MAINNET;

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

const castHashHex = "c830f66f4f7beed1516126897b61102ae48c0626"; // Cast to fetch reactions for, remove 0x
const castHashBytes = hexStringToBytes(castHashHex)._unsafeUnwrap(); // Safety: castHashHex is known and can't error

// Construct the reaction
const reactionAdd = await makeReactionAdd(
  {
    type: ReactionType.LIKE,
    targetCastId: {
      fid: 602, // caster FID
      hash: castHashBytes,
    },
  },
  dataOptions,
  ed25519Signer
);

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);

    if (reactionAdd.isOk()) {
      // Broadcast the reactions to the Farcaster network
      const submitResult = await client.submitMessage(
        reactionAdd.value,
        metadata
      );
      if (submitResult.isOk()) {
        console.log(`Reply posted successfully`);
      } else {
        console.error(`Error posting reply: ${submitResult.error}`);
      }
    } else {
      const error = reactionAdd.error;
      // Handle the error case
      console.error(`Error posting reply: ${error}`);
    }
    // After everything, close the RPC connection
    client.close();
  }
});

Get Reactions By FID and Target Cast

You can get a reaction by its created FID and target cast by using Airstack Hubs API with the code below:

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

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 reaction data with `getReaction`
    const reactionsResult = await client.getReaction(
      {
        fid: 8150,
        reactionType: ReactionType.LIKE,
        targetCastId: {
          fid: 2,
          hash: castHashBytes,
        },
      },
      metadata,
    );
    
    if (reactionsResult.isOk()) {
      console.log(reactionsResult.value);
    } else {
      console.error("No reactions found: reactionsResult.error);
    }
    // After everything, close the RPC connection
    client.close();
  }
});

Get Reactions By FID

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

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

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 reactions data with `getReactionsByCast`
    const reactionsResult = await client.getReactionsByCast(
      {
        reactionType: ReactionType.LIKE,
        targetCastId: {
          fid: 2,
          hash: castHashBytes,
        },
      },
      metadata,
    );
    console.log(reactionsResult.value);
    // After everything, close the RPC connection
    client.close();
  }
});

Get Reactions By Cast

You can get all reactions by a specific cast hash by using Airstack Hubs API with the code below:

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

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 reaction data with `getReactionsByFid`
    const reactionsResult = await client.getReactionsByFid(
      { fid: 2, reactionType: ReactionType.LIKE },
      metadata,
    );
    console.log(reactionsResult.value);
    // After everything, close the RPC connection
    client.close();
  }
});

Get Reactions By Target Cast URL

You can get all reactions by a specific cast's target URL by using Airstack Hubs API with the code below:

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

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 reaction data with `getReactionsByTarget`
    const reactionsRes = await client.getReactionsByTarget(
      {
        targetUrl:
          "chain://eip155:1/erc721:0x39d89b649ffa044383333d297e325d42d31329b2",
        reactionType: ReactionType.LIKE,
      },
      metadata,
    );
    console.log(reactionsRes.value);
    // After everything, close the RPC connection
    client.close();
  }
});

Developer Support

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

More Resources

Last updated