Using TypeScript

How to Scrape Facebook Ad Library Search Companies with TypeScript

Search for companies and get their Ad Library page ID using TypeScript. This comprehensive guide will walk you through the entire process, from setup to implementation.

Overview

What You'll Learn

  • Setting up your TypeScript environment
  • Installing the required HTTP client
  • Authenticating with SociaVault API
  • Making requests to Facebook Ad Library
  • Handling responses and errors

What You'll Get

  • Access to search companies data
  • JSON formatted responses
  • Real-time data access
  • Scalable solution
  • Error handling patterns

Prerequisites

1. API Key

First, you'll need a SociaVault API key to authenticate your requests.

2. Development Environment

Make sure you have the following installed:

  • TypeScript installed
  • A code editor (VS Code, Sublime, etc.)
  • Command line interface access

Implementation

Step 1: Install HTTP Client

We'll use axios to make HTTP requests.

bash
npm install axios

Step 2: API Implementation

Now let's make a request to the Facebook Ad Library API using TypeScript. Replace YOUR_API_KEY with your actual API key.

typescript
import axios from 'axios';

const API_KEY: string = 'YOUR_API_KEY';

interface ResponseData {
  [key: string]: any;
}

async function scrape(): Promise<ResponseData | void> {
  try {
    const response = await axios.get<ResponseData>(`https://api.sociavault.com/facebook-ad-library/search-companies?query=Nike`, {
      headers: {
        'x-api-key': API_KEY,
        'Content-Type': 'application/json'
      }
    });

    console.log('Response:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error:', error);
  }
}

scrape();

Testing Your Code

API Parameters

ParameterTypeRequiredDescription
querystringYesExample: Nike

Expected Response

You should receive a structured JSON response containing the search companies data.

json
{
  "searchResults": [
    {
      "page_id": "51212153078",
      "category": "Product/service",
      "image_uri": "https://scontent.ford4-1.fna.fbcdn.net/v/t39.30808-1/387181570_863325755161415_3589774989337788111_n.jpg?stp=dst-jpg_s200x200_tt6&_nc_cat=103&ccb=1-7&_nc_sid=418b77&_nc_ohc=SzL6LoxpjrMQ7kNvgF5Uyy3&_nc_oc=AdgIflwSC-gA-77yFJHPKOC1dEGUI7YqvKYkmll9n5n8BcvLzRKToufpLG-068BnjE4&_nc_zt=24&_nc_ht=scontent.ford4-1.fna&_nc_gid=AYuU655BQ_HFcmz-fz9agWZ&oh=00_AYDD0ELO2MQikn7TBtxiTe7n64HX6iKiI5ofiI86BQ_jYw&oe=67CAC605",
      "likes": 41136495,
      "verification": "BLUE_VERIFIED",
      "name": "Nike Football",
      "entity_type": "PERSON_PROFILE",
      "ig_username": "nikefootball",
      "ig_followers": 46451228,
      "ig_verification": true,
      "page_alias": "nikefootball",
      "page_is_deleted": false
    }
  ]
}

Best Practices

Error Handling

Implement comprehensive error handling and retry logic for failed requests. Log errors properly for debugging.

Caching

Cache responses when possible to reduce API calls and improve performance. Consider data freshness requirements.

Security

Never expose your API key in client-side code. Use environment variables and secure key management practices.

Troubleshooting

401

Unauthorized

Check your API key is correct and properly formatted in the x-api-key header.

402

Payment Required

You ran out of credits and need to buy more.

404

Not Found

The resource (user, video, etc.) might not exist or be private.

429

Too Many Requests

You have exceeded your rate limit. Slow down your requests.

Frequently Asked Questions

How much does it cost to scrape Facebook Ad Library?

SociaVault offers 50 free API calls to get started. After that, pricing starts at $10 for 5k requests with volume discounts available.

Is it legal to scrape Facebook Ad Library data?

Scraping publicly available data is generally considered legal. We only collect public data that is accessible without logging in.

How fast can I scrape Facebook Ad Library?

Our API handles the rate limiting for you. You can make requests as fast as your plan allows.

What data format does the API return?

All API responses are returned in JSON format, making it easy to integrate with any programming language or application.

Related Tutorials

Ready to Start Scraping?

Get started with 50 free API calls. No credit card required. Stop worrying about proxies and captchas.