upi-qr-code-generator
v1.0.0
Published
Generate UPI payment URIs and QR codes with a pre-filled amount in Node.js and browsers.
Maintainers
Readme
UPI QR Code Generator
Generate UPI payment URIs and QR codes with a pre-filled INR amount. The library works in Node.js and browser applications using a bundler, and ships with ESM, CommonJS, and TypeScript declarations.
This package generates payment requests. It does not initiate, verify, or reconcile payments and never handles a payer's UPI PIN.
Features
- Creates standards-shaped
upi://payURIs - Includes and validates a fixed payment amount
- Generates PNG data URLs or SVG strings
- Escapes payee names, notes, and references safely
- Supports merchant transaction references and merchant category codes
- Provides TypeScript types and works from JavaScript
Install
Once published to npm:
npm install upi-qr-code-generatorTo install the current GitHub version:
npm install github:techpool/upi-qr-code-generatorNode.js 20 or newer is supported.
Quick start
import {
createUpiPaymentUri,
generateUpiQrDataUrl,
} from "upi-qr-code-generator";
const payment = {
payeeVpa: "merchant@bank",
payeeName: "Example Store",
amount: "149.00",
transactionRef: "ORDER-123",
transactionNote: "Order 123",
};
const uri = createUpiPaymentUri(payment);
console.log(uri);
const qrDataUrl = await generateUpiQrDataUrl(payment);
// Browser example
document.querySelector("img").src = qrDataUrl;The generated URI looks like this:
upi://pay?pa=merchant%40bank&pn=Example%20Store&am=149.00&cu=INR&tr=ORDER-123&tn=Order%20123Generate SVG
import { generateUpiQrSvg } from "upi-qr-code-generator";
const svg = await generateUpiQrSvg({
payeeVpa: "merchant@bank",
payeeName: "Example Store",
amount: "499.50",
transactionRef: "ORDER-456",
});In Node.js, save the returned SVG with node:fs/promises. A complete example
is available in examples/generate-svg.mjs.
CommonJS
const { generateUpiQrDataUrl } = require("upi-qr-code-generator");
const qrDataUrl = await generateUpiQrDataUrl({
payeeVpa: "merchant@bank",
payeeName: "Example Store",
amount: "99.00",
});API
createUpiPaymentUri(payment)
Returns the encoded upi://pay URI.
generateUpiQrDataUrl(payment, qrOptions?)
Returns a promise containing a PNG data:image/png;base64,... URL.
generateUpiQrSvg(payment, qrOptions?)
Returns a promise containing an SVG string.
normalizeUpiAmount(amount)
Validates a positive amount with at most two decimal places and returns an exact two-decimal string. It does not silently round.
Payment options
| Option | Type | Required | UPI field | Description |
| --- | --- | --- | --- | --- |
| payeeVpa | string | Yes | pa | Payee UPI virtual payment address |
| payeeName | string | Yes | pn | Name shown in the payer's app |
| amount | string \| number | Yes | am | Positive INR amount, maximum two decimal places |
| transactionRef | string | No | tr | Unique merchant reference, maximum 35 characters |
| transactionNote | string | No | tn | Payment note |
| merchantCode | string | No | mc | Merchant category code supplied by an acquirer |
Use decimal strings such as "149.00" for money. JavaScript numbers are
accepted for convenience, but values with more than two decimal places are
rejected rather than rounded.
For dynamic merchant QR codes, supply a unique transactionRef for each
payment. Your acquiring bank or PSP may require additional fields or impose
stricter rules.
QR options
{
errorCorrectionLevel?: "low" | "medium" | "quartile" | "high" | "L" | "M" | "Q" | "H";
width?: number;
margin?: number;
scale?: number;
color?: {
dark?: string;
light?: string;
};
}Defaults are a width of 512 pixels, margin of 2 modules, and error correction
level M.
Payment confirmation
A successful QR scan is not proof of payment. To confirm payments, use the
status, reconciliation, or webhook APIs provided by your bank, PSP, or payment
gateway. Match their confirmed transaction to your unique transactionRef.
Always show the expected payee and amount in your checkout UI. The payer should verify those details in their UPI app before authorizing the transaction.
UPI compatibility
UPI app behavior can vary. Test production QR codes with the UPI apps your
customers use and follow the requirements supplied by your acquiring bank or
PSP. NPCI's merchant QR interoperability circular identifies pa and pn as
critical fields and requires am and tr for dynamic merchant QRs.
Development
npm install
npm test
npm run typecheck
npm run buildRun the complete verification suite with:
npm run check