1. Tutorials
  2. How to scrape Facebook Ad Library ads by page

How to scrape Facebook Ad Library ads by page

Get Facebook Ad Library ads by page as JSON with one request. It costs 1 credit.

Get an API key; the examples use api.socialscrape.dev.

Before you start

  • An API key, sent in the x-api-key header. Keep it on your server.
  • For this endpoint you send one of id or company_name. The Facebook Ad Library ads by page page lists every parameter.

Step 1: Send the request

GET /v1/facebook/ad-library/company/ads. Pick your language; each one reads the key from SOCIALSCRAPE_API_KEY.

1curl -G "https://api.socialscrape.dev/v1/facebook/ad-library/company/ads" \2  -H "x-api-key: $SOCIALSCRAPE_API_KEY" \3  --data-urlencode 'id=15087023444' \4  --data-urlencode 'region=US'

Step 2: Read the response

Results are in data.items. Here is what one item gives you, from a recorded sample.

1for item in body["data"]["items"]:2    print(item["title"])3    print(item["id"])
SampleFields printed above
1{2  "title": "NikeSKIMS Outerwear",3  "id": "1741150940324450"4}

A field the platform does not give comes back as null. The full field list is on the endpoint page.

Step 3: Get the next page

Send the same request with the cursor you got back, until has_more is false. Each page costs its own price.

1import os2import requests34url = "https://api.socialscrape.dev/v1/facebook/ad-library/company/ads"5headers = {"x-api-key": os.environ["SOCIALSCRAPE_API_KEY"]}6params = {"id": "15087023444", "region": "US"}78items = []9while len(items) < 100:10    body = requests.get(url, headers=headers, params=params, timeout=60).json()11    if not body["ok"]:12        raise RuntimeError(body["error"]["code"])13    items += body["data"]["items"]14    if not body["has_more"]:15        break16    params["cursor"] = body["cursor"]1718print(len(items), "items")

Step 4: Handle errors

Check ok, then branch on error.code and error.retryable, never on the message.

1body = res.json()2if not body["ok"]:3    err = body["error"]4    if err["retryable"]:5        ...  # wait a moment and send the same request again6    else:7        raise RuntimeError(f'{err["code"]}: {err["message"]}')
CodeHTTPRetryMeaning
invalid_params400noA parameter is missing, unknown or has the wrong type.
not_found404noThe platform says this profile, post or item does not exist.

Other errors, such as unauthorized or rate_limited, are on the errors page.

With the SDK

The Python and TypeScript clients wrap all of this: typed errors, retries, and cursor paging. Install them with pip install socialscrape-sdk or npm install socialscrape.

1from socialscrape import SocialScrape23client = SocialScrape()  # reads SOCIALSCRAPE_API_KEY45for item in client.paginate(client.facebook.ad_library_company_ads, id="15087023444", region="US", max_items=100):6    print(item["title"])

What it costs

1 credit. A failed request costs nothing. Add max_age_days to reuse a cached result for 0 credits. Results are kept 30 days.

Next

Questions

How do I watch Facebook Ad Library ads by page for new items?

Create a monitor on this endpoint and get new items at a webhook, instead of polling. See the monitors reference.

Which language should I use?

Any. It is plain HTTP and JSON, so every language with an HTTP client works. The tabs show the same request nine ways.