đŸ’ŦCasts

Learn how to fetch Farcaster casts data using Airstack Hubs API.

Get Cast By FID and Hash

You can get a cast by a specific FID and cast hash 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);

    const castHashHex = 'd2b1ddc6c88e865a33cb1a565e0058d757042974';
    const castHashBytes = hexStringToBytes(castHashHex)._unsafeUnwrap(); // Safety: castHashHex is known and can't error
    
    // Fetch cast data with `getCast`
    const castResult = await client.getCast({ fid: 2, hash: castHashBytes });
    console.log(castResult.value);
    // After everything, close the RPC connection
    client.close();
  }
});

Get Casts Authored By FID

You can get all the casts authored 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 casts data with `getCastsByFid`
    const castsResult = await client.getCastsByFid({ fid: 2 }, metadata);
    console.log(castsResult.value);
    // After everything, close the RPC connection
    client.close();
  }
});

Get Cast That Mention FID

You can get all the casts that mentioned 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 casts data with `getCastsByMention`
    const castsResult = await client.getCastsByMention({ fid: 2 }, metadata);
    console.log(castsResult.value);
    // After everything, close the RPC connection
    client.close();
  }
});

Get Cast By Parent Cast Or URL

You can get all casts by parent cast's FID and Hash OR by the parent's URL 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);

    const castHashHex = 'a48dd46161d8e57725f5e26e34ec19c13ff7f3b9';
    const castHashBytes = hexStringToBytes(castHashHex)._unsafeUnwrap(); // Safety: castHashHex is known

    // Fetch casts data with `getCastsByParent`
    const castsResult = await client.getCastsByParent({ parentCastId: { fid: 2, hash: castHashBytes } });
    console.log(castsResult.value);
    // After everything, close the RPC connection
    client.close();
  }
});

Developer Support

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

More Resources

Last updated