- If you want to integrate Farcaster auth for your users, easiest way to start is Sign in with Neynar (Neynar pays all onchain fee)
- If you want dedicated signers for your user or bot, simplest to clone this example app for quickstart
This guide demonstrates how to like or recast a cast with the Neynar SDK.
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, Configuration } 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 config = new Configuration({
apiKey:process.env.NEYNAR_API_KEY,
});
const client = new NeynarAPIClient(config);
const signer = process.env.NEYNAR_SIGNER;
Then, like a cast:
const hash = "0x6932a9256f34e18892d498abb6d00ccf9f1c50d6";
client.publishReaction({ signerUuid: signer, reactionType: "like", target:hash });
Recasting works the same way, replace “like” with “recast”:
const hash = "0x6932a9256f34e18892d498abb6d00ccf9f1c50d6";
client.publishReaction({ signerUuid: signer, reactionType: "like", target:hash });
The response status code should return a 200 status code.
To verify that the reaction was published, you can fetch the cast’s reactions:
const types = ReactionsType.All;
const reactions = await client.fetchCastReactions({ hash, types: [types] });
console.log(reactions);
Which would print out
{
"result": {
"casts": [
{
"type": "like",
"hash": "0x691fabb3fc58bd4022d4358b2bc4f44469ad959a",
"reactor": {
"fid": "4640",
"username": "picture",
"displayName": "Picture",
"pfp": {
"url": "https://lh3.googleusercontent.com/erYudyT5dg9E_esk8I1kqB4bUJjWAmlNu4VRnv9iUuq_by7QjoDtZzj_mjPqel4NYQnvqYr1R54m9Oxp9moHQkierpY8KcYLxyIJ"
},
"followerCount": "45",
"followingCount": "57"
},
"timestamp": "2023-12-10T15:26:45.000Z",
"castHash": "0x6932a9256f34e18892d498abb6d00ccf9f1c50d6"
}
],
"next": {
"cursor": null
}
}
}
That’s it! You can now like or recast any cast on Farcaster.
PS - to learn more about how writes technically works on Farcaster, read here
Ready to start building?
Get your subscription at neynar.com and reach out to us on Telegram with any questions!