Last updated 22 November 2026View as Markdown

Meta Ad Library API in Python

Search the Meta Ad Library from Python with requests: one search, every page with next_cursor, and retries when a platform asks you to wait.

This guide searches the Meta Ad Library from Python with Public Ads API, pages through the results and retries politely. It uses only requests. A sandbox key returns sample ads for free, so you can run it before you spend a credit.

1Get a key

Sign up and copy your sandbox key from the dashboard, then set it in your environment as PADS_API_KEY.

Sign up free and your sandbox key is filled in here and in every example. The examples read it from PADS_API_KEY.Log in
curl "https://api.publicadsapi.com/v1/meta/ads?q=running+shoes&country=GB" \
  -H "authorization: Bearer $PADS_API_KEY"

3Every page, with retries

A live call reads Meta as it runs, so set a 60 second timeout. On 503 (BUSY or UPSTREAM_BLOCKED) wait the Retry-After seconds and send it again. Pass next_cursor back as cursor until it is null.

all_ads.py
import os
import time
import requests

API = "https://api.publicadsapi.com"
HEADERS = {"Authorization": f"Bearer {os.environ['PADS_API_KEY']}"}


def get(path, params):
    for attempt in range(3):
        r = requests.get(API + path, params=params, headers=HEADERS, timeout=60)
        if r.status_code == 503:
            time.sleep(int(r.headers.get("Retry-After", "5")))
            continue
        r.raise_for_status()
        return r.json()
    r.raise_for_status()


def all_ads(params, max_pages=5):
    params = dict(params)
    for _ in range(max_pages):
        page = get("/v1/meta/ads", params)
        yield from page["ads"]
        if not page["next_cursor"]:
            return
        params["cursor"] = page["next_cursor"]


for ad in all_ads({"q": "running shoes", "country": "GB", "sort": "newest"}):
    print(ad["first_shown"], ad["advertiser"]["name"], ad["text"]["body"])

4What it costs

Each page with ads uses 1 credit; an empty page and an error use none. credits_used and credits_remaining come back with every answer. With a sandbox key nothing is charged. When it works, swap in a live key: nothing else changes.

Every account starts with 100 free credits a month and a sandbox key, no card. Get a free API key or read the quickstart.

Try it free

No password and no card. We email you a sign-in link.

Questions

Do I need a Meta developer account?

No. Public Ads API reads the public Ad Library page, so you only need its API key.

Read next

Search the ad libraries from your own code

Meta, Google and TikTok, one Ad record, free to start.