Skip to main content
In this guide, we’ll take a look at how to integrate neynar managed signers with neynar in a next.js app so your users can take actions on the Farcaster protocol. Managed signers allow you to take full control of the connection, including the branding on Warpcast and everything else!

If using login providers like Privy, it’s best to use this signer product. Users won’t see a double login and developers will get full write functionality to Farcaster.

Context

This guide covers setting up a full app with frontend and backend to start writing with a logged in user. You can pick and choose the right pieces from this guide as needed. We’ll go over:
  1. Creating a signer key for the user so they can sign in
  2. Storing the user’s credentials in local storage
  3. Writing casts to Farcaster using the signer
Before we begin, you can access the complete source code for this guide on GitHub. The simplest way to set up managed signers is by cloning the above repo! You can use the following commands to check it out:

The ideal user flow

Terminology To understand the ideal user flow, let’s quickly go over some terminology:
  • Authentication: This is where an account proves they are who they say they are. Flows like Sign in with Farcaster (SIWF) or login providers like Privy allow this for app logins.
  • Authorization: this is where an account gives the app certain access privileges to take actions on behalf of the account. This is what Neynar signers allow for writing data to the protocol.
Authorization requires authentication so that once a user is authenticated, they can then authorize an action. E.g. authenticating into your Google account to then authorize a 3rd party app like Slack to access your Google Calendar. If starting logged out, the full flow takes two distinct steps. User journey You can build a user flow using tools like: If using Privy
  • the 1st step on authentication happens on Privy login and the 2nd step of authorization happens on Neynar
  • The second step requires the user to scan a QR code or tap on a link to then generate a signer on a Farcaster client like Warpcast
  • Generating a signer requires paying onchain fees. Neynar sponsors signers by default so users pay $0.
Let’s go! Now that we have context, let’s get started!

Setting up a next.js app

Creating a next app

We are going to need a frontend as well as a backend server, so we are going to use Next.js for this guide. Run the following command to create a new next.js app:
Choose the configuration for your app and wait for the dependencies to install. Once they are installed, let’s install the additional packages that we are going to need:
Now, you can open the folder in your favourite code editor and we can start building!

Configuring env variables

Firstly, let’s configure the env variables we will need. Create a new .env.local file and add these two variables:
  • The neynar api key should be the api key that you can view on your dashboard
  • the mnemonic should be the mnemonic associated with the developer account which will be used to create the signers e.g. @your_company_name account on Farcaster (to state the obvious out loud, you won’t need user mnemonics at any point)

Creating API route for generating the signature

Create a new route.ts file in the src/app/api/signer folder and add the following:
This is just defining a get and a post request and calling a getSignedKey function but we haven’t created that yet, so let’s do that. Create a new utils/getSignedKey.tsfile and add the following:
We are doing a couple of things here, so let’s break it down. We first use the neynarClient (yet to create) to create a signer, and then we use the appAccountKey.signKeyRequest function from the @farcaster/hub-nodejs package to create a sign key request. Finally, we use the registerSignedKey function from the neynarClient to return the signedKey. Let’s now initialise our neynarClient in a new lib/neynarClient.ts file like this:
We are also using another util function named getFid in the signature generation, so let’s create a utils/getFid.ts file and create that as well:
We can use this api route on our front end to generate a signature and show the QR code/deep link to the user. So, head over to app/page.tsx and add the following:
Here, we show a button to sign in with farcaster in case the farcasterUser state is empty which it will be initially. And if the status is “pending_approval” then we display a QR code and a deep link for mobile view like this:
Sign in with farcaster
If you try scanning the QR code it will take you to Warpcast to sign into your app! But signing in won’t do anything right now, so let’s also handle that. In the api/signer/route.ts file let’s add a GET function as well to fetch the signer using the signer uuid like this:
Let’s use this route in the page.tsx file to fetch the signer and set it in local storage using some useEffects:
Here, we are checking if the user has approved the TX and if they have approved it, we call the signer api and set the user details in the local storage. Let’s also add a condition in the return statement to display the fid of the user if they are logged in:

Allowing users to write casts

Let’s now use the signer that we get from the user to publish casts from within the app. Create a new api/cast/route.ts file and add the following:
Here, I am using the publishCast function to publish a cast using the signer uuid and the text which I am getting from the body of the request. Head back to the page.tsx file and add a handleCast function to handle the creation of casts like this:
Now, we just need to add an input to accept the cast text and a button to publish the cast. Let’s add it below the hello fid text like this:
Your final page.tsx file should look similar to this:
If you now try going through the whole flow of signing in and creating a cast, everything should work seamlessly!

Read more Two Ways to Sponsor a Farcaster Signer via Neynar to see how to sponsor a signer on behalf of the user

Conclusion

This guide taught us how to integrate neynar managed signers into your next.js app to add sign-in with Farcaster! If you want to look at the completed code, check out the GitHub repository. Lastly, please share what you built with us on Farcaster by tagging @neynar and if you have any questions, reach out to us on warpcast or Slack!