Overview
Build a Next.js app with CDP embedded wallets using our dedicated Next.js template. This guide shows you how to create a server-side rendered app with CDP’s client-side wallet functionality.Check out the CDP Web SDK reference for comprehensive method signatures, types, and examples.
Prerequisites
- A free CDP Portal account and project
- Node.js 22+
- A node package manager installed (i.e.,
npm
,pnpm
, oryarn
) - Basic familiarity with Next.js and React
1. Create a Next.js app with CDP
1
Create a new demo app
Use the latest version of
cdp-app
to create a new Next.js app:2
Copy your Project ID
Navigate to CDP Portal and select your project from the top-left dropdown. Copy the Project ID value for the next step.
2. Configure
Follow the prompts to configure your app. When prompted, select Next.js as your template and enter your CDP Project ID:Note that we also support EVM Smart accounts and Solana accounts for Next.js.
3. Run
Navigate to your project and start the development server:Want to see a walkthrough of using the demo app? Check out the Quickstart guide for step-by-step instructions on signing in, funding your wallet, and sending your first transaction.
How it works
The Next.js integration uses a clean separation between server and client components to leverage Next.js’s server-side rendering while maintaining CDP’s client-side functionality.Provider pattern
The template follows Next.js best practices by:- Keeping the root layout as a server component (no CDP imports or hooks)
- Making the page component a client component that wraps the app with providers
- Containing all CDP functionality within client components
Component architecture
Server component (root layout)
The layout remains a pure server component for metadata and basic HTML structure:src/app/layout.tsx
Client component (page)
In Next.js, components that use CDP hooks (
useEvmAddress
, useSendEvmTransaction
, etc.) or CDP components (AuthButton
, Button
, etc.) must include the "use client"
directive. Your root layout and any components without CDP functionality can remain server components.src/app/page.tsx
Themed providers
The Providers component wraps the app with CDP context and includes theme customization:src/components/Providers.tsx
src/components/theme.ts
Authentication flow
The template implements a complete authentication flow:- ClientApp checks initialization and authentication status
- SignInScreen displays the AuthButton for sign-in
- SignedInScreen shows wallet features after authentication
Example components
Sign-in screen
src/components/SignInScreen.tsx
Transaction component
The template includes a full-featured transaction component that demonstrates sending ETH on Base Sepolia:src/components/Transaction.tsx
- Base Sepolia: A test network where you can experiment with fake ETH
- Gas: The computational fee (21000 is standard for simple ETH transfers)
- Wei: The smallest unit of ETH (1 ETH = 10^18 wei)
- EIP-1559: Modern transaction format with predictable gas fees
Troubleshooting
Common errors without 'use client'
Common errors without 'use client'
If you forget the
"use client"
directive in components that use CDP functionality, you’ll see errors like:TypeError: createContext is not a function
Error: useContext must be used within a Provider
window is not defined
ordocument is not defined
ReferenceError: localStorage is not defined
"use client"
as the first line in any component file that uses CDP hooks or components.What to read next
- Quickstart: Full walkthrough of building your first CDP app with detailed explanations
- CDP React Hooks: Available hooks like
useSignInWithEmail
,useEvmAddress
, anduseSendEvmTransaction
- CDP React Components: Pre-built UI components for authentication and wallet management
- CDP Web SDK Reference: Comprehensive API documentation