facebookfacebook-graph-apifacebook-marketing-api

How do I update the daily budget for multiple adsets within a single API call to Meta API?


I'm trying to update the daily_budget parameter for multiple adsets within a singular call. I've been able to get this working in batches, but unfortunately, according to the batching documentation:

Batch requests are limited to 50 requests per batch. Each call within the batch is counted separately for the purposes of calculating API call limits and resource limits. For example, a batch of 10 API calls will count as 10 calls and each call within the batch contributes to CPU resource limits in the same manner.

Which is a problem for larger accounts, wherein I may need to update a very large number of adsets. Therefore, I wish to make a function to perform this task in a single API call, similar to how one can get daily_budgets for multiple adsets within a single Meta API call:

class MetaAPI:
    def __init__(
        self, access_token: str, ad_account_id_val: str, sqlapi: SQLAPI, test=False
    ):
        self.access_token = access_token
        self.ad_account_id = "act_" + ad_account_id_val
        self.sqlapi = sqlapi
        self.test = test

        self.root_url = "https://graph.facebook.com/v18.0/"
        self.campaigns_ext = "/campaigns"
        self.adsets_ext = "/adsets"
        self.insights_ext = "/insights"

def fetch_group_adset_insights(
        self, adset_id_list: List[str], insights_list: List[str]
    ) -> Dict[str, Tuple[Dict[str, float], str]]:
        params_insights_get = {
            "access_token": self.access_token,
            "level": "adset",
            "filtering": str(
                [
                    {
                        "field": "adset.id",
                        "operator": "IN",
                        "value": adset_id_list,
                    }
                ]
            ),
            "fields": ",".join(
                insights_list + ["optimization_goal", "adset_id"]
            ),  # 'objective,impressions',
            "time_range[since]": f"{self.date_start}",
            "time_range[until]": f"{self.date_end}",
        }

        response_insights_get = rq.get(
            self.root_url + self.ad_account_id + self.insights_ext,
            params=params_insights_get,
        )

I was able to do this on a per-adset basis with the following function:

def update_object_budgets(self, object_id: str, budget: float):
        """
        Updates the budget of the object (adset or campaign) to the given value.
        """

        url = self.root_url + object_id
        params = {"access_token": self.access_token, "daily_budget": f"{budget}"}

        response = rq.post(url, data=params)

        return response.status_code == 200  # 200: success

but this doesn't seem to translate for multiple adsets:

    def update_objects_budgets(
        self, object_id_list: List[str], budget_list: List[float]
    ):
        """
        Updates the budget of the object (adset or campaign) to the given value.
        """

        url = self.root_url
        params = {
            "access_token": self.access_token,
            "adset_id": object_id_list,
            "daily_budget": budgets_list,
        }

        response = rq.post(url, data=params)

        return response.status_code == 200  # 200: success

since it returns the following JSON response: {'error': {'message': 'Unsupported post request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api', 'type': 'GraphMethodException', 'code': 100, 'error_subcode': 33, 'fbtrace_id': 'APtBihvkXqpal9gGI9wCwXZ'}}

How do I solve this problem?


Solution

  • Therefore, I wish to make a function to perform this task in a single API call

    That is, in all likelihood, not going to help.

    Marketing API has its own set of rules in terms of rate limiting, but I am pretty sure that what https://developers.facebook.com/docs/graph-api/overview/rate-limiting/#faq says, applies here as well:

    What do we consider an API call?
    All calls count towards the rate limits, not just individual API requests. For example, you can make a single API request specifying multiple IDs, but each ID counts as one API call.