This package, pi-sdk-nextjs, helps you quickly scaffold, configure, and integrate all
necessary components for using Pi Network payments, authentication,
and user flows with a Next.js project. It is designed for modern
Next.js apps (App Router or Pages Router) that want a working,
idiomatic Pi payment and authentication experience with minimal
boilerplate.
The pi-sdk-nextjs
package
is part of the “Ten Minutes to Transactions” effort described in this
video.
If you are planning to use the Next.js framework for your app, it is highly suggested that you use this package rather than implement transaction processing by hand with the core Pi SDK. The three way handshake between client, server, and the Pi servers required is provded for you.
While this process is covered in the Getting Started Guide, here is a brief reminder of the steps you need to take. Application registration is also discussed in the video.
http://localhost:3000. The actual port is between you and your development server.pi-sdk-nextjs as a dependency in your Next.js projectnpm install pi-sdk-nextjsyarn add pi-sdk-nextjsThis step, and the following ones, can be seen in the video.
npx pi-sdk-nextjs-installyarn pi-sdk-nextjs-installThis will generate:
components/PiButton.tsx file (ready-to-use React component)app/api/pi_payment/<event>/route.ts (approve, complete, cancel, error, incomplete)Add in your main app HTML (e.g., public/index.html or via a Script/Head component).
<head>
<!-- ... other <head> content (meta, title, styles, etc.) ... -->
<script src="https://sdk.minepi.com/pi-sdk.js"></script>
</head>This is required for all Pi SDK browser integrations.
import { PiButton } from '@/components/PiButton';
// or relative:
import { PiButton } from '../components/PiButton';
export default function Page() {
return <PiButton />;
}export PI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxStart the local server.
npm devyarn devVisit https://sandbox.minepi.com/mobile-app-ui/app/your-app-name. It will ask you to provide an
authorization code to the Pi Mining app. Click the link at the bottom of the Pi Utilities screen.
You can watch a video describing the entire process, or just setting up Next.js. Here are the commands used in the video.
# Create the app
yarn create next-app tmtt_nextjs --yes
cd tmtt_nextjs
# Add the Pi SDK package for Next.js
yarn add pi-sdk-nextjs@https://github.com/pi-apps/pi-sdk-nextjs
# Generate the routes and PiButton.tsx
yarn pi-sdk-nextjs-install
# Load Pi SDK on your pages
sed -i '' '3i\
import Script from "next/script";\
' app/layout.tsx
sed -i '' '28i\
<head>\
<Script src="https://sdk.minepi.com/pi-sdk.js" strategy="beforeInteractive" />\
</head>\
' app/layout.tsx
# Enable PiButton on the home page
sed -i '' '2i\
import { PiButton } from "@/components/PiButton";
' app/page.tsx
sed -i '' '38i\
<PiButton/>
' app/page.tsx
# Build the app
yarn buildpi-sdk-nextjs-install actually do?PiButton component for direct use in your app.use client directives for new components.components/PiButton.tsx and API route files as you wish. New SDK versions won’t overwrite unless you delete them first (or add a force flag to the installation script).pi-sdk-nextjs-install many times?pi-sdk-react, pi-sdk-js) for advanced use cases, custom payment flows, and more.