library(httr)
library(jsonlite)
API_KEY <- "YOUR_API_KEY"
BASE_URL <- "https://api.scrapecreators.com"
ENDPOINT_PATH <- "/v1/facebook/adLibrary/search/ads"
scrape <- function() {
# Build query parameters
params <- list(
"query" = "running",
"sort_by" = "total_impressions",
"search_type" = "keyword_unordered",
"ad_type" = "all",
"country" = "ALL",
"language" = "EN",
"status" = "ACTIVE",
"media_type" = "ALL",
"start_date" = "2025-01-05",
"end_date" = "2025-02-16",
"cursor" = "AQHRYLVDkoMkvGv7yK1rcce-vJmKiKv330R4v3j9KHSOaYvmF1bq1QkotG0rgW8Fkrj-",
"trim" = "false"
)
tryCatch({
response <- GET(
url = paste0(BASE_URL, ENDPOINT_PATH),
add_headers(
"x-api-key" = API_KEY,
"Content-Type" = "application/json"
),
query = params
)
if (status_code(response) == 200) {
data <- fromJSON(rawToChar(response$content))
cat("Response:
")
print(data)
return(data)
} else {
stop(paste("HTTP", status_code(response), ":", rawToChar(response$content)))
}
}, error = function(e) {
cat("Error:", e$message, "
")
return(NULL)
})
}
# Usage
result <- scrape()