Start building on Farcaster with Neynar
Farcaster is a protocol for building decentralized social apps. Neynar makes it easy to build on Farcaster.
Basic understanding of Farcaster
Farcaster is a decentralized social protocol. Here are three short bullets on the primary Farcaster primitives that will be helpful to keep in mind as you dive in:
- User - every user on Farcaster is represented by a permanent FID that is the numerical identifier for the user. All user profile data for this FID e.g. username, display name, bio, etc. are stored on Farcaster protocol and mapped to this FID.
- Casts - users can broadcast information to the protocol in units of information called "casts". It's somewhat similar to a tweet on Twitter/X. Each cast has a unique "hash".
- Followers / following - users can follow each other to see casts from them. This creates a social graph for each user on Farcaster.
There's obviously more to this but let's start with this. All the above data is open and decentralized, available on Farcaster nodes called hubs. Neynar makes interfacing with this data relatively trivial.
In this tutorial, we will learn how use the above primitives to fetch a simple feed of casts for a given user.
Get Neynar API key
Don't have an API key yet? Register now at neynar.com.
- Click "Subscribe now."
- You will be redirected to a Stripe checkout page.
- Upon successful payment, we'll send you an email. Once the email arrives, you'll be able to sign in to the Developer Portal
Don't hesitate to reach out to us on our channel or Telegram with any questions!
Set up Neynar SDK
This section only needs to be done once when setting up the SDK for the first time.
To install the Neynar TypeScript SDK:
yarn add @neynar/nodejs-sdk
npm install @neynar/nodejs-sdk
pnpm install @neynar/nodejs-sdk
bun add @neynar/nodejs-sdk
To get started, initialize the client in a file named index.ts
:
import { NeynarAPIClient } from "@neynar/nodejs-sdk";
// make sure to set your NEYNAR_API_KEY .env
// for testing purposes, you can insert your key as a string param into NeynarAPIClient
const client = new NeynarAPIClient(process.env.NEYNAR_API_KEY);
Depending on your build environment, you might also need the following two steps:
- check the
type
field in package.json. Since we're using ES6 modules, you may need to set it to "module".
{
"scripts": {
"start": "node --loader ts-node/esm index.ts"
},
"type": "module", // <-- set to module if needed
"dependencies": {
// this is for illustration purposes, the version numbers will depend on when you do this tutorial
"@neynar/nodejs-sdk": "^1.71.0",
"ts-node": "^10.9.2",
"typescript": "^5.6.3"
}
}
- If you hit errors, try adding a
tsconfig.json
file in the directory to help with typescript compilation
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "node",
"target": "ESNext",
"esModuleInterop": true,
"skipLibCheck": true
},
"ts-node": {
"esm": true
}
}
Your directory should have the following:
- node_modules
- index.ts
- package-lock.json
- package.json
- tsconfig.json (optional)
- yarn.lock
Fetch Farcaster data using Neynar SDK
Fetching feed
To fetch the feed for a user, you need to know who the user is following and then fetch casts from those users. Neynar abstracts away all this complexity. Simply put in the fid
of the user in the fetchFeed
function and get a feed in response.
In this example, we will fetch the feed for Dan Romero . This is the feed Dan would see if he were to log into a client that showed a feed from people he followed in a reverse chronological order.
import { NeynarAPIClient, FeedType } from "@neynar/nodejs-sdk";
const client = new NeynarAPIClient(process.env.NEYNAR_API_KEY);
// fetch feed of Dan Romero: fid 3
async function fetchFollowingFeed() {
const feed = await client.fetchFeed(FeedType.Following, {
fid: 3,
});
console.log('User Feed:', feed);
}
fetchFollowingFeed();
You can now run this code by opening up this folder in the terminal and running
yarn start
Depending on your machine, typescript might take a few seconds to compile. Once done, it should print the output to your console. Something like below:
User Feed: {
casts: [
{
object: 'cast',
hash: '0x5300d6bd8f604c0b5fe7d573e02bb1489362f4d3',
author: [Object],
thread_hash: '0x5300d6bd8f604c0b5fe7d573e02bb1489362f4d3',
parent_hash: null,
parent_url: null,
root_parent_url: null,
parent_author: [Object],
text: 'https://open.spotify.com/track/5oQcOu1omDykbIPSdSQQNJ?si=2qMjk-fESMmxqCoAxTsPmw',
timestamp: '2024-11-14T04:57:23.000Z',
embeds: [Array],
channel: null,
reactions: [Object],
replies: [Object],
mentioned_profiles: [],
viewer_context: [Object]
},
]
}
You've successfully fetched the feed for a user in a simple function call!
Future reading: you can fetch many different kind of feeds. See Feed APIs.
Fetching user profile data
Now let's fetch data about a user. Remember users are represented by FIDs? We will take an FID and fetch data for that user. Here's how to do it using the SDK:
import { NeynarAPIClient, FeedType } from "@neynar/nodejs-sdk";
const client = new NeynarAPIClient(process.env.NEYNAR_API_KEY);
// fetch feed of Dan Romero: fid 3
async function fetchUser() {
const user = await client.fetchBulkUsers([3]);
console.log('User:', user);
}
fetchUser();
You can run this in your terminal similar to above by typing in:
yarn start
It should show you a response like below:
User: {
users: [
{
object: 'user',
fid: 3,
username: 'dwr.eth',
display_name: 'Dan Romero',
pfp_url: 'https://imagedelivery.net/BXluQx4ige9GuW0Ia56BHw/bc698287-5adc-4cc5-a503-de16963ed900/original',
custody_address: '0x6b0bda3f2ffed5efc83fa8c024acff1dd45793f1',
profile: [Object],
follower_count: 489109,
following_count: 3485,
verifications: [Array],
verified_addresses: [Object],
verified_accounts: [Array],
power_badge: true
}
]
}
Future reading: you can also fetch data about a user by using their wallet address or username as identifiers. See APIs for that here: User by wallet address, By username.
You're ready to build!
Now that you're able to fetch user and cast data, you're ready to dive in further and start making your first Farcaster application. We have numerous guides available here and our full API reference is here .
If you have questions or feedback, please reach out to rish on Farcaster or Telegram .