> ## Documentation Index
> Fetch the complete documentation index at: https://docs.peopledatalabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Enrich a List of LinkedIn Profiles

```python Python3
from time import sleep
import requests, json, csv

API_KEY = "YOUR API KEY"

PDL_URL = "https://api.peopledatalabs.com/v5/person/bulk"

data = {
            "requests": []
       }

all_records = []

HEADERS = {
    'Content-Type': "application/json",
    'X-api-key': API_KEY
}

with open('profiles.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter = ',')
    profile_count = 0
    for row in csv_reader:
        if profile_count > 0:            
            data['requests'] += [{'params': {'profile': row[0]}}]
        profile_count += 1
    print(f'Read {profile_count-1} profiles.')

json_responses = requests.post(
    PDL_URL,
    headers = HEADERS,
    json = data
).json()

for response in json_responses:
    if response["status"] == 200:
        record = response['data']
        all_records.extend([record])
    else:
        print("Bulk Person Enrichment Error:", response)
 
# Save profiles to csv (utility function)
def save_profiles_to_csv(profiles, filename, fields = [], delim = ','):
    # Define header fields
    if fields == [] and len(profiles) > 0:
        fields = profiles[0].keys()
    # Write csv file
    with open(filename, 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter = delim)
        # Write Header:
        writer.writerow(fields)
        # Write Body:
        count = 0
        for profile in profiles:
            writer.writerow([ profile[field] for field in fields ])
            count += 1
        print(f"Wrote {count} lines to: '{filename}'.")

# Use utility function to save profiles to csv    
csv_header_fields = ['full_name', 'job_title', 'job_company_name', 'job_company_website', 'work_email', 'mobile_phone', "linkedin_url"]
csv_filename = "enriched_profiles.csv"
save_profiles_to_csv(all_records, csv_filename, csv_header_fields)
```

```python Python3 SDK
from time import sleep
import requests, json, csv

# See https://github.com/peopledatalabs/peopledatalabs-python
from peopledatalabs import PDLPY

# Create a client, specifying an API key
CLIENT = PDLPY(
    api_key="YOUR API KEY",
)

data = {
            "requests": []
       }

all_records = []

with open('profiles.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter = ',')
    profile_count = 0
    for row in csv_reader:
        if profile_count > 0:            
            data['requests'] += [{'params': {'profile': row[0]}}]
        profile_count += 1
    print(f'Read {profile_count-1} profiles.')

json_responses = CLIENT.person.bulk(**data).json()

for response in json_responses:
    if response["status"] == 200:
        record = response['data']
        all_records.extend([record])
    else:
        print("Bulk Person Enrichment Error:", response)
 
# Save profiles to csv (utility function)
def save_profiles_to_csv(profiles, filename, fields = [], delim = ','):
    # Define header fields
    if fields == [] and len(profiles) > 0:
        fields = profiles[0].keys()
    # Write csv file
    with open(filename, 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter = delim)
        # Write Header:
        writer.writerow(fields)
        # Write Body:
        count = 0
        for profile in profiles:
            writer.writerow([ profile[field] for field in fields ])
            count += 1
        print(f"Wrote {count} lines to: '{filename}'.")

# Use utility function to save profiles to csv    
csv_header_fields = ['full_name', 'job_title', 'job_company_name', 'job_company_website', 'work_email', 'mobile_phone', "linkedin_url"]
csv_filename = "enriched_profiles.csv"
save_profiles_to_csv(all_records, csv_filename, csv_header_fields)
```

```json Response Example
Read 2 profiles.
Wrote 2 lines to: 'enriched_profiles.csv'
```

# Description

<!-- python@1-61 -->
<!-- python@1-56 -->

First, this recipe reads a list of LinkedIn profile urls from a CSV file. Then, it builds a Bulk Person Enrichment API request from them and executes the query. Finally, it saves the output to a CSV file.

# Initial Setup

<!-- python@1-6 -->
<!-- python@1-10 -->

If using the Python SDK, import the SDK library and initiate the client object. Otherwise, specify the Bulk Person Enrichment API endpoint.

# Initialize Arrays

<!-- python@8-11 -->
<!-- python@12-14 -->

We initialize the array that will contain the list of LinkedIn profile URLs that we want to enrich. We also initialize the array that will contain the output from our query.

# Set API Key

<!-- python@16 -->
<!-- python@9 -->

If using the Python SDK, we place our API key for authentication in the client class constructor.

Otherwise, we place our API key for authentication in the header fields of our request. Alternatively, we could have added the API Key into the input parameters.

# Read List of LinkedIn Profiles

<!-- python@19-26 -->
<!-- python@18-25 -->

We read a CSV file that contains a list of LinkedIn profiles and parse them.

Example CSV file: https://drive.google.com/file/d/1CrNXR6lCDEVY_Vvpn2QAWbidxhxCe3dg/view?usp=sharing

# Adds each row to the Requests Array

<!-- python@24 -->
<!-- python@23 -->

As we iterate through the list of LinkedIn profile URLs, we add the current URL to our requests array.

# Execute Bulk Person Enrichment API Query

<!-- python@28-32 -->
<!-- python@27 -->

We execute our Bulk Person Enrichment API query from the requests array that we have built.

# Iterate Through Query Responses

<!-- python@34-39 -->
<!-- python@29-34 -->

For each response returned by the Bulk Person Enrichment API, we add the results to our output array while checking for errors.

# Output Results to a CSV File

<!-- python@41-61 -->
<!-- python@36-56 -->

Finally, using our output array, we write a set of fields to a CSV file for each person in our list.