Farcaster feed of NFT owners

Make a Farcaster feed showing casts from a specific set of users

This guide demonstrates how to make a feed of Farcaster casts from users who own a specific NFT.

Check out this Getting started guide to learn how to set up your environment and get an API key.

Before all that, initialize Neynar client:

// npm i @neynar/nodejs-sdk
import { NeynarAPIClient, FeedType, FilterType } from "@neynar/nodejs-sdk";

// make sure to set your NEYNAR_API_KEY .env
// don't have an API key yet? get one at neynar.com
const client = new NeynarAPIClient(process.env.NEYNAR_API_KEY);

First, we need to get the addresses owning Milady. We can use the Alchemy NFT API to get the addresses of users who own the NFT.

const getAddr = async (nftAddr: string): Promise<string[]> => {
  const apiKey = process.env.ALCHEMY_API_KEY;
  const baseUrl = `https://eth-mainnet.g.alchemy.com/nft/v3/${apiKey}/getOwnersForContract?`;
  const url = `${baseUrl}contractAddress=${nftAddr}&withTokenBalances=false`;

  const result = await fetch(url, {
    headers: { accept: "application/json" },
  });
  const data = await result.json();
  return data.owners;
};

// milady maker contract address
const nftAddr = "0x5af0d9827e0c53e4799bb226655a1de152a425a5";
const addrs = await getAddr(nftAddr);

Next, get Farcaster FIDs of each address, then filter out any undefined values.

const fidLookup = async (addrs: string[]) => {
  const fids = await Promise.all(
    addrs.map(async (addr) => {
      try {
        const response = await client.lookupUserByVerification(addr);
        return response ? response.result.user.fid : undefined;
      } catch (error) {
        return undefined;
      }
    })
  );
  return fids.filter((fid) => fid !== undefined);
};

const fids = await fidLookup(addrs);

Lastly, fetch the feed using the FIDs.

const feed = await client.fetchFeed(FeedType.Filter, {
  filterType: FilterType.Fids,
  fids
});

console.log(feed);

Example output:

{
  casts: [
    {
      object: "cast_hydrated",
      hash: "0x4b02b1ef6daa9fe111d3ce871ec004936f19b979",
      thread_hash: "0x4b02b1ef6daa9fe111d3ce871ec004936f19b979",
      parent_hash: null,
      parent_url: "https://veryinter.net/person",
      parent_author: [Object ...],
      author: [Object ...],
      text: "What'd you buy for Black Friday / Cyber Monday?\n\nI got a new webcam and bought a Roomba+mop that I'm excited to fiddle with.",
      timestamp: "2023-11-27T14:46:01.000Z",
      embeds: [],
      reactions: [Object ...],
      replies: [Object ...],
      mentioned_profiles: []
    }, {
      object: "cast_hydrated",
      hash: "0xf1210d9eb6b21bbf3847ca5983539ed9c2baee13",
      thread_hash: "0xf1210d9eb6b21bbf3847ca5983539ed9c2baee13",
      parent_hash: null,
      parent_url: null,
      parent_author: [Object ...],
      author: [Object ...],
      text: "Great couple days mostly off the internet. 🦃🤗\n\nAlso excited to be back in the mix.\n\nWhat will be the biggest stories to end the year?",
      timestamp: "2023-11-27T14:44:19.000Z",
      embeds: [],
      reactions: [Object ...],
      replies: [Object ...],
      mentioned_profiles: []
    }, {
      object: "cast_hydrated",
      hash: "0x7d3ad4be401c0050cf20a060ebbd108383b6357c",
      thread_hash: "0x7d3ad4be401c0050cf20a060ebbd108383b6357c",
      parent_hash: null,
      parent_url: "https://foundation.app",
      parent_author: [Object ...],
      author: [Object ...],
      text: "Consisting of 50 1/1 works, Ver Clausi's new drop Blaamius imagines life after the Anthropocene. His rich, colorful illustrations that meld subject and scenery remind me of old sci-fi comics and H.R. Giger in the best possible way. \nPrice: 0.025\nhttps://foundation.app/collection/bla-cebc",
      timestamp: "2023-11-27T14:29:37.000Z",
      embeds: [
        [Object ...]
      ],
      reactions: [Object ...],
      replies: [Object ...],
      mentioned_profiles: []
    }
  ],
  next: {
    cursor: "eyJ0aW1lc3RhbXAiOiIyMDIzLTExLTI3IDE0OjI5OjM3LjAwMDAwMDAifQ=="
  }
}

Farcaster feed of Milady owners!

🚀

Ready to start building?

Get your subscription at neynar.com and reach out to us on Telegram with any questions!