Liking and recasting with Neynar SDK

πŸ’‘

  • 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 } 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;

Then like a cast:

const hash = "0x6932a9256f34e18892d498abb6d00ccf9f1c50d6";
client.publishReactionToCast(signer, "like", hash);

Recasting works the same way, just replace "like" with "recast":

const hash = "0x6932a9256f34e18892d498abb6d00ccf9f1c50d6";
client.publishReactionToCast(signer, "recast", hash);

The end result should look like this:

{
  success: true;
}

To verify that the reaction was published, you can fetch the cast's reactions:

const hash = "0x6932a9256f34e18892d498abb6d00ccf9f1c50d6";
const reactions = await client.fetchCastReactions(hash);
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 work on Farcaster, read here

πŸš€

Ready to start building?

Get your subscription at neynar.com and reach out to us on Telegram with any questions!