Getting started with Neynar NodeJS SDK

Easily call Neynar APIs with our nodejs sdk

📘

This tutorials uses the Neynar nodejs sdk

Prerequisites

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

Project Setup

Initialize Project Directory

mkdir get-started-with-neynar-sdk
cd get-started-with-neynar-sdk

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: Let's use sdk to look up a user by their FID

Create index.ts file at root level

touch index.ts

Add following code in index.ts

// index.ts

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

// Instantiate the client
const client = new NeynarAPIClient("<YOUR_API_KEY_HERE>"); // Replace with your Neynar API Key.

(async () => {
  try {
    // 19960 (Required*) => fid of user  we are looking for
    // 191 (Optional) => fid of the viewer
    // Get more info @ https://docs.neynar.com/reference/user-v1
    const user = await client.lookupUserByFid(19960, 191);

    // Stringify and log the response
    console.log(JSON.stringify(user));
  } catch (error) {
    // isApiErrorResponse can be used to check for Neynar API errors
    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 see a response like this. (You might not get beautified/ formated response since we JSON.stringify the response in order to log everything)

{
  "result": {
    "user": {
      "fid": 19960,
      "custodyAddress": "0xd1b702203b1b3b641a699997746bd4a12d157909",
      "username": "shreyas-chorge",
      "displayName": "Shreyas",
      "pfp": {
        "url": "https://i.imgur.com/LPzRlQl.jpg"
      },
      "profile": {
        "bio": {
          "text": "Everyday regular normal guy | 👨‍💻 @neynar",
          "mentionedProfiles": []
        }
      },
      "followerCount": 13,
      "followingCount": 55,
      "verifications": ["0xd1b702203b1b3b641a699997746bd4a12d157909"],
      "activeStatus": "inactive",
      "viewerContext": {
        "following": true,
        "followedBy": false
      }
    }
  }
}

Congratulations! 🎉 You successfully setup @neynar/nodejs-sdk and used it to look up a user by their FID!

🚀

Ready to start building?

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