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 the following code in index.ts

// index.ts

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


const config = new Configuration({
  apiKey: "<YOUR_API_KEY_HERE>", // Replace with your Neynar API Key.
});

const client = new NeynarAPIClient(config);

(async () => {
  try {
    const fid = 19960; // 19960 (Required*) => fid of user we are looking for
    const viewerFid = 194; // 191 (Optional) => fid of the viewer
    // Get more info @ https://docs.neynar.com/reference/fetch-bulk-users
    const users = await client.fetchBulkUsers({ fids: [fid], viewerFid });

    // Stringify and log the response
    console.log(JSON.stringify(users));
  } 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 a beautified/ formatted response since we JSON.stringify the response to log everything)

{
  "users": [
    {
      "object": "user",
      "fid": 19960,
      "username": "shreyas-chorge",
      "display_name": "Shreyas",
      "pfp_url": "https://i.imgur.com/LPzRlQl.jpg",
      "custody_address": "0xd1b702203b1b3b641a699997746bd4a12d157909",
      "profile": {
        "bio": {
          "text": "Everyday regular normal guy | 👨‍💻 @neynar ..."
        },
        "location": {
          "latitude": 19.22,
          "longitude": 72.98,
          "address": {
            "city": "Thane",
            "state": "Maharashtra",
            "country": "India",
            "country_code": "in"
          }
        }
      },
      "follower_count": 250,
      "following_count": 92,
      "verifications": [
        "0xd1b702203b1b3b641a699997746bd4a12d157909",
        "0x7ea5dada4021c2c625e73d2a78882e91b93c174c"
      ],
      "verified_addresses": {
        "eth_addresses": [
          "0xd1b702203b1b3b641a699997746bd4a12d157909",
          "0x7ea5dada4021c2c625e73d2a78882e91b93c174c"
        ],
        "sol_addresses": []
      },
      "verified_accounts": null,
      "power_badge": false,
      "viewer_context": {
        "following": true,
        "followed_by": true,
        "blocking": false,
        "blocked_by": false
      }
    }
  ]
}

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

⚠️ Caution: Please do not use @neynar/nodejs-sdk on browser since NEYNAR_API_KEY will be exposed in the bundle.

🚀

Ready to start building?

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