Prerequisites
This guide assumes you’ve completed the steps to create an account, organization, and API keypair as described in the Quickstart section.Installation
Create a new Next.js app vianpx create-next-app@latest
. Or install into an existing project.
Technical RequirementsNext.js App Router Required
@turnkey/sdk-react
requires the Next.js App Router architecture as it leverages React Server Components and Server Actions to handle authentication on your behalf. The SDK automatically manages the “use client” and “use server” directives. The Pages Router is not supported.React 19 UsersIf you’re using Next.js 15 with React 19 you may encounter an installation error with @turnkey/sdk-react
. Consider:- Downgrading React to
18.x.x
- Using
npm install --force
or--legacy-peer-deps
Setup
1
Environment
The following environment variables are necessary to use the Turnkey SDK.
.env
Note: These environment variable names must be used exactly as shown.
The SDK depends on them internally for server actions and authentication.
2
Configure
Fill in with your Organization ID and API Base URL.
src/app/layout.tsx
3
Provider
Wrap your layout with the
TurnkeyProvider
component, and import styles from sdk-react.src/app/layout.tsx
React 19 UsersWe’re actively working towards React 19 compatibility.
@turnkey/sdk-react
is built with React 18. If you’re using React 19 you’ll find a type mismatch on the children type.To fix this, you can use the @ts-ignore
directive to suppress the error.src/app/layout.tsxAuthenticate

1
Configure
For simplicity, this app will only support email authentication. We have other guides on additional authentication methods. Additionally, you can customize the order in which the auth methods are displayed.
src/app/page.tsx
Auth Config Options
Auth Config Options
2
Import
Import the auth component into your app and pass in the config object.
src/app/page.tsx
3
Handlers
Define two functions to handle the “success” and “error” states. Initially, the
onError
function will set an errorMessage
state variable which will be used to display an error message to the user. The onAuthSuccess
function will route the user to the dashboard after successful authentication.A new sub-organization and wallet is created for each new user during the authentication flow.src/app/page.tsx
4
Dashboard: User Session
Add a dashboard route to the app where the user will be able to view their account and sign messages.Since the app is wrapped with the
src/app/dashboard/page.tsx
TurnkeyProvider
component, the useTurnkey
hook is available to all child components. Calling turnkey.getCurrentUser()
will return the current user’s session information from local storage.Add a state variable to store the user:src/app/dashboard/page.tsx
User Session
User Session
Sign Message
Turnkey supports signing arbitrary messages with thesignRawPayload
method.
The signRawPayload
method requires these parameters:
payload
: The raw unsigned payload to signsignWith
: The signing address (wallet account, private key address, or private key ID)encoding
: The message encoding formathashFunction
: The selected hash algorithm
1
The Payload
For simplicity, a human readable string,
message
, will be the payload to sign. Add a state variable to store the message and an input field to allow the user to enter the message:src/app/dashboard/page.tsx
2
The Signer
Signing messages requires a signer e.g. a Turnkey wallet address to sign with and a payload or message to sign. A new wallet is created for each user during the authentication flow.Create a function called
getSignWith
, to get the user’s wallet account address which will be used to sign the message.Use the getActiveClient
method from the useTurnkey
hook to get the client authenticated with the user’s read-write session:src/app/dashboard/page.tsx
3
The Signing Function
Create a function called
signMessage
. This function will:- Get the user’s wallet account for signing the message
- Compute the keccak256 hash of the message
- Call the
signRawPayload
method
keccak256
hash of the message, this example uses the hashMessage
function from viem
. However, any other hashing library can be used.4
Display
Add a button to the UI to trigger the
signMessage
function.src/app/dashboard/page.tsx
Recap
In this quickstart guide, you’ve learned how to:- Set up Turnkey’s SDK in a Next.js application
- Configure authentication with email sign-in
- Create a protected dashboard route
- Implement message signing functionality using a user’s Turnkey wallet
- Handle user sessions and wallet interactions
Complete Code
Complete Code
src/app/page.tsx