Use the Batch API for multiple requests
mainThe Batch API allows you to group multiple API calls into a single request to reduce latency. Wrap your operations in a FacebookAds::Batch.with_batch block and call .execute on the resulting batch object.
Experimental: Dependencies within a batch You can create dependencies between requests using JSONPath references. For example, you can create a campaign and then immediately create an ad set that references that campaign's ID within the same batch block. The SDK handles the reference so the second request waits for the first to provide the ID.
ad_account = FacebookAds::AdAccount.get('act_<YOUR_AD_ACCOUNT_ID>')
batch = FacebookAds::Batch.with_batch do
10.times.map do |n|
ad_account.campaigns.create({
name: 'My Test Campaign #' + n,
objective: 'CONVERSIONS',
status: 'PAUSED',
})
end
end
batch.execute
# Example with dependencies
batch_with_deps = FacebookAds::Batch.with_batch do
campaign = ad_account.campaigns.create({
name: 'My Test Campaign',
objective: 'CONVERSIONS',
status: 'PAUSED',
})
ad_account.adsets.create({
name: 'My AdSet',
campaign_id: campaign.id, # Uses JSONPath reference internally
})
end
batch_with_deps.execute