Fetch mutual follow/followers in Farcaster

Find mutual follows with another Farcaster user

On X (Twitter) profile page, there is a "Followed by A, B, C, and 10 others you follow". This guide demonstrates how to use the Neynar SDK to make the same thing but for Farcaster.

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

First, initialize the client:

// npm i @neynar/nodejs-sdk
import { NeynarAPIClient } 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);

Say we want to get people @rish follows that also follows @manan. This is useful if we want to mutual connections between two users. We'll fetch @rish's followings first.

const fetchAllFollowing = async (fid: number) => {
  let cursor: string | null = "";
  let users: unknown[] = [];
  do {
    const result = await client.fetchUserFollowing(fid, {
      limit: 150,
      cursor,
    });
    users = users.concat(result.result.users);
    cursor = result.result.next.cursor;
    console.log(cursor);
  } while (cursor !== "" && cursor !== null);

  return users;
};

const rishFID = 194;
const rishFollowings = await fetchAllFollowing(rishFID);

Then we'll fetch @manan's followers.

const fetchAllFollowers = async (fid: number) => {
  let cursor: string | null = "";
  let users: unknown[] = [];
  do {
    const result = await client.fetchUserFollowers(fid, {
      limit: 150,
      cursor,
    });
    users = users.concat(result.result.users);
    cursor = result.result.next.cursor;
    console.log(cursor);
  } while (cursor !== "" && cursor !== null);

  return users;
};

const mananFID = 191;
const mananFollowers = await fetchAllFollowers(mananFID);

Think of these two arrays as sets. We want to find the intersection of these two sets. We can use the fid property to find the intersection.

const mutualFollowings = rishFollowings.filter((following) =>
  mananFollowers.some((follower) => follower.fid === following.fid)
);

console.log(mutualFollowings);

Example output:

[
  {
    fid: 6227,
    custodyAddress: "0x35b92ea9c3819766ec1fff8ddecec69028b0ac42",
    username: "ekinci.eth",
    displayName: "Emre Ekinci ~ q/dau",
    pfp: {
      url: "https://i.imgur.com/smbrNPw.jpg"
    },
    profile: {
      bio: [Object ...]
    },
    followerCount: 670,
    followingCount: 660,
    verifications: [ "0x5f57c686bdbc03242c8fa723b80f0a6cdea79546"
    ],
    activeStatus: "active",
    timestamp: "2023-11-14T04:13:11.000Z"
  }, {
    fid: 280,
    custodyAddress: "0xd05d60b5762728466b43dd94ba882d050b60af67",
    username: "vrypan.eth",
    displayName: "vrypan.eth",
    pfp: {
      url: "https://i.imgur.com/jmXEW3I.png"
    },
    profile: {
      bio: [Object ...]
    },
    followerCount: 1296,
    followingCount: 493,
    verifications: [ "0x8b0573d1c80362db589eda39c2e30f5190d7eb51",
      "0x93c620d2af377c6c37e3e3c1d3e065eb04b08ae2"
    ],
    activeStatus: "active",
    timestamp: "2023-11-14T01:37:40.000Z"
  }
  // ...
]

Note: you'd probably want to cache the results of fetchAllFollowing and fetchAllFollowers so you don't have to make the same API calls again.

That's it! You can use this to make a "Followed by A, B, C, and 10 others you follow" info in your Farcaster app.

πŸš€

Ready to start building?

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