Follow NFT holders
How to follow all Farcaster users who own a certain NFT
This guide demonstrates how to follow Farcaster 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 } 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);
const signer = process.env.NEYNAR_SIGNER;
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);
Then, we can use the followUser
endpoint to follow each user.
const result = await client.followUser(signer, fids);
console.log(result);
Example output:
{
"success": true,
"details": [
{
"success": true,
"target_fid": 132
},
{
"success": true,
"target_fid": 78
},
{
"success": true,
"target_fid": 4262
},
{
"success": true,
"target_fid": 3602
},
]
}
That's it! You can now follow users who own a specific NFT easily with the Neynar SDK.
Ready to start building?
Get your subscription at neynar.com and reach out to us on Telegram with any questions!
Updated 2 months ago