How to get followers of a channel through Farcaster API and airdrop an NFT to their wallets

If you read this too long and not so SEO friendly title I have selected for this post, you would probably think that there are already services that do that in a really user-friendly and easy way, but that is not fun, doing things your way (the hard way) is always more fun, so let's dig in.

The first thing we need is the farcaster API, which you can find its documentation here. The API endpoint we need is called Get Channel Followers and has this URL:

​https://api.warpcast.com/v1/channel-followers?channelId=books

This endpoint has a parameter called channelD which is the name of the channel (the text after the / in the channel URL) and returns an array of FIDs and follow time and another array of fid which is going to be removed soon and is redundant.

This API is different from the one we used in the last post, this one has a limit in each call and only returns 100 results, and needs pagination to get the next results. There are lots of ways to get this done, like some add-ons on Google Sheets. basically, you need to loop the API some how to get all the results you need. I have taught R in the past few years and R is my goto tool for Data stuff so I fire up some packages in R studio and take a look at the API:

library(jsonlite) 
library(httr)      
library(stringr)
res <- GET(url = 'https://api.warpcast.com/v1/channel-followers?channelId=success')
resDF <- fromJSON(httr::content(res, as = "text"))

These lines of code, call the API (notice that I have put success as the name of the channel, which is one of the best farcaster channels right now vibe-vise), and with fromJSON function, we get the content of the result of the call as text:

In the picture above, you can see that we have a list of 100 FIDs, 100 timestamps (It's in a format called UNIX time, more on that later) and a pointer to the next set of results which here is called cursor.

So we have everything we need, we get the first 100, get the first cursor and put those in a while loop that calls the API until the cursor returns empty or NULL and meanwhile we paste the results together in a tidy table:

library(jsonlite) 
library(httr)      
library(stringr)

res <- GET(url = 'https://api.warpcast.com/v1/channel-followers?channelId=success')
resDF <- fromJSON(httr::content(res, as = "text"))
curs = resDF[["next"]][["cursor"]] 

res.t <- as.data.frame(resDF$result)

while (!is.null(resDF[["next"]][["cursor"]])) {
  curs = resDF[["next"]][["cursor"]]
  res <- GET(url = 'https://api.warpcast.com/v1/channel-followers?channelId=success', query = list(cursor =curs))
  resDF <- fromJSON(httr::content(res, as = "text"))
  res.t <- rbind(res.t ,as.data.frame(resDF$result))
}

This script returns a table with 1038 rows which is consistent with what Warpcast is showing as the number of followers of the success channel:

The fids column is not needed, we remove that, and also we convert the followedAt to something human readable:

res.t <- data.frame(res.t$users.fid , as.Date(as.POSIXct(res.t$users.followedAt, origin="1970-01-01"))  )
colnames(res.t) <- c('fid' , 'follow_date')

Now that we have everything we need, we can save our table as a csv file:

write.csv(res.t , file = "succsess_snapshot_6_11_2024.csv")

In the next step, we open dune.com, click on create, and then upload a dataset and upload our data into dune. the next is really simple and you can read about these queries I'm using here in my previous posts. we just select fid, fname, and address from Farcaster database on Dune with the condition that the fid is present in the dataset we just uploaded:

select fid, fname, verified_addresses from dune.neynar.dataset_farcaster_profile_with_addresses where fid in (select fid from dune.mimg1.dataset_succsess_snapshot_6_11_2024)

and we export that to Google Sheets via dune API as I explained here:

This is the end of part one. In part two we are going to design an NFT and take a look at a few options to airdop it to our list.

Loading...
highlight
Collect this post to permanently own it.
Subscribe to Tinkering Onchain and never miss a post.