> 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.

Web version: https://publicadsapi.com/guides/meta-ad-library-api-python · Sign up: https://publicadsapi.com/signup?ref=md · Last updated: 2026-11-22

# 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](https://publicadsapi.com/signup) and your sandbox key is filled in here and in every example. The examples read it from `PADS_API_KEY`.[Log in](https://publicadsapi.com/login)

## 2One search

```
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](https://publicadsapi.com/signup) or read the [quickstart](https://publicadsapi.com/docs/quickstart).

Questions

## 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

-   [Quickstart](https://publicadsapi.com/docs/quickstart)
-   [How to see a competitor’s Facebook and Instagram ads](https://publicadsapi.com/guides/competitors-facebook-ads)
-   [Errors and retries](https://publicadsapi.com/docs/errors)

## Search the ad libraries from your own code

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

[Get a free API key](https://publicadsapi.com/signup) · [Read the quickstart](https://publicadsapi.com/docs/quickstart)
