Create a Firebase Cloud Function using onRequest (v2). Store your Paystack secret key with firebase functions:secrets:set PAYSTACK_SECRET_KEY. In the function, access it with process.env.PAYSTACK_SECRET_KEY, call the Paystack initialize endpoint using native fetch (Node 18+), and return the authorization URL to your frontend.
# Initialize Cloud Functions if not already done
firebase init functions
# Choose TypeScript, Node 18
# Store Paystack secret key as a Firebase secret
firebase functions:secrets:set PAYSTACK_SECRET_KEY
# Install dependencies
cd functions
npm install cors
npm install --save-dev @types/cors
// functions/src/index.ts
import { onRequest } from "firebase-functions/v2/https";
import { defineSecret } from "firebase-functions/params";
import * as cors from "cors";
const paystackSecretKey = defineSecret("PAYSTACK_SECRET_KEY");
const corsHandler = cors({ origin: true });
export const initializePayment = onRequest(
{ secrets: [paystackSecretKey] },
async (req, res) => {
corsHandler(req, res, async () => {
if (req.method !== "POST") {
res.status(405).send("Method not allowed");
return;
}
const secretKey = paystackSecretKey.value();
const { email, amount } = req.body;
if (!email || !amount) {
res.status(400).json({ error: "email and amount are required" });
return;
}
const reference = "fcn_" + Date.now() + "_" + Math.random().toString(36).slice(2, 8);
try {
const paystackRes = await fetch(
"https://api.paystack.co/transaction/initialize",
{
method: "POST",
headers: {
Authorization: "Bearer " + secretKey,
"Content-Type": "application/json",
},
body: JSON.stringify({
email,
amount: Math.round(Number(amount) * 100),
reference,
}),
}
);
const data = await paystackRes.json();
if (!data.status) {
res.status(400).json({ error: data.message });
return;
}
res.json({
authorization_url: data.data.authorization_url,
access_code: data.data.access_code,
reference: data.data.reference,
});
} catch (error) {
res.status(500).json({ error: "Failed to initialize payment" });
}
});
}
);
Get weekly developer tips
Join 25,000+ developers. Practical guides, job tips, and new content — straight to your inbox.
No spam. Unsubscribe anytime.
# Deploy the function
firebase deploy --only functions:initializePayment
# Get the function URL from the console or CLI output:
# https://<region>-<project-id>.cloudfunctions.net/initializePayment
// Frontend call
async function initializePayment(email: string, amount: number) {
var response = await fetch(
"https://<region>-<project-id>.cloudfunctions.net/initializePayment",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, amount }),
}
)
var data = await response.json()
// data.authorization_url — redirect or open in WebView
window.location.href = data.authorization_url
}
This guide is part of the Paystack integration guides by language and framework series.
KES 2,999
The definitive guide to building payment systems with Paystack — from your first transaction to production-grade payment infrastructure across Africa. 9 modules, 47 lessons. Free preview available.
Start Paystack Course