20 min read
Overview
Solana Pay enables swift, secure payment rails on the Solana blockchain. A little-known fact, however, is that the technology behind Solana Pay can be used for more than just payments. This guide will show you how to use Solana Pay to invoke a custom Solana program.
What You Will Do
Create a Next.js 13 application that generates a QR code that is used to invoke a custom Solana program through your backend:
Source: Solana Pay Docs
Specifically, you will:
- Create a new Next.js project.
- Build a simple UI with React.
- Use Next API routes to generate a custom program transaction.
- Render a QR code for the user to access and sign the transaction.
- Use Solana Websockets to listen to the program and update a counter in the UI when the program is invoked.
What You Will Need
This advanced guide will use concepts from several elements of building on Solana. Please review these requisites before proceeding.
- Nodejs (version 16.15 or higher) installed
- TypeScript experience and ts-node installed
- Experience with Next.js and React
- Experience with Solana Pay will be helpful: review our Guide to Getting Started with Solana Pay and Guide to Adding Solana Pay to your dApp
- A mobile wallet that supports Solana Pay (e.g., Solflare) (Note: at present, there is a known issue with Phantom on Android, so if you have an Android device, we recommend to use Solflare instead. We will update this when the issue has been fixed.)
- Ngrok, or another service to expose your local development server to the internet (or a service like Vercel, if you want to deploy your application to the cloud)
- Experience with Solana programs (Check out our Guide: Your First Anchor Program in Solana)
- Experience Deserializing Solana Account Data (Check out our Guide: How to Deserialize Account Data on Solana)
Create a New Next.js Project
To get started, open your terminal and run the following command to create a new Next.js project:
npx create-next-app@latest solana-pay-beyond
## or
yarn create next-app solana-pay-beyond
You will be prompted with about 5 questions on how you want to configure your project. For this guide, you can accept the default values for all of them. This will create a new directory, solana-pay-beyond, for your project and initialize it with the latest version of Next.js. Navigate to your new project directory:
cd solana-pay-beyond
Run yarn dev to start the development server and make sure the installation was successful. This will open up the project in your default browser (usually localhost:3000). You should see the default Next.js landing page:

Great job. Close the browser window and stop the development server by pressing Ctrl + C (or Cmd + C on Mac) in your terminal.
Now we need to install the Solana-web3.js, Solana Pay, and Solana Kit packages. We will use Kit's codecs to decode our program's account data. Run the following command:
npm install @solana/web3.js@3 @solana/pay @solana/kit
## or
yarn add @solana/web3.js@3 @solana/pay @solana/kit
Finally, you'll need a Solana endpoint to connect to the Solana devnet to assemble a transaction.
Connect to a Solana Cluster with Your Quicknode Endpoint
Create a Solana Devnet endpoint to connect your application to the network. You can use a public endpoint, run your own node, or create a Quicknode endpoint.
Open the HTTPS view of your endpoint to find the connection settings.
Great job. You're ready to start building your application. If you need help getting set up or running into any issues, please reach out to us on Discord.
Create a Custom Solana Program
For this demonstration, we will use a simple program incrementing a counter. We will ultimately invoke this program by calling an increment instruction with our QR code. We have already created a program for you to use in this guide (Devnet yV5T4jugYYqkPfA2REktXugfJ3HvmvRLEw7JxuB2TUf). The program includes a function called increment that will increment a counter by 1 each time it is called.
The important thing to know about our program is it creates a single PDA that stores a count state. For more information on PDAs, check out our Guide: How to use PDAs. Here's our account struct:
#[account]
pub struct Counter {
pub count: u64,
}
If you want to see this program's source code or create your own version, check it out on Solana Playground.
Create Your Backend
Before building our backend, let's look at the steps we need to take to send a custom transaction using Solana Pay. Here's a summary of the Solana Pay spec and flow for sending custom transactions:
- User scans a QR code on the frontend.
- User's wallet sends a
GETrequest to the backend - Backend receives the request and responds with a
labelandiconURL to display to the user in their wallet. - User's wallet sends a
POSTrequest to the backend with the user'saccountid (public key as a string)* - Backend receives the request and assembles a Solana Transaction that includes the
incrementinstruction on our custom program. - Backend responds with a serialized transaction.
- User approves and signs the transaction in their wallet.
- User's wallet sends the signed transaction to the cluster for processing
In short, our backend must respond to a GET request with a label and icon URL and a POST request with a serialized transaction.
For this demonstration, we will use Next.js API routes. API routes are a great way to create a backend for your application without setting up a separate server. "Any file inside the folder pages/api is mapped to /api/* and will be treated as an API endpoint instead of a page." You can read more about Next.js API routes here.
Navigate to pages/api and delete hello.ts. We will be replacing this file with our own API routes. Create a new file called pay.ts and add the following code:
In .env.local, set QUICKNODE_RPC_URL to your Solana Devnet Base URL and QUICKNODE_RPC_TOKEN to its Auth token. An existing full HTTPS URL also works with the token variable unset. The API route reads these variables on the server.
import { NextApiRequest, NextApiResponse } from 'next';
import { Connection, PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';
import crypto from 'crypto';
// CONSTANTS
const programId = new PublicKey('yV5T4jugYYqkPfA2REktXugfJ3HvmvRLEw7JxuB2TUf'); // 👈 You can use this program or create/use your own
const counterSeed = 'counter'; // This is the seed used to generate the counter account (may be different if you use a different program)
const functionName = 'increment'; // This is the name of our anchor instruction (may be different if you use a different program)
const message = `Quicknode Demo - Increment Counter`;
function getConnection() {
const { QUICKNODE_RPC_URL, QUICKNODE_RPC_TOKEN } = process.env;
if (!QUICKNODE_RPC_URL) throw new Error('Set QUICKNODE_RPC_URL on the server.');
return new Connection(QUICKNODE_RPC_URL, {
commitment: 'finalized',
httpHeaders: QUICKNODE_RPC_TOKEN ? { 'x-token': QUICKNODE_RPC_TOKEN } : undefined,
});
}
const label = 'QuickCount +1';
const icon = 'https://www.arweave.net/wtjT0OwnRfwRuUhe9WXzSzGMUCDlmIX7rh8zqbapzno?ext=png';
// Utility function to generate data for a specific Anchor instruction
function getInstructionData(instructionName: string) {
return Buffer.from(
crypto.createHash('sha256').update(`global:${instructionName}`).digest().subarray(0, 8)
);
}
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'POST') {
// POST code will be here
} else if (req.method === 'GET') {
res.status(200).json({ label, icon });
} else {
res.status(405).json({ error: 'Method Not Allowed' });
}
}
What we are doing here is:
- defining some key variables that we will use throughout the application. We are also importing the necessary packages from Solana Pay, Solana-web3.js, and crypto (a NodeJS library).
- defining a few constants here for demonstration--you may want to make some of these values variable based on your application's needs (e.g.,
messageandlabel). If you are using a different program than the one we provided, you will likely need to modify thecounterSeedandfunctionNameconstants. - defining our API handler. We will use a single handler to process
GETandPOSTrequests. We will use thereq.methodproperty to determine which action to take. If the request method isGET, respond with alabelandiconURL - since we defined these in our constants, we can just return them, callingres.status(200).json({ label, icon }). We will generate a transaction if the request method isPOST. If the request method is anything else, we will return an error. You could use a separate handler for each action, but for the sake of simplicity, we will use a single handler. - defining a
getInstructionDatafunction. This function will generate the data for ourincrementinstruction. We are using a hashing function to generate the serialized data that we can pass into our Transaction. This is how Anchor serializes account instructions - you can see the source code here.
Handle POST Requests - Generate a Transaction
When the wallet sends a POST request to our backend, we will need to generate a transaction. We will use the account id (public key) the wallet sends us to create the transaction. First, we will need to ensure the wallet, in fact, passed an account. Add the following code to the POST handler:
if (req.method === 'POST') {
try {
const account: string = req.body?.account;
if (!account) return res.status(400).json({ error: 'Missing account field' });
const transaction = await generateTx(account);
res.status(200).send({ transaction, message });
} catch {
res.status(500).json({ error: 'Internal Server Error' });
}
}
What we are doing here:
- Checking whether the
accountfield was passed in the request body. If it is not, we will return a400error. - If the
accountfield was passed, we will call a new function,generateTx, and pass theaccountas an argument. This function will generate a transaction that will increment the counter (we will build that next). - If the transaction was successfully generated, we will return the serialized
transactionand amessage(defined in our constants) to the wallet. The wallet will display the message to the user and ask them to confirm the transaction.
Let's create the generateTx function. Add the following code to pay.ts, below your handler:
async function generateTx(account: string) {
// 1. Get the counter PDA
const [counterPda] = await PublicKey.findProgramAddress([Buffer.from(counterSeed)], programId);
// 2. Create the data buffer with the function selector
const data = getInstructionData(functionName);
// 3. Build the transaction to call the increment function
const tx = new Transaction();
const incrementIx = new TransactionInstruction({
keys: [
{ pubkey: counterPda, isWritable: true, isSigner: false },
],
programId: programId,
data
});
// 4. Set the latest blockhash and set the fee payer
const latestBlockhash = await getConnection().getLatestBlockhash();
tx.feePayer = new PublicKey(account);
tx.recentBlockhash = latestBlockhash.blockhash;
tx.add(incrementIx);
// 5. Serialize the transaction
const serializedTransaction = await tx.serialize({
verifySignatures: false,
requireAllSignatures: false,
});
// 6. Encode the transaction data as base64
const base64Transaction = Buffer.from(serializedTransaction).toString('base64');
return base64Transaction;
}
Let's walk through what we are doing here:
- We use the
counterSeedandprogramIdto generate the counter PDA. PDA derivation is async in web3.js v3, so weawaitit. We must pass this account into ourincrementfunction's transaction instruction. - We generate the data buffer for our
incrementfunction. We are using thegetInstructionDatafunction we defined earlier. - We are building the transaction. We are creating a new Transaction and adding a new TransactionInstruction. We pass the
counterPda(as a writeable, non-payer account) anddatawe generated in steps 1 and 2. We also pass theprogramIdwe defined in our constants. Note: If you are using your own program, you will need to update these values based on the context defined in your program. - We fetch and set the latest blockhash and set the user's wallet as the fee payer.
- We serialize the transaction. We are setting
verifySignaturesandrequireAllSignaturesasfalsebecause we are not signing the transaction. We will let the wallet handle that. In web3.js v3,serialize()returns a Promise of aUint8Array, so weawaitit. - Finally, we wrap the bytes in a
Buffer, encode them asbase64(Base64 is a common encoding format for binary data), and return the result to the wallet.
Great job! You just created a function that will generate a transaction that will increment the counter. Your backend is now ready to accept requests from the wallet. Let's test it out!
Run the following command to start the server:
npm run dev
# or
yarn dev
Then in a separate terminal window, run the following cURL script to make a GET request to the /api/pay endpoint:
curl -X GET http://localhost:3000/api/pay
This should return your label and icon that we defined in our constants. Now let's test out the POST request. Run the following cURL script to make a POST request to the /api/pay endpoint:
curl -X POST "http://localhost:3000/api/pay" \
-H "Content-Type: application/json" \
-d '{"account": "YOUR_WALLET_ADDRESS"}'
You should get a response back that looks similar to the following:
{
"transaction":"AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEDWvArSV39ujMKeO06xNO5Sx4ql4HJrmyWxnQjubHQp0iDxxzKuMff4tsV5PtxlzfcnR+CW+QUuiF+PqTIV/uDQ54RxxfTuGSHXAe+/I1AVzHOi5+zqX/ntgsd/DMy3V0VsyJ9ZUQHHexample/ZfFplpKKLcl3bpmiHJ0DTRUBAgexample",
"message":"Quicknode Demo - Increment Counter"
}
Nice job! You just created an API endpoint that generates a transaction to our customer program and returns it to the wallet. Now let's build the front end.
Create a Front End
Now that our backend is set up, let's create a front end. The front end will be a simple React app that:
- Generates a QR code on page load that triggers the scanning wallet to make a
GETrequest to our backend - Fetch, deserialize, and display our program's account data (count)
- Create a subscription to our program's account data to update the count in real time
Open /pages/index.tsx and replace the default content with the following:
import Head from 'next/head';
import Image from 'next/image';
import { useEffect, useState } from 'react';
import { createQR, encodeURL } from '@solana/pay';
export default function Home() {
const [qrCode, setQrCode] = useState<string>();
const [count, setCount] = useState<string>('');
useEffect(() => {
// TODO: Cal QR code generation
}, []);
const generateQr = async () => {
// TODO: Add QR code generation
}
return (
<>
<Head>
<title>Quicknode Solana Pay Demo: Quick Count</title>
<meta name="description" content="Quicknode Guide: Solana Pay" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex">
<h1 className='text-2xl font-semibold'>Solana Pay Demo: QuickCount</h1>
<h1 className='text-xl font-semibold'>Count: {count}</h1>
</div>
{qrCode && (
<Image
src={qrCode}
style={{ position: "relative", background: "white" }}
alt="QR Code"
width={200}
height={200}
priority
/>
)}
</main>
</>
);
}
The page uses React and Next.js to display the counter and Solana Pay to generate the QR code. The browser will read the counter through your API route.
Implement the QR Generator
We will generate a QR Code as a base64 string and store it in the qrCode state variable so that it will be passed into our Image component. First, let's build out our generateQr function. Add the following code to the generateQr function:
const generateQr = async () => {
const apiUrl = `${window.location.protocol}/${window.location.host}/api/pay`;
const label = 'label';
const message = 'message';
const url = encodeURL({ link: new URL(apiUrl), label, message });
const qr = createQR(url);
const qrBlob = await qr.getRawData('png');
if (!qrBlob) return;
const reader = new FileReader();
reader.onload = (event) => {
if (typeof event.target?.result === 'string') {
setQrCode(event.target.result);
}
};
reader.readAsDataURL(qrBlob);
}
Let's dig into what this function is doing:
- We define an
apiUrl, the URL of our backend API endpoint. We are using thewindow.locationobject to get the protocol and host of our current page. We are then appending/api/payto the end of the URL. This will allow our API to work on bothlocalhostand our deployed app (instead of hard coding the URL). - We define a
labelandmessage, effectively placeholders for this demo. - We are using the
encodeURLfunction from@solana/payto create a URL that will trigger the scanning wallet to make aGETrequest to our backend. We are passing in theapiUrlas anew URL. - Finally, we render the QR code as a base64 string and store it in the
qrCodestate variable.
Now that we have a function to generate our QR code let's call it when the component mounts. Add the following code to the useEffect hook:
useEffect(() => {
generateQr();
}, []);
This should render our QR code when the page loads. If you were to run your app now, you should see a QR code! Here's an example of what it should look like:

Fetch and Update the Count
Read the counter on the server through the same Quicknode connection. The counter account stores an 8-byte Anchor discriminator followed by the unsigned 64-bit count. Add this helper to pages/api/pay.ts.
async function getCount() {
const [counterPda] = await PublicKey.findProgramAddress([Buffer.from(counterSeed)], programId);
const account = await getConnection().getAccountInfo(counterPda);
if (!account || account.data.length < 16) return null;
return Buffer.from(account.data).readBigUInt64LE(8).toString();
}
In the GET branch, serve the counter only when the page requests it. The wallet still receives the payment metadata on its ordinary GET request.
if (req.query.count === 'true') {
try {
const count = await getCount();
if (count === null) return res.status(404).json({ error: 'Counter account not found' });
return res.status(200).json({ count });
} catch {
return res.status(502).json({ error: 'Unable to read the counter' });
}
}
res.status(200).json({ label, icon });
Replace the useEffect in your page with the following. It reads the count every three seconds and cancels the next poll when the component unmounts.
useEffect(() => {
generateQr();
let active = true;
let timer: ReturnType<typeof setTimeout>;
async function pollCount() {
try {
const response = await fetch('/api/pay?count=true');
if (!response.ok) throw new Error('Unable to read the counter');
const { count } = await response.json();
if (active) setCount(count);
} catch {
if (active) setCount('Unavailable');
} finally {
if (active) timer = setTimeout(pollCount, 3000);
}
}
pollCount();
return () => {
active = false;
clearTimeout(timer);
};
}, []);
Awesome work! Let's recap what we have built so far. We have:
- Created and deployed a Solana counter program using Anchor
- Built a Next.js front end that
- fetches our program counter
- refreshes the counter through the same-origin API
- generates and displays a QR code that can be scanned by Solana Pay-compatible wallet apps
- Created a backend API endpoint that can be called by Solana Pay-compatible wallet apps when they scan the wallet. The API endpoint follows the Solana Pay API spec and will send a transaction invoking our counter to the user's wallet for their signature.
Now, all we need to do is test it out.
Test the App
Open up a new terminal window and run the following command to start the Next.js development server:
npm run dev
# or
yarn dev
This will start the Next.js development server on port 3000. Navigate to http://localhost:3000 in your browser to view the app. You should see a QR code and the current count. Unfortunately, since our app is running on localhost, our wallet app on a separate device will not be able to access our API endpoint. We will need to deploy our app to a public URL to fix this. If you're so inclined, you can publish your project to a service like Vercel or Netlify (just make sure to secure your endpoints as we mentioned before). However, for the purposes of this guide, we will use ngrok, a tool that allows you to expose a local development server to the internet. After you have installed ngrok, you will need to follow the instructions to create an account and register your API key. Once you have done this, run the following command in your terminal:
ngrok http 3000
You should see a message that looks like this:
ngrok (Ctrl+C to quit)
Session Status online
Account your@email.com (Plan: Free)
Version 3.2.2
Region United States (us)
Latency -
Web Interface http://127.0.0.1:xxxx
Forwarding https://wxyz-00-123-456-789.ngrok.io -> http://loc
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
Follow the forwarding URL and click 'Visit Page.' You should be directed to a ngrok.io page running your local development server (NextJS app). You should see your final app running with a valid QR code and updated count:

Note: the QR code above is invalid because it points to a ngrok backend that is no longer active.
Now that we have our app running let's test it out. Open your Solana Pay-compatible wallet app (at present, there is a known issue with Phantom on Android-we will update when this has been fixed) and make sure the network is set to Devnet. Then, scan the QR code. You should be prompted to sign a transaction that will invoke our counter program:
Once you have approved the transaction and it is finalized by the network, you should see the count increment in your app!
If you want to reference our code in its entirety, check out our GitHub page here.
Wrap Up
Great work! That was a lot of work, but you have successfully built an integration between Solana Pay and a custom Solana program. The possibilities for this are endless. We can't wait to see what you come up with! If you're struggling for inspiration, check out this fun Tug-of-War game built by the Solana Foundation.
If you need help or want to share what you are building with Solana Pay, let us know on Discord or Twitter.
