node.jsapigoogle-cloud-functionsfacebook-chatbot

429 - {"status":"error","message":"Api max rps reached"} while sending request to manychat sendFlow API


I'm trying to setCustomField to Manychat subscribers using its API. The link to their API is:

https://api.manychat.com/swagger#/Subscriber/post_fb_subscriber_setCustomFieldByName

Below is the code for request :

   var rp = require("request-promise");
   var config = require("./../campaign_config.json");

module.exports = { setWaybillForUser :  async function setWaybillForUser(userid,waybill,dbRef){

        var options = { method: 'POST',
          uri: 'https://api.manychat.com/fb/subscriber/setCustomFieldByName',
          headers: 
           {'Cache-Control': 'no-cache',
             Accept: '*/*',
             Authorization: config.Manychat_token,  
             'Content-Type': 'application/json' },
          body: 
           { subscriber_id: parseInt(userid),
             field_name: 'waybill',
             field_value: waybill },
          json: true };

        try {
        const response = await rp(options);
        console.log(response);
        if (response.status == "success") {
            return dbRef.child('datanode').child(userid).update({
                "Order_Status": 3
            }).then(function () {
                //do nothing.
            }).catch(function (error) {
                console.log(error);
            });
        }
    }
    catch (err) {
        console.log(err.message);
    }
    }
};

I'm calling this function multiple times as there are many subscribers for whom the function has to run. The code where it's being called is:

dbRef.child("subData").once('value').then(function(snapshot){
        var data = snapshot.val();
        var keys = Object.keys(data);

        let promiseArray = [];
        let orderStatusArr =[];
        for(let i=0; i< keys.length; i++){

                    var order_status =  data[keys[i]].Order_Status;
                    var subid =  data[keys[i]].UserID;
                    var waybill =  data[keys[i]].Waybill_Number;
                    if(order_status == 1){
                        orderStatusArr.push({
                            "Order_Status":order_status,
                            "UserID": subid,
                            "Waybill_Number":waybill
                        });
                    }
        }

        for(let i=0; i<orderStatusArr.length;i++){
            var order_status =  orderStatusArr[i].Order_Status;
            var subid =  orderStatusArr[i].UserID;
            var waybill =  orderStatusArr[i].Waybill_Number;

            promiseArray.push(setWaybillsForUser.setWaybillForUser(subid,waybill,dbRef));
        }

        Promise.all(promiseArray).then(() => {
            // all done here
            response.send("success");
        }).catch(function(err) {
            console.log(err.message); 
          });

    })

When I run the code - it logs success for some requests while throwing the above for others. At a time it's able to run the code for about 10 subscribers successfully.


Solution

  • From this page:

    Is there any limit to a number of API calls?

    There is only a request per second limit for POST methods (per page):

    1. /fb/sending/sendFlow, /fb/sending/sendContent - 25 RPS;
    2. /fb/subscriber/addTag, /fb/subscriber/removeTag, /fb/subscriber/setCustomField - 10 RPS.

    So judging from this, and the symptoms you've posted, it looks like the second rule applies