Before you start
- Get an API keySign up for 100 free credits, no card. Keys start with
gs_live_. - Put it in the environmentEvery example reads it from
GROUNDSOCIAL_API_KEY:export GROUNDSOCIAL_API_KEY=gs_live_… - Set up Go
go mod init example
Call Facebook Ad Library company ads API from Go
GET /v1/facebook/ads/company. Save this as main.go and run go run main.go. It reads the items in ads and prints page_name for each.
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"time"
)
// The fields this program reads, as the recorded response has them.
type Page struct {
Ads []struct {
PageName string `json:"page_name"`
} `json:"ads"`
PageInfo struct {
EndCursor string `json:"end_cursor"`
} `json:"page_info"`
}
var client = &http.Client{Timeout: 60 * time.Second}
func fetch(q url.Values) (*Page, error) {
req, err := http.NewRequest("GET", "https://api.groundsocial.dev/v1/facebook/ads/company"+"?"+q.Encode(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("GROUNDSOCIAL_API_KEY"))
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
var e struct {
Error struct {
Code string `json:"code"`
Hint string `json:"hint"`
Retryable bool `json:"retryable"`
} `json:"error"`
}
json.NewDecoder(res.Body).Decode(&e)
return nil, fmt.Errorf("%s: %s", e.Error.Code, e.Error.Hint)
}
var page Page
if err := json.NewDecoder(res.Body).Decode(&page); err != nil {
return nil, err
}
return &page, nil
}
func main() {
q := url.Values{}
q.Set("page", "nike")
q.Set("status", "active")
page, err := fetch(q)
if err != nil {
log.Fatal(err)
}
for _, item := range page.Ads {
fmt.Println(item.PageName)
}
}The response
A real Facebook response to this call, recorded from the live source and shortened for display: arrays keep their first items and … marks every cut. The recorded page had 4 items in ads. Live calls return current data in the same shape.
{
"ads": [
{
"ad_archive_id": "1702938977100376",
"page_id": "15087023444",
"page_name": "Nike",
"is_active": true,
"start_date": 1744009200,
"end_date": 1795507200,
"publisher_platform": [
"FACEBOOK",
"INSTAGRAM",
"… 2 more"
],
"collation_count": null,
"collation_id": null,
"categories": [
"UNKNOWN"
],
"currency": "",
"spend": null,
"impressions_with_index": {
"impressions_text": null,
"impressions_index": -1
},
"reach_estimate": null,
"…": "16 more fields"
},
{
"ad_archive_id": "1057679207068133",
"page_id": "15087023444",
"page_name": "Nike",
"is_active": true,
"start_date": 1788159600,
"end_date": 1795507200,
"publisher_platform": [
"FACEBOOK",
"INSTAGRAM",
"… 1 more"
],
"collation_count": null,
"collation_id": null,
"categories": [
"UNKNOWN"
],
"currency": "",
"spend": null,
"impressions_with_index": {
"impressions_text": null,
"impressions_index": -1
},
"reach_estimate": null,
"…": "16 more fields"
},
"… 2 more"
],
"count": 14726,
"page_info": {
"has_next_page": true,
"end_cursor": "AQHTJv-Mqo-slQJ7WfNOmRiV6v0bUYhad318wL6WhbOznsuAvQeSDJW5CHoW-pg9DrWW"
}
}Top-level fields
| Field | Type | Description |
|---|---|---|
ads | FacebookAd[] | – |
count | integer | null | Meta’s estimate of how many ads match, on the first page (null on later pages). Meta caps it at 50,001. |
page_info | object | – |
Parameters
| Name | In | Type | Description |
|---|---|---|---|
page | query | string | Page id, Page URL or vanity name. Example: 15087023444. |
company_name | query | string | Advertiser name, when you don’t have the Page. Example: Nike. |
country | query | string | ISO 3166-1 alpha-2 country the ads reached (US, GB, DE…), or ALL (default). |
status | query | "active" | "inactive" | "all" | active (default), inactive, or all. One of active, inactive, all. |
media_type | query | "all" | "image" | "video" | "meme" | "image_and_meme" | "none" | all (default), image, video, meme (image with text), image_and_meme, none (text only). One of all, image, video, meme, image_and_meme, none. |
ad_type | query | "all" | "political_and_issue_ads" | "housing" | "employment" | "credit" | "financial" | all (default), or one of Meta’s special ad categories: political_and_issue_ads, housing, employment, credit, financial. One of all, political_and_issue_ads, housing, employment, credit, financial. |
sort_by | query | "total_impressions" | "relevancy_monthly_grouped" | "total_active_time" | total_impressions (default, high to low), relevancy_monthly_grouped (most recent), or total_active_time (longest-running first, within each page of 30). One of total_impressions, relevancy_monthly_grouped, total_active_time. |
language | query | string | Comma-separated ISO 639-1 languages of the ad text (en,es). |
publisher_platforms | query | string | Comma-separated placements: facebook, instagram, audience_network, messenger, whatsapp, threads, oculus. |
start_date | query | string | YYYY-MM-DD: only ads delivered on or after this day. |
end_date | query | string | YYYY-MM-DD: only ads delivered on or before this day. |
after | query | string | The previous page’s page_info.end_cursor; keep the other filters the same. |
Pagination
Each page carries the next cursor in page_info.end_cursor; pass it back as after and stop when it is empty. Every page is a separate call at 2 credits per call, so cap the pages you read. It replaces main in the program above and reuses its Page and fetch.
func main() {
q := url.Values{}
q.Set("page", "nike")
q.Set("status", "active")
for i := 0; i < 5; i++ { // each page is one call
page, err := fetch(q)
if err != nil {
log.Fatal(err)
}
for _, item := range page.Ads {
fmt.Println(item.PageName)
}
if page.PageInfo.EndCursor == "" {
break
}
q.Set("after", page.PageInfo.EndCursor)
}
}Errors and retries
Every error is JSON with error.code (stable: branch on it), message, hint (what to do next), docs_url and retryable. A failed call is never charged. Retry only when retryable is true, after the Retry-After header.
// fetch returns the API error as "code: hint". To retry the retryable ones
// (rate_limited, upstream_timeout), wait for Retry-After and call again:
for attempt := 0; attempt < 3; attempt++ {
page, err = fetch(q)
if err == nil {
break
}
time.Sleep(2 * time.Second)
}| Status | Meaning |
|---|---|
200 | A page of the advertiser’s ads, with the next cursor. |
400 | Invalid input. Nothing was charged. |
401 | Missing or invalid API key. |
402 | Not enough credits, a spend cap was reached, or payment is required. |
403 | The target is private, or the account is suspended. |
404 | The ad, Page or advertiser is not in the Meta Ad Library. |
429 | Too many requests. Wait for Retry-After seconds. |
503 | Facebook is temporarily unavailable. Retryable; nothing was charged. |
Every error code and what to do about it.
Pricing
| Cost | |
|---|---|
| Per call | 2 credits per call |
| 1,000 calls | $2.00 on the starter pack, $0.80 at volume |
| No key (MPP or x402) | $0.02 per call |
| Failed call | Refunded, never charged |
Every response has x-credits-charged and x-credits-remaining headers. Credits never expire. Packs and calculator.
From an AI agent (MCP)
The same call is the facebook_ads_company tool on the hosted MCP server, so Claude, Cursor or any MCP client can make it without code. Add the server once:
claude mcp add --transport http groundsocial https://api.groundsocial.dev/mcpThen ask for it in words (“Every ad of one advertiser (Facebook Page) in the Meta Ad Library, 30 a page, filterable”), or call the tool directly:
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "facebook_ads_company",
"arguments": {
"page": "nike",
"status": "active"
}
}
}MCP setup for Claude Desktop, Cursor, VS Code and ChatGPT.
Watch a competitor’s new ads
Call it daily with status=active&sort_by=relevancy_monthly_grouped and store the ad_archive_ids you have seen: anything new is a creative they just launched.
Questions
Page or company name?
A Page id is exact. A company name is matched through the Ad Library’s own advertiser search, so check page_id on the ads, or resolve it first with the advertiser search endpoint.
Keep going
- This call in Python, JavaScript, TypeScript, PHP, cURL.
- Facebook Ad Library company ads API reference: every field and status code.
- Facebook tutorials and all Go tutorials.
- Facebook API: every endpoint and price.
- How to scrape Facebook Ad Library search with Go: Search the Meta Ad Library by keyword: 30 ads a page with country, status, media, category, language, placement and date filters.
- How to scrape Facebook Ad Library ad details with Go: One Meta Ad Library ad with its details panel (EU reach by age and gender, payer, beneficiary).
- How to scrape Facebook Ad Library advertiser search with Go: Find advertisers (Facebook Pages) in the Meta Ad Library by name.
- How to scrape Facebook Ad Library advertiser profile with Go: An advertiser’s Meta Ad Library profile: Page and Instagram details, political spend, top ad.
- How to scrape Facebook Marketplace location search with Go: Find Facebook Marketplace places (cities, neighbourhoods, postcodes) with coordinates.
- How to scrape Facebook Marketplace search with Go: Search Facebook Marketplace listings near a place, with price, condition, delivery, date and sort filters.
Run it on your own data
One key for Facebook and 76 more platforms, over REST or MCP. Free credits on signup, no card.
Facebook is a trademark of Meta Platforms, Inc. We are not affiliated with or endorsed by Meta.