API endpoint

This tutorial uses the GET v2/notifications endpoint
Show follow, like, recast, reply, quote and mention notifications for a given user.
notifications

Get notifications for a given user fid

In this example, we will try to create a notifications page for the given user.

Prerequisites

  • Install Node.js
  • Optional: Install Yarn (Alternatively, npm can be used)

Project Setup

Initialize Project Directory
mkdir fetch-notifications-for-a-user
cd fetch-notifications-for-a-user
Install Neynar SDK along with typescript Install using npm
npm i @neynar/nodejs-sdk
npm i -D typescript
// or Install using Yarn
yarn add @neynar/nodejs-sdk
yarn add -D typescript
Initialize typescript environment
npx tsc --init

Implementation

Create index.ts file at root level
touch index.ts
Add imports
import { NeynarAPIClient, isApiErrorResponse } from "@neynar/nodejs-sdk";
import { AxiosError } from "axios";
Instantiate the client
const client = new NeynarAPIClient("API_KEY");
Call fetchAllNotifications function to get feed
(async () => {
  try {
    const cast =
      await client.fetchAllNotifications(194, {
        limit: 1, // change to however many notifications you want to get at once (max 50)
      });
    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);
    }
  }
})();

Running the project

npx ts-node index.ts

Result

You should now see an output like this

{
  "notifications": [
    {
      "object": "notification",
      "most_recent_timestamp": "2023-11-17T01:22:43.000Z",
      "type": "likes", // Can be "likes", "mentions", "replies", or "quotes"
      "cast": {
        "hash": "0x1aa36542c2dc8799dded2cde37a3c1a3840eb41f",
        "thread_hash": "2a47ce562ff5ce5916ea9095227c5fbff9618157",
        "parent_hash": "2a47ce562ff5ce5916ea9095227c5fbff9618157",
        "parent_url": null,
        "parent_author": {
          "fid": "576"
        },
        "author": {
          "object": "user",
          "fid": 194,
          "custody_address": "0xb95dc10483be18f7c69ddf78d4baafecc01c5530",
          "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": 8313,
          "following_count": 470,
          "verifications": [
            "0x5a927ac639636e534b678e81768ca19e2c6280b7",
            "0xe9e261852ea62150eee685807df8fe3f211310a0"
          ],
          "active_status": "active"
        },
        "text": "wtf",
        "timestamp": "2023-11-17T00:56:01.000Z",
        "embeds": [],
        "reactions": {
          "likes": [
            {
              "fid": 1600,
              "fname": "yashkarthik"
            },
            {
              "fid": 576,
              "fname": "nonlinear.eth"
            },
            {
              "fid": 6546,
              "fname": "artlu.eth"
            }
          ],
          "recasts": []
        },
        "replies": {
          "count": 1
        },
        "mentioned_profiles": []
      },
      "reactions": [
        {
          "object": "likes",
          "cast": {
            "object": "cast_dehydrated",
            "hash": "0x1aa36542c2dc8799dded2cde37a3c1a3840eb41f"
          },
          "user": {
            "object": "user",
            "fid": 1600,
            "username": "yashkarthik",
            "display_name": "Yash Karthik",
            "pfp_url": "https://lh3.googleusercontent.com/W_MM3NN-i9OYxM3XPJjwpb5mkMLjJFZJjnEXsvrhiTwirSFhRjyAa3qTzV63ago6NkX9qeesi20hoK9fHdhiE-SqICH0vPcTm3Dl",
            "profile": {
              "bio": {
                "text": "I like building cool stuff on top of Ethereum, Farcaster and GPT (dm for collab)\n\nCurrently learning C and exploring low level stuff\n\nnf.td/yashkarthik"
              }
            },
            "follower_count": 585,
            "following_count": 336,
            "verifications": [
              "0x33cc45d8b0336bfa830fb512b54b02a049277403"
            ],
            "active_status": "active"
          }
        },
      ]
    }
  ],
  "next": {
    "cursor": "eyJ0aW1lc3RhbXAiOiIyMDIzLTExLTE3IDAxOjIyOjQzLjAwMDAwMDAifQ=="
  }
}

Summary

You can use this to show a notification on your client! 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/feed. You can take the cursor from the output and pass it in the next request to the SDK to page to the next set of results.

Ready to start building?

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