Skip to main content

Follow the example code while reading the guide for better understanding

Prerequisites

  • A browser with a wallet extension (e.g., MetaMask, Coinbase, etc.) installed.
    • If you are using a Privy wallet, you can see this guide written by community member jpfraneto, which includes source code for getting started.
  • Access to the Farcaster API and ID Registry smart contract.
  • Familiarity with JavaScript and Ethereum wallet interactions.
  • App Wallet with wallet_id - Required for FID fetching and account registration. See Managing Onchain Wallets to create one in the developer portal.
  • Funded wallet with $5+ ETH on Optimism - Covers initial account pre-registration on first call
New to App Wallets? The /v2/farcaster/user/fid endpoint requires a x-wallet-id header to cover the costs of FID registration. Create your app wallet in the Developer Portal first!Recommended Setup Steps:
  1. Log in to dev.neynar.com and select your app
  2. Activate a wallet from the “App Wallet” tab
  3. Fund the wallet with $5+ ETH on Optimism
  4. Add the x-wallet-id header to your API requests (treat it like a secret)

Step 1: Connect a Wallet

To connect a wallet in the browser:
  1. Check if the browser supports window.ethereum.
  2. Use eth_requestAccounts to request wallet connection.

Code Example:

Step 2: Switch to the Optimism Network

To interact with the ID Registry contract, ensure the wallet is on the Optimism network.

Code Example:

Step 3: Fetch FID

Use the GET-/v2/farcaster/user/fid endpoint to retrieve the FID of the account that will be transferred to the wallet’s address. This endpoint requires a x-wallet-id header.
Cold Start (First Call Only): The first time you call this endpoint with a new wallet_id, it will take approximately 1 minute to complete as it pre-registers a few FID accounts. Subsequent calls will be fast (< 1 second). Make sure your wallet has $5+ ETH on Optimism before the first call.
Wallet Consistency Required: The same wallet_id used here must also be used in Step 6 when calling POST /v2/farcaster/user/. Using different wallets will result in an error.

Code Example:

Backend Required: The FID fetch should be done from your backend, not the frontend, to keep your wallet_id secure. Your backend should call Neynar’s API with the x-wallet-id header.

Step 4: Generate signTypedData with Viem

To generate a signature for FID registration:
  1. Fetch the nonce from the ID Registry contract.
  2. Create EIP-712 typed data and request a signature from the wallet.

Code Example:

Step 5: Check fname Availability

Before registering a username, check if it is available using the GET /v2/farcaster/fname/availability endpoint.

Code Example:

Step 6: Call the POST-/v2/farcaster/user Endpoint

Submit the required data to create the Farcaster account. This endpoint also requires a x-wallet-id header.

Code Example:

Backend Required: Like Step 3, this registration should be done from your backend to keep your wallet_id secure. Your backend calls Neynar’s API with the x-wallet-id header.

Conclusion

By following these steps, you can create an account using the user’s wallet. (No mnemonic required)