Embedded Wallets Quickstart
Turnkey’s Embedded Wallets enable you to integrate secure, custom wallet experiences directly into your product. With features like advanced security, seamless authentication, and flexible UX options, you can focus on building great products while we handle the complexities of private key management.
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 via npx create-next-app@latest
. Or install into an existing project.
React 19 Users
If 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
Environment
The following environment variables are necessary to use the Turnkey SDK.
Configure
Fill in with your Organization ID and API Base URL.
Provider
Wrap your layout with the TurnkeyProvider
component, and import styles from sdk-react.
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
The auth component contains the UI and logic to handle the authentication flow.
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.
Import
Import the auth component into your app and pass in the config object.
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.
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 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:
Sign Message
Turnkey supports signing arbitrary messages with the signRawPayload
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
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:
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:
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.
Display
Add a button to the UI to trigger the signMessage
function.
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
Next Steps
Was this page helpful?