Common subscriptions of 2 users

Finding Hypersub subscriptions on Social Token Protocol (STP) using Neynar

In this guide, we'll take two FIDs and then find their common subscriptions on fabric.

We'll use JavaScript for this guide, but the same logic would work for any other language you use!

So, let's get started by creating a new file and defining our constants:

const fid1 = 194;
const fid2 = 191;
const url1 = `https://api.neynar.com/v2/farcaster/user/subscribed_to?fid=${fid1}&viewer_fid=3&subscription_provider=fabric_stp`;
const url2 = `https://api.neynar.com/v2/farcaster/user/subscribed_to?fid=${fid2}&viewer_fid=3&subscription_provider=fabric_stp`;

You can replace the FIDs with the ones you want to check the subscriptions for and leave the URLs as they are. The URL is the API route to get all the channels a user is subscribed to. You can find more info about the API route in the API reference.

Then, call the APIs using fetch like this:

const fetchUrls = async () => {
  const options = {
    method: "GET",
    headers: { accept: "application/json", api_key: "NEYNAR_API_DOCS" },
  };

  const response = await Promise.all([
    fetch(url1, options),
    fetch(url2, options),
  ]);
  const data = await Promise.all(response.map((res) => res.json()));
  return data;
};

Here, make sure to replace the API key with your API key instead of the docs API key in production.

Finally, let's filter out the data to find the common subscriptions like this:

fetchUrls().then((data) => {
  const [subscribedTo1, subscribedTo2] = data;
  const commonSubscriptions = subscribedTo1.subscribed_to.filter(
    (item1: { contract_address: string }) =>
      subscribedTo2.subscribed_to.some(
        (item2: { contract_address: string }) =>
          item2.contract_address === item1.contract_address
      )
  );
  console.log(commonSubscriptions);
});

Here, we use the filter function on the data that we just fetched and match the channel's contract address since that will be unique for every channel.

Now, we can test the script by running it.

The two FIDs we used were subscribed to Terminally Onchain, so that shows up.

If you want to look at the complete script, you can look at this GitHub Gist. If you want to know more about the subscription APIs take a look here.

Lastly, please share what you built with us on Farcaster by tagging @neynar, and if you have any questions, reach out to us on warpcast or Telegram!