Skip to main content

Prerequisites

This guide assumes you’ve completed the steps to create an account, organization, and API keypair as described in the Account Setup section.

Installation

Create a new Next.js app via npx 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
You may learn more about this here.

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 Users@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.tsx
We’re actively working towards React 19 compatibility.

Authenticate

Auth Component
The auth component contains the UI and logic to handle the authentication flow.
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
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.
src/app/dashboard/page.tsx
Since the app is wrapped with the TurnkeyProvider component, the useTurnkey hook is available to all child components. Calling turnkey.getSession() 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

Sign Message

Turnkey supports signing arbitrary messages with the signRawPayload method. The signRawPayload method requires these parameters:
  • payload: The raw unsigned payload to sign
  • signWith: The signing address (wallet account, private key address, or private key ID)
  • encoding: The message encoding format
  • hashFunction: 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 getSession method from the useTurnkey hook to get 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
Note: To compute the 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
5

Handle logout

Add a logout button that triggers handleLogout and clears the indexedDb stored key and session.
src/app/dashboard/page.tsx

Recap

In this quickstart guide, you’ve learned how to:
  1. Set up Turnkey’s SDK in a Next.js application
  2. Configure authentication with email sign-in
  3. Create a protected dashboard route
  4. Implement message signing functionality using a user’s Turnkey wallet
  5. Handle user sessions and wallet interactions
src/app/page.tsx
src/app/dashboard/page.tsx

Next Steps

Learn more about integrating Embedded Wallets and our powerful features here.