Using Privy Wallet on Somnia
Privy is a secure, embeddable wallet infrastructure provider that allows developers to authenticate users, manage sessions, and provide seamless wallet experiences within dApps. Privy embedded wallets can be made interoperable across apps. Somnia has adopted the global wallets setup to foster a cross-app ecosystem where users can easily port their wallets from one app to another in the Somnia Ecosystem.
Using global wallets, users can seamlessly move assets between different apps and easily prove ownership of, sign messages, or send transactions with their existing wallets. Developers do not have to worry that users will generate a new wallet to sign into different applications. Kindly read more here. This guide will integrate Privy with the Somnia Testnet, enabling users to create and connect wallets effortlessly.

Prerequisites
This guide is not an introduction to JavaScript Programming; you are expected to understand JavaScript.
To complete this guide, sign up for Privy and get an AppID and get the Somnia Provider AppID.
Familiarity with React and Next.js is assumed.
Installation
Create the Next.js Project
Open your terminal and run the following commands to set up a new Next.js project:
npx create-next-app@latest somnia-privy
cd somnia-privy
Install the necessary packages
npm install @privy-io/react-auth viem
Set Up PrivyProvider
Go to https://dashboard.privy.io/ to set up an account.
Click "New App
" to create a new application that will connect to the Somnia Provider AppID.

Open the newly created app and in the left side navigation menu navigate to:
User Management >>>> Global Wallet >>>> Integrations
Click the toggle to turn ON the Somnia Provider App.

Wrap your application layout.ts
file with PrivyProvider and supply your PrivateKey from Privy and the Somnia Provider App ID to the loginMethods
:
'use client';
import { PrivyProvider } from '@privy-io/react-auth';
import { somniaTestnet } from 'viem/chains';
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang='en'>
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
<PrivyProvider
appId={process.env.NEXT_PUBLIC_PRIVY_APP_ID!}
config={{
loginMethods: {
primary: ['email', 'google', 'privy:cm8d9yzp2013kkr612h8ymoq8'],
},
defaultChain: somniaTestnet,
supportedChains: [somniaTestnet],
embeddedWallets: {
createOnLogin: 'users-without-wallets',
},
}}
>
{children}
</PrivyProvider>
</body>
</html>
);
}
Add your environment variable in .env.local:
NEXT_PUBLIC_PRIVY_APP_ID=your-privy-app-id
Privy Hooks
These hooks make it easy to authenticate users, manage wallets, and interact with the Somnia Network using Privy Global Wallet
import { useCrossAppAccounts, usePrivy } from '@privy-io/react-auth';
Authenticate
Use the provided hooks to authenticate users and access their wallets.
Send Transactions
Once authenticated, use the useSendTransaction
hook from useCrossAppAccount
method to interact with Somnia Testnet:
const { sendTransaction } = useCrossAppAccounts();
......
const sendSTT = async () => {
if (!walletAddress) return;
const txn = {
to: '0xb6e4fa6ff2873480590c68D9Aa991e5BB14Dbf03',
value: 1000000000000000,
chainId: 50312,
};
try {
const tx = await sendTransaction(txn, { address: walletAddress });
console.log('TX Sent:', tx);
} catch (err) {
console.error('TXN Failed:', err);
}
};
......
<button onClick={sendSTT}> Send 0.001 STT</button>
Complete Code
By using Privy Global Wallet on the Somnia Testnet, developers can offer a seamless onboarding and wallet experience. This setup is ideal for onboarding Web2 users into Web3 with embedded wallets, abstracting away traditional wallet complexities.
Last updated