API endpoint

This tutorial uses the GET v2/cast endpoint
You have to fetch the raw cast data, mentioned profiles, reactions, profile of the author, etc. and stitch it back into the final cast object before you can render it or use the data in a subsequent operation. Thankfully, Neynar’s SDK and APIs make this much simpler!
cast information

Fetch cast information easily with Neynar SDK

Ensure you have the right environment setup

Make sure to have node.js, typescript (ts) and yarn installed on your machine

Create a new typescript file with the right imports

import { NeynarAPIClient, isApiErrorResponse } from "@neynar/nodejs-sdk";
import { AxiosError } from "axios";

import {
  CastParamType,
} from "@neynar/nodejs-sdk/build/neynar-api/neynar-v2-api";

Initialize the client

const client = new NeynarAPIClient("API_KEY");

Call lookUpCastByHashOrUrl function to get cast data based on a url

(async () => {
  try {
    const cast = await client.lookUpCastByHashOrUrl(
      'https://warpcast.com/rish/0x9288c1',
      CastParamType.Url
    );
    console.log(JSON.stringify(cast));
  } catch (error) {
    // isApiErrorResponse can be used to check for Neynar API errors
    // handle errors accordingly
    if (isApiErrorResponse(error)) {
      console.log("API Error", error.response.data);
    } else {
      console.log("Generic Error", error);
    }
  }
})();
You can change the CastParamType.Url to CastParamType.Hash use a hash (e.g. 0x123…abc) instead to fetch cast data if you so want.

Run your new .ts file

  • Navigate to the right folder in your terminal e.g. cd ./test-sdk
  • Run script by typing yarn start into the terminal

You should now see an output like this

{
    "cast": {
        "hash":"0x9288c1e862aa72bd69d0e383a28b9a76b63cbdb4",
        "thread_hash":"0x9288c1e862aa72bd69d0e383a28b9a76b63cbdb4",
        "parent_hash":null,
        "parent_url":"chain://eip155:7777777/erc721:0x4f86113fc3e9783cf3ec9a552cbb566716a57628",
        "parent_author": {
                "fid":null
            },
        "author": {
            "object":"user",
            "fid":194,
            "custody_address":"0xb43a7cc909d842721c288ff90b03e511a78a4a8d",
            "username":"rish",
            "display_name":"rish",
            "pfp_url":"https://res.cloudinary.com/merkle-manufactory/image/fetch/c_fill,f_png,w_256/https://lh3.googleusercontent.com/MEaRCAMdER6MKcvmlfN1-0fVxOGz6w98R8CrP_Rpzse9KZudgn95frTd0L0ZViWVklBj9fuAcJuM6tt7P-BRN0ouAR87NpzZeh2DGw",
            "profile": {
                "bio": {
                    "text":"@neynar 🪐 | nf.td/rish",
                    "mentioned_profiles":[]
                    }
                },
            "follower_count":8253,
            "following_count":468,
            "verifications": [
                "0x5a927ac639636e534b678e81768ca19e2c6280b7",
                "0xe9e261852ea62150eee685807df8fe3f211310a0"
            ],
            "active_status":"active"
        },
        "app": {
            "object": "user_dehydrated",
            "fid": 9152,
            "username": "warpcast",
            "display_name": "Warpcast",
            "pfp_url": "https://i.imgur.com/3d6fFAI.png"
        },
        "text":".@fun built out hatecast.xyz based on a random conversation we had about app ideas for Farcaster.\n\nThere are many things we'd love to build at @neynar if we had the time but spoiler alert: we don't. So would love to see others build these! \nhttps://paragraph.xyz/@neynar/farcaster-ideas \n\n🪐",
        "timestamp":"2023-09-13T22:10:22.000Z",
        "embeds": [
            {
                "url":"https://paragraph.xyz/@neynar/farcaster-ideas"
                }
            ],
        "reactions": {
            "likes": [
                {
                    "fid":1285,
                    "fname":"0xbenersing.eth"
                },
                {
                    "fid":19315,
                    "fname":"sonofsun"
                }
            ],
            "recasts": [
                {
                    "fid":19036,
                    "fname":"geohookah"
                },
                {
                    "fid":13313,
                    "fname":"008"
                }
            ]
        },
        "replies": {
            "count":10
        },
        "mentioned_profiles": [
            {
                "object":"user",
                "fid":5620,
                "custody_address":"0xbee82d68f49ee2aa8408f8111db64d92e4d61971",
                "username":"fun",
                "display_name":"welter.eth",
                "pfp_url":"https://i.imgur.com/LE5osyF.jpg",
                "profile": {
                    "bio": {
                        "text":"building apps like FarcasterUserStats.com",
                        "mentioned_profiles":[]
                    }
                },
                "follower_count":1522,
                "following_count":207,
                "verifications": [
                    "0x02ac33835070d0c90bef87c273514e67aea36ef8"
                ],
                "active_status":"active"
            }
        ]
    }
}
You get all the data in one request, making it easy to display or use in your next operation. If you want to try getting a live response, head over to our API page for v2/cast.

Ready to start building?

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