User Balances Directly with FID
This guide provides a step-by-step approach to fetching token balances for a user using their Farcaster FID via the Neynar API.
Related API: Token balance
Fetching User Balances Using Farcaster FID with Neynar API
This API abstracts the complexity of finding Ethereum addresses and querying multiple providers, allowing developers to retrieve balances with a single API call.
Overview
-
API Endpoint:
/farcaster/user/balance
-
Method:
GET
-
Parameters:
fid
(required): The Farcaster FID of the user.networks
(required): A comma-separated list of networks to fetch balances for. Currently, only “base” is supported.
Prerequisites
- API Key: Ensure you have a Neynar API key. You can obtain one by signing up at neynar.com.
- Node.js SDK: Install the Neynar Node.js SDK.
Fetching User Balances
Initialize the Neynar Client
First, set up the Neynar client using your API key.
Fetch User Balances
Use the fetchUserBalance
method to retrieve the token balances for a user by their FID.
Response Structure
The response will include the user’s balance information structured as follows:
Error Handling
Ensure to handle potential errors, such as invalid FID or network issues, by wrapping your API calls in try-catch blocks.
Conclusion
By following this guide, you can efficiently fetch token balances for a user using their Farcaster FID with the Neynar API. This streamlined process eliminates the need for multiple API calls and simplifies the integration into your application.