Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Razorpay NPM Package Issue in Cloud Function: TypeError: Cannot Read Properties of Undefined

Hello Community,

I'm facing an issue while using the Razorpay NPM package in a Google Cloud Function. When calling the create order function using the Razorpay instance, the function throws the following error:

error creating order TypeError: Cannot read properties of undefined (reading 'error')
at normalizeError (/workspace/node_modules/razorpay/dist/api.js:41:22)
at bound (node:domain:432:15)
at runBound (node:domain:443:12)
at tryCatcher (/workspace/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/workspace/node_modules/bluebird/js/release/promise.js:547:31)
at Promise._settlePromise (/workspace/node_modules/bluebird/js/release/promise.js:604:18)
at Promise._settlePromise0 (/workspace/node_modules/bluebird/js/release/promise.js:649:10)
...

Environment:
Node.js version: (mention your Node.js version, e.g., v20.0.0)
Razorpay NPM package version: (mention your package version, e.g., 2.9.2 or 2.9.5)
Running in: Google Cloud Functions

Thank you for the help!

0 1 149
1 REPLY 1

Hi @Accredian,

Welcome to Google Cloud Community!

It seems that your Google Cloud Function is experiencing the error "Cannot read properties of undefined (reading 'error')" when invoking razorpay.orders.create(). This usually occurs because of wrong API credentials, network problems, or an unforeseen API response.

Here are some Quick Fix that might help:

  1. Check API Credentials – Make sure your Razorpay key/secret are correctly set:
    const razorpay = new Razorpay({
        key_id: process.env.RAZORPAY_KEY_ID,
        key_secret: process.env.RAZORPAY_KEY_SECRET
    });
  2. Verify Request Format – Ensure the payload follows Razorpay’s API:
    const order = await razorpay.orders.create({
        amount: 50000, // in paise (₹500)
        currency: "INR",
        receipt: "order_rcptid_11"
    });
  3. Improve Error Logging – Catch full error details:
    try {
        const order = await razorpay.orders.create(orderDetails);
        console.log("Order Created:", order);
    } catch (error) {
        console.error("Error:", error.response?.data || error);
    }
    
  4. Check Network/VPC – If using a VPC, ensure internet access.
  5.  Update Node.js & Razorpay – Use Node 18+ and update the package:
    npm install razorpay@latest

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.