from facebook_business.api import FacebookAdsApi, FacebookRequest from facebook_business.adobjects.adaccount import AdAccount # Initialize the API with your access token and app secret access_token = 'your_access_token' app_secret = 'your_app_secret' FacebookAdsApi.init(access_token=access_token, app_secret=app_secret) # Create a batch of API requests requests = [ AdAccount('act_123456').get_insights(), AdAccount('act_789012').get_ad_sets() ] # Add the requests to a batch batch = [] for request in requests: batch.append(request.get_api_request()) FacebookRequest.add_to_batch(batch) # Execute the batch of requests responses = FacebookRequest.execute_batch() # Print the responses for response in responses: print(response.json())
from facebook_business.api import FacebookAdsApi, FacebookRequest from facebook_business.adobjects.adaccount import AdAccount from facebook_business.adobjects.campaign import Campaign # Initialize the API with your access token and app secret access_token = 'your_access_token' app_secret = 'your_app_secret' FacebookAdsApi.init(access_token=access_token, app_secret=app_secret) # Create a batch of API requests campaign1 = Campaign('123456').api_get() campaign2 = Campaign('789012').api_get() requests = [ AdAccount('act_123456').get_insights(), campaign1.get_ads(), campaign2.get_ads() ] # Add the requests to a batch batch = [] for request in requests: batch.append(request.get_api_request()) FacebookRequest.add_to_batch(batch) # Execute the batch of requests responses = FacebookRequest.execute_batch() # Print the responses for response in responses: print(response.json())In this example, we create a batch of API requests that include getting insights for an ad account and getting ads for two different campaigns. We then create a batch and add the requests to it using the FacebookRequest.add_to_batch() method. Finally, we execute the batch of requests and print the responses. In conclusion, the FacebookRequest.add_to_batch() method is a useful method provided by the python facebook_business.api package library. It allows developers to group several API requests into a single batch, which can save time and reduce the number of requests that need to be made to the API.