Skip to main content
This example demonstrates how to fetch all proxies associated with a specific proxy user ID. The process handles the three different access types: unrestricted access, service-restricted access, and proxy-restricted access.

Overview

The workflow varies based on the proxy user’s proxy_user_access_type:
  1. Retrieve the proxy user to check their access type
  2. If access_type: "all": Fetch all proxies in your account
  3. If access_type: "service_restricted": Search ACLs for service IDs, then fetch proxies from those services
  4. If access_type: "proxy_restricted": Search ACLs for proxy IDs, then fetch those specific proxies
import requests
import math

# API credentials
API_PUBLIC_KEY = "your_public_key"
API_PRIVATE_KEY = "your_private_key"
BASE_URL = "https://api.pingproxies.com/1.0/public"

# Headers for authentication
headers = {
    "X-API-Public-Key": API_PUBLIC_KEY,
    "X-API-Private-Key": API_PRIVATE_KEY
}

def get_all_proxies_for_proxy_user(proxy_user_id):
    """
    Get all proxies accessible by a specific proxy user.

    Args:
        proxy_user_id: The proxy user ID to get proxies for

    Returns:
        List of proxy objects
    """
    all_proxies = []

    # Step 1: Retrieve the proxy user to check access type
    proxy_user_url = f"{BASE_URL}/user/proxy_user/retrieve/{proxy_user_id}"
    proxy_user_response = requests.get(proxy_user_url, headers=headers)

    if proxy_user_response.status_code != 200:
        print(f"Error retrieving proxy user: {proxy_user_response.status_code}")
        return []

    proxy_user_data = proxy_user_response.json()["data"]
    access_type = proxy_user_data.get("proxy_user_access_type", "all")

    print(f"Proxy user {proxy_user_id} has access_type: {access_type}")

    # Step 2: Handle based on access type
    if access_type == "all":
        # Unrestricted access - get all proxies
        print("Fetching all proxies (unrestricted access)...")
        all_proxies = fetch_all_proxies()

    elif access_type == "service_restricted":
        # Service-restricted - get ACLs, then fetch proxies by service
        print("Fetching proxies from restricted services...")

        # Get ACL entries for this proxy user
        acl_url = f"{BASE_URL}/user/proxy_user_acl/search"
        acl_params = {"proxy_user_id": proxy_user_id}
        acl_response = requests.get(acl_url, params=acl_params, headers=headers)

        if acl_response.status_code != 200:
            print(f"Error retrieving ACLs: {acl_response.status_code}")
            return []

        acls = acl_response.json().get("data", [])
        service_ids = [acl["service_id"] for acl in acls if "service_id" in acl]

        print(f"Found {len(service_ids)} service(s): {service_ids}")

        # Fetch proxies for each service
        for service_id in service_ids:
            proxies = fetch_proxies_by_service(service_id)
            all_proxies.extend(proxies)

    elif access_type == "proxy_restricted":
        # Proxy-restricted - get ACLs, then fetch specific proxies
        print("Fetching specific restricted proxies...")

        # Get ACL entries for this proxy user
        acl_url = f"{BASE_URL}/user/proxy_user_acl/search"
        acl_params = {"proxy_user_id": proxy_user_id}
        acl_response = requests.get(acl_url, params=acl_params, headers=headers)

        if acl_response.status_code != 200:
            print(f"Error retrieving ACLs: {acl_response.status_code}")
            return []

        acls = acl_response.json().get("data", [])
        proxy_ids = [acl["proxy_id"] for acl in acls if "proxy_id" in acl]

        print(f"Found {len(proxy_ids)} proxy/proxies")

        # Fetch each specific proxy
        for proxy_id in proxy_ids:
            proxy = fetch_proxy_by_id(proxy_id)
            if proxy:
                all_proxies.append(proxy)

    print(f"\nTotal proxies accessible: {len(all_proxies)}")
    return all_proxies

def fetch_all_proxies():
    """Fetch all proxies with pagination."""
    all_proxies = []
    page = 1
    per_page = 100

    while True:
        search_url = f"{BASE_URL}/user/proxy/search"
        params = {"page": page, "per_page": per_page}

        response = requests.get(search_url, params=params, headers=headers)
        if response.status_code != 200:
            print(f"Error on page {page}: {response.status_code}")
            break

        data = response.json()
        proxies = data.get("data", [])
        all_proxies.extend(proxies)

        total_count = data.get("total_count", 0)
        total_pages = math.ceil(total_count / per_page)

        if page >= total_pages:
            break
        page += 1

    return all_proxies

def fetch_proxies_by_service(service_id):
    """Fetch all proxies for a specific service."""
    all_proxies = []
    page = 1
    per_page = 100

    while True:
        search_url = f"{BASE_URL}/user/proxy/search"
        params = {"service_id": service_id, "page": page, "per_page": per_page}

        response = requests.get(search_url, params=params, headers=headers)
        if response.status_code != 200:
            print(f"Error fetching service {service_id} page {page}: {response.status_code}")
            break

        data = response.json()
        proxies = data.get("data", [])
        all_proxies.extend(proxies)

        total_count = data.get("total_count", 0)
        total_pages = math.ceil(total_count / per_page)

        if page >= total_pages:
            break
        page += 1

    return all_proxies

def fetch_proxy_by_id(proxy_id):
    """Fetch a specific proxy by ID."""
    proxy_url = f"{BASE_URL}/user/proxy/retrieve/{proxy_id}"
    response = requests.get(proxy_url, headers=headers)

    if response.status_code != 200:
        print(f"Error fetching proxy {proxy_id}: {response.status_code}")
        return None

    return response.json().get("data")

# Example usage
if __name__ == "__main__":
    proxy_user_id = "your_proxy_user_id"
    proxies = get_all_proxies_for_proxy_user(proxy_user_id)

    if proxies:
        print(f"\nFirst proxy example:")
        print(f"  ID: {proxies[0].get('proxy_id')}")
        print(f"  IP: {proxies[0].get('proxy_ip_address')}")
        print(f"  Type: {proxies[0].get('proxy_type')}")

How It Works

1. Check Access Type

First, retrieve the proxy user to determine their proxy_user_access_type:
{
  "proxy_user_id": "seo_team",
  "proxy_user_access_type": "service_restricted"
}

2. Three Different Workflows

For access_type: "all":
  • Fetch all proxies from your account using the proxy search endpoint
  • No ACL lookup needed
For access_type: "service_restricted":
  • Search Proxy User ACLs for this proxy user
  • Extract all service_id values from the ACL entries
  • Fetch proxies for each service using the proxy search endpoint with service_id filter
For access_type: "proxy_restricted":
  • Search Proxy User ACLs for this proxy user
  • Extract all proxy_id values from the ACL entries
  • Fetch each individual proxy using the proxy retrieve endpoint

3. Pagination Handling

For unrestricted and service-restricted access, the code handles pagination to ensure all proxies are retrieved, even when there are many results.