Getresponse export all contacts with contact details

39 Views Asked by At

Need to fetch all contacts with details of each contact. Currently using API https://apireference.getresponse.com/ with endpoint like

  • /contacts -- To get contacts
  • For each contact hitting below url to get the contact details
    • /contacts/{contactId}

This takes lot of time as there are 2.6 million records/records.

Is there any alternative way, where we can get all contacts with details like in excel/csv file. It will be one time export.

1

There are 1 best solutions below

0
On

You can achieve like this way

import requests
import pandas as pd
from concurrent.futures import ThreadPoolExecutor, as_completed

contacts_endpoint = "https://api.getresponse.com/v3/contacts"

def get_contact_details(contact_id):
    try:
        response = requests.get(
            f"{contacts_endpoint}/{contact_id}",
            headers={"X-Auth-Token": "YOUR_API_KEY"},
        )
        response.raise_for_status()
        return response.json()
    except requests.HTTPError as http_err:
        print(f"HTTP error occurred: {http_err}")
    except Exception as err:
        print(f"An error occurred: {err}")

def fetch_all_contacts():
    try:
        response = requests.get(
            contacts_endpoint, headers={"X-Auth-Token": "YOUR_API_KEY"}
        )
        response.raise_for_status()
        return response.json()
    except requests.HTTPError as http_err:
        print(f"HTTP error occurred: {http_err}")
    except Exception as err:
        print(f"An error occurred: {err}")

contacts = fetch_all_contacts()
if contacts:
    contact_ids = [contact['contactId'] for contact in contacts]

    with ThreadPoolExecutor(max_workers=10) as executor:
        future_to_contact_id = {executor.submit(get_contact_details, cid): cid for cid in contact_ids}

        details = []
        for future in as_completed(future_to_contact_id):
            contact_detail = future.result()
            if contact_detail:
                details.append(contact_detail)

    df = pd.DataFrame(details)

    # Save the DataFrame to a CSV file
    df.to_csv('contacts_details.csv', index=False)

    print("Contacts have been saved to 'contacts_details.csv'")