Facebook Ad Library aggregation

Search Meta's Ad Library by keyword, find an advertiser's page and list its ads, page through results and fetch one ad, with the Playground and the TypeScript SDK.

Meta's Ad Library is a public list of the ads shown on Facebook, Instagram and Meta's other apps, including ones that have stopped running. Social Fetch gives you the same data as JSON, so you can pull it into a spreadsheet, a dashboard or your own code. This recipe covers the two ways in: search every advertiser's ads by keyword, or find one advertiser and list theirs.

Meta's public Ad Library website searching for running shoes in the United States, with the search box labelled The same keyword search, and the first ad's Library ID, 1339489931715040, labelled The ad's id in the API
The API searches the same library as Meta's website. Each ad's Library ID is its id in the API.

Want to follow a competitor's ads without code?

The Ad Tracker in the Social Fetch app does this for you: type a brand's name and it saves every ad they run on Meta, LinkedIn, Google and TikTok, and checks for new ones on a schedule. Use this recipe when you want the raw data in your own code, or to search by keyword across advertisers.

What you'll need

  • A Social Fetch account with some credits. The Playground runs requests from your browser, so you can try everything on this page before writing code.
  • For code, an API key and the TypeScript SDK. Any language works with the raw API; the SDK just adds types.

What it costs

1 credit per request

Each of the four requests below costs 1 credit, and each page of results is a separate request. A search that finds nothing, or an advertiser lookup that comes back not_found, still costs its credit. Failed requests (errors) are free. Every response says what it cost in meta.creditsCharged. See Credits.

The four requests

ToRequestSDK method
Search all ads by keywordGET /v1/facebook/ad-library/ads/searchclient.facebook.searchAdLibraryAds()
Find an advertiser's pageGET /v1/facebook/ad-library/companies/searchclient.facebook.searchAdLibraryCompanies()
List one advertiser's adsGET /v1/facebook/ad-library/companies/adsclient.facebook.listCompanyAds()
Get one ad by its ID or linkGET /v1/facebook/ad-library/adsclient.facebook.getAdLibraryAd()

Search ads by keyword

Keyword search finds ads from any advertiser whose text matches your words, the same as typing in the Ad Library's search box.

  1. Open the Ad search endpoint in the Playground. The link fills in query with running shoes and sets country to US, status to active and mediaType to video.
  2. Click Send.
The Playground on Search Facebook Ad Library ads with running shoes in the query box, labelled Your keyword goes in query. The response on the right lists ads; the first ad's id is labelled The ad's Library ID and its page name, KURU Footwear, is labelled The advertiser
Each entry in data.ads is one ad: its Library ID, a link to it, the advertiser's page and the creative.

The same search in code:

import { SocialFetchClient } from "@socialfetch/sdk";

const client = new SocialFetchClient({
  apiKey: process.env.SOCIALFETCH_API_KEY!,
});

const result = await client.facebook.searchAdLibraryAds({
  query: "running shoes",
  country: "US",
  status: "active",
  mediaType: "video",
});

if (!result.ok) {
  console.error(result.error.code, result.error.requestId);
} else {
  for (const ad of result.value.data.ads) {
    console.log(ad.id, ad.page?.name, ad.startedAt, ad.url);
  }
  console.log("Credits:", result.value.meta.creditsCharged);
}

The filters you can add:

FilterValues
countryA two-letter country code such as US or GB, or ALL
statusactive, inactive or all
mediaTypeall, image, video, meme, image-and-meme or none
sortByimpressions or most-recent
searchTypekeyword-unordered (all the words, in any order) or exact-phrase
adTypeall or political-and-issue
languageA two-letter language code such as EN or ES
startDate, endDateDates as YYYY-MM-DD

Each ad's text, link and call to action are in creative. Its pictures and videos are in creative.images and creative.videos, but some ads keep theirs in creative.cards instead, so check both.

Get the next page

A search returns one page of ads. When there are more, data.page.hasMore is true and data.page.nextCursor holds a cursor. Send it back as cursor, with the same filters, to get the next page.

The end of the Playground response for the running shoes search, with nextCursor and hasMore true boxed and labelled Send nextCursor as cursor for the next page, and creditsCharged 1 labelled 1 credit per page
The end of every response says whether there's another page, and what this one cost.
const filters = {
  query: "running shoes",
  country: "US",
  status: "active",
  mediaType: "video",
} as const;

const ads = [];
let cursor: string | undefined;

// Stop after 3 pages (3 credits), or sooner if there are no more.
for (let page = 1; page <= 3; page++) {
  const result = await client.facebook.searchAdLibraryAds({ ...filters, cursor });
  if (!result.ok) {
    console.error(result.error.code, result.error.requestId);
    break;
  }

  ads.push(...result.value.data.ads);

  const { hasMore, nextCursor } = result.value.data.page;
  if (!hasMore || !nextCursor) break;
  cursor = nextCursor;
}

console.log(`Collected ${ads.length} ads`);

A popular keyword can match tens of thousands of ads, so always set a page limit. If you change a filter but keep an old cursor, the search returns an error saying the cursor doesn't match the current filters.

List one advertiser's ads

To see everything one brand runs, find its Facebook page first, then list that page's ads.

  1. Open Company search in the Playground, type the brand's name in query (here Saucony) and click Send.
  2. Find the right page in data.companies and copy its pageId.
The Playground's company search for Saucony, scrolled to the match whose country is null, with its pageId, 20662169998, labelled Copy the pageId, and its country labelled Big brands have a page per country: check which this is
Saucony has a page for each of many countries, all named Saucony. Use country, pageAlias and likes to pick the one you want.
  1. Open Company ads, paste the ID into pageId and click Send.
The Playground's company ads request for pageId 20662169998 with status active, and lookupStatus found at the top of the response labelled Check lookupStatus first
found means the page was found. Its ads follow in data.ads, which can be empty if nothing matches your filters.

In code:

const companies = await client.facebook.searchAdLibraryCompanies({
  query: "Saucony",
});

if (!companies.ok) {
  throw new Error(`${companies.error.code} (${companies.error.requestId})`);
}

// Look through the matches and pick the page you mean.
for (const company of companies.value.data.companies) {
  console.log(company.pageId, company.name, company.country, company.likes);
}

const result = await client.facebook.listCompanyAds({
  pageId: "20662169998",
  status: "active",
});

if (!result.ok) {
  console.error(result.error.code, result.error.requestId);
} else if (result.value.data.lookupStatus === "not_found") {
  console.log("No Ad Library page matched that pageId or name.");
} else {
  for (const ad of result.value.data.ads) {
    console.log(ad.id, ad.startedAt, ad.creative?.bodyText);
  }
}

listCompanyAds also takes companyName instead of pageId, which saves the company search. You don't choose the page, though: for Saucony it listed the ads of one of its country pages rather than its main one. Use pageId when it matters which page you get. It takes the same country, status, mediaType, language, sortBy, startDate, endDate and cursor as keyword search, and pages the same way.

Get one ad

When you already have an ad's Library ID, or its link from the Ad Library website, fetch just that ad:

const result = await client.facebook.getAdLibraryAd({
  url: "https://www.facebook.com/ads/library/?id=1339489931715040",
  // or: adId: "1339489931715040",
});

if (result.ok && result.value.data.lookupStatus === "found") {
  console.log(result.value.data.ad);
}

Add includeTranscript: true to get the words spoken in a video ad as text in data.transcript, when there's speech to transcribe. It's still 1 credit.

Without code: the Ad Tracker

If what you want is to keep an eye on a few competitors, the Ad Tracker does the whole job in the app. You type a brand's name, check the account it found on each ad library, and it collects their ads and checks for new ones from then on, saving each ad's images and video so they stay after the brand takes the ad down.

The Ad Tracker's Add a competitor page for Allbirds, with the Confirm the matches card spotlit and the Meta row, showing the Allbirds page with 267K likes, labelled The brand's Facebook page, found for you
The Ad Tracker finds the brand's page on each library for you to check.

It runs on the same credits. Track your first competitor walks through it.

Other ad libraries

The same kind of search works on other platforms' ad libraries:

On this page