How to Scrape Facebook Ad Library Company Ads with R
Get all ads a company is currently running using R. This comprehensive guide will walk you through the entire process, from setup to implementation.
Overview
What You'll Learn
- Setting up your R environment
- Installing the required HTTP client
- Authenticating with SociaVault API
- Making requests to Facebook Ad Library
- Handling responses and errors
What You'll Get
- Access to company ads data
- JSON formatted responses
- Real-time data access
- Scalable solution
- Error handling patterns
Prerequisites
1. API Key
First, you'll need a SociaVault API key to authenticate your requests.
2. Development Environment
Make sure you have the following installed:
- R installed
- A code editor (VS Code, Sublime, etc.)
- Command line interface access
Implementation
Step 1: Install HTTP Client
We'll use httr to make HTTP requests.
install.packages('httr')Step 2: API Implementation
Now let's make a request to the Facebook Ad Library API using R. Replace YOUR_API_KEY with your actual API key.
library(httr)
url <- "https://api.sociavault.com/facebook-ad-library/company-ads?companyName=Nike"
response <- GET(url, add_headers("x-api-key" = "YOUR_API_KEY", "Content-Type" = "application/json"))
content(response, "text")Testing Your Code
API Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| companyName | string | Yes | Example: Nike |
Expected Response
You should receive a structured JSON response containing the company ads data.
{
"results": [
{
"ad_archive_id": "1162496978867592",
"categories": [
"UNKNOWN"
],
"collation_count": 3,
"collation_id": "596215693307098",
"contains_digital_created_media": false,
"contains_sensitive_content": false,
"currency": "",
"end_date": 1740902400,
"entity_type": "PERSON_PROFILE",
"finserv_ad_data": {
"is_deemed_finserv": false,
"is_limited_delivery": false
},
"gated_type": "ELIGIBLE",
"has_user_reported": false,
"hidden_safety_data": false,
"hide_data_status": "NONE",
"impressions_with_index": {
"impressions_index": -1
},
"is_aaa_eligible": false,
"is_active": true,
"is_profile_page": false,
"page_id": "367152833370567",
"page_is_deleted": false,
"page_name": "Instagram",
"publisher_platform": [
"INSTAGRAM"
],
"regional_regulation_data": {
"finserv": {
"is_deemed_finserv": false,
"is_limited_delivery": false
},
"tw_anti_scam": {
"is_limited_delivery": false
}
},
"snapshot": {
"body": {
"text": "Bendito el día que seguí a mi compa en Insta que se la pasa viajando a Japón :) \n\n#SigueParaConectarMás"
},
"branded_content": {
"current_page_name": "Instagram",
"page_categories": [
"App page"
],
"page_id": "367152833370567",
"page_is_deleted": false,
"page_name": "Instagram",
"page_profile_pic_url": "https://scontent.ford4-1.fna.fbcdn.net/v/t39.35426-6/476964481_941793684729034_2496972419565164102_n.jpg?_nc_cat=110&ccb=1-7&_nc_sid=c53f8f&_nc_eui2=AeGVpQiwQpJ6NjZ_r0QRPnRnsuTPvj6vGu2y5M--Pq8a7SAmDJh_-ME6w2hYzLCRUn3LT7Xk_VjtQJJAtf8gTdOz&_nc_ohc=009FNKlR1YYQ7kNvgE4OQoA&_nc_oc=AdjGoiWjyE6GoQ0YBz1it-zYD9Ya-U6JCX3QtXqhWOdDYBx3smRXKKxnS3G2j5h-deg&_nc_zt=14&_nc_ht=scontent.ford4-1.fna&_nc_gid=AGD5XOOPTiOPhHk7r8BEiFX&oh=00_AYAXW-aXA8Z7HCplfCUjigGwUf-JGvvU4dNdNhvBtD3SUA&oe=67CAD8E6",
"page_profile_uri": "https://www.facebook.com/instagram/"
},
"caption": "fb.me",
"cta_text": "Learn more",
"cta_type": "LEARN_MORE",
"current_page_name": "Lalobri",
"display_format": "VIDEO",
"is_reshared": false,
"link_url": "http://fb.me/1171219090745394",
"page_categories": [
"Digital Creator"
],
"page_entity_type": "PERSON_PROFILE",
"page_id": "378515988682204",
"page_is_deleted": false,
"page_is_profile_page": false,
"page_like_count": 841,
"page_name": "Lalobri",
"page_profile_picture_url": "https://scontent.ford4-1.fna.fbcdn.net/v/t39.35426-6/476964663_1271062517296994_6188561291427920134_n.jpg?stp=dst-jpg_s60x60_tt6&_nc_cat=109&ccb=1-7&_nc_sid=c53f8f&_nc_eui2=AeHBDvekE5dmCbTMmrY_8dllpDaWWI2JTnykNpZYjYlOfEjNaSaXKVDSg6FoNna0hGRx8p2upelFdTWN2SrOSK_L&_nc_ohc=XXqHzPd3JDgQ7kNvgElJgXd&_nc_oc=Adhe-d488a4U2q_HQFh-j0BuIQ_dlf3bdGYta_SGr4qW9EIAX4VPYU4ZXdVzTv9ivhw&_nc_zt=14&_nc_ht=scontent.ford4-1.fna&_nc_gid=AGD5XOOPTiOPhHk7r8BEiFX&oh=00_AYDeX8vv11c8n7ix8wy2hYeDFey5jFN4xai6yhTBVyODkg&oe=67CAB11D",
"page_profile_uri": "https://www.facebook.com/61564084618363/",
"videos": [
{
"video_hd_url": "https://scontent.ford4-1.fna.fbcdn.net/v/t42.1790-2/476756415_1627858374528588_2478648308751007555_n.?_nc_cat=103&ccb=1-7&_nc_sid=c53f8f&_nc_eui2=AeGFMIZfO0nNUHrrpAL4MMzqfhQq5C78fIN-FCrkLvx8gyr-aQF3bhoax7jXSFnJnkAa1X2f8uP1cV86_ByXwtX-&_nc_ohc=5qsq6dJhb7MQ7kNvgEeqawf&_nc_oc=AdjFJYrqg7Ya7UtWHXJTesrcEKaeowoTpYRkxFm9axiczKD5h6mVJMIkrEMEipBTD4k&_nc_zt=28&_nc_ht=scontent.ford4-1.fna&_nc_gid=AGD5XOOPTiOPhHk7r8BEiFX&oh=00_AYC6J64vXFCTqKTPwk1P1-D768j9CaYhyGLRUjTfxfai7g&oe=67CAD6A9",
"video_preview_image_url": "https://scontent.ford4-1.fna.fbcdn.net/v/t39.35426-6/476771066_477630302068939_2551497936913391127_n.jpg?_nc_cat=109&ccb=1-7&_nc_sid=c53f8f&_nc_eui2=AeExU4fio3Vf0ebVdjAtnPMzkaPcEcZlo92Ro9wRxmWj3T3ipJseK8TTUisJMBn7XLwZPk51OuZPXETva7XYzJCw&_nc_ohc=1SXHcM6R6xsQ7kNvgF5AeKN&_nc_oc=AdiucCHMlY13qIvmCrBNLJ21VNKQFS0bpDmUDmYxIZDPdYeGDz_N-tLCcHSn2_D5JFk&_nc_zt=14&_nc_ht=scontent.ford4-1.fna&_nc_gid=AGD5XOOPTiOPhHk7r8BEiFX&oh=00_AYBIMeMGBjh3OZlsPD_qkBmLnft-dbpu266dYyaBe4cmIQ&oe=67CAAC25",
"video_sd_url": "https://scontent.ford4-1.fna.fbcdn.net/v/t42.1790-2/477158098_1295450461504411_8736217373119550609_n.mp4?_nc_cat=108&ccb=1-7&_nc_sid=c53f8f&_nc_eui2=AeGUQi87HBCOGRBlXIbI8Gv3a5wRn_rBGxVrnBGf-sEbFQ-PtvJoxaduBFa8IBqQr56VwCqO4zdvDGY8BpRuHIJX&_nc_ohc=A2mk0kLNTI4Q7kNvgEDHJfQ&_nc_oc=Adh5t_fnpyBDEzOprcnzBjAb6O8k1sBtjf2yDF0dXgmRNN1N8c8_ZTLZFHyVA8-K4z0&_nc_zt=28&_nc_ht=scontent.ford4-1.fna&_nc_gid=AGD5XOOPTiOPhHk7r8BEiFX&oh=00_AYCP-i5pWooCrOJpA0fdjwBGUbihKLtlBXf5MnKt3dRMKg&oe=67CAAB26",
"watermarked_video_hd_url": "",
"watermarked_video_sd_url": ""
}
]
},
"start_date": 1740643200
}
],
"cursor": "AQHRBUAxNmFlxBVMFL6uTb1ICFsV65O4SqmPbcVZJhiveBpPS1hFHAmL6yCJcF760cXP"
}Best Practices
Error Handling
Implement comprehensive error handling and retry logic for failed requests. Log errors properly for debugging.
Caching
Cache responses when possible to reduce API calls and improve performance. Consider data freshness requirements.
Security
Never expose your API key in client-side code. Use environment variables and secure key management practices.
Troubleshooting
Unauthorized
Check your API key is correct and properly formatted in the x-api-key header.
Payment Required
You ran out of credits and need to buy more.
Not Found
The resource (user, video, etc.) might not exist or be private.
Too Many Requests
You have exceeded your rate limit. Slow down your requests.
Frequently Asked Questions
How much does it cost to scrape Facebook Ad Library?
SociaVault offers 50 free API calls to get started. After that, pricing starts at $10 for 5k requests with volume discounts available.
Is it legal to scrape Facebook Ad Library data?
Scraping publicly available data is generally considered legal. We only collect public data that is accessible without logging in.
How fast can I scrape Facebook Ad Library?
Our API handles the rate limiting for you. You can make requests as fast as your plan allows.
What data format does the API return?
All API responses are returned in JSON format, making it easy to integrate with any programming language or application.
Related Tutorials
Company Ads in Other Languages
Company Ads with Node.jsCompany Ads with JavaScriptCompany Ads with PythonCompany Ads with PHPReady to Start Scraping?
Get started with 50 free API calls. No credit card required. Stop worrying about proxies and captchas.