> ## 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

> This recipe shows you how to enrich a list of LinkedIn profile IDs through the use of the Bulk Person Enrichment API

<RequestExample>
  ```python Python3 expandable theme={null}
  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 expandable theme={null}
  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)
  ```
</RequestExample>

<ResponseExample>
  ```json Response Example theme={null}
  Read 2 profiles.
  Wrote 2 lines to: 'enriched_profiles.csv'
  ```
</ResponseExample>

<Steps>
  <Step title="Description">
    {/* <!-- 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.
  </Step>

  <Step title="Initial Setup">
    {/* <!-- 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.
  </Step>

  <Step title="Initialize Arrays">
    {/* <!-- 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.
  </Step>

  <Step title="Set API Key">
    {/* <!-- 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.
  </Step>

  <Step title="Read List of LinkedIn Profiles">
    {/* <!-- 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](https://drive.google.com/file/d/1CrNXR6lCDEVY_Vvpn2QAWbidxhxCe3dg/view?usp=sharing)
  </Step>

  <Step title="Adds each row to the Requests Array">
    {/* <!-- python@23 --> */}

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

  <Step title="Execute Bulk Person Enrichment API Query">
    {/* <!-- python@27 --> */}

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

  <Step title="Iterate Through Query Responses">
    {/* <!-- 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.
  </Step>

  <Step title="Output Results to a CSV File">
    {/* <!-- python@36-56 --> */}

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


## Related topics

- [Examples - Person Identify API](/docs/examples-person-identify-api.md)
- [Examples - Job Posting Search API](/docs/examples-job-posting-search-api.md)
- [Send your first Company Enrichment API request](/docs/send-your-first-company-enrichment-api-request.md)
- [Send your first Person Enrichment API request](/docs/send-your-first-person-enrichment-api-call.md)
- [Input Parameters - Person Enrichment API](/docs/input-parameters-person-enrichment-api.md)
