🚀 Executive Summary
TL;DR: This guide addresses the tedious manual process of cross-referencing LinkedIn connections with a CRM by providing a Python script. The solution automates fetching LinkedIn connections, cleaning and formatting the data, and exporting it to a CRM-ready CSV file, significantly saving time and reducing errors.
🎯 Key Takeaways
- Securely manage LinkedIn credentials using `python-dotenv` and a `config.env` file, ensuring it’s added to `.gitignore` to prevent accidental version control exposure.
- Utilize the `linkedin_api` Python library for authenticating and programmatically fetching LinkedIn connections, and `pandas` for efficient data manipulation and CSV export.
- Process raw API connection data by extracting relevant fields like `firstName`, `lastName`, `occupation` (parsed into ‘Title’ and ‘Company’), and constructing `LinkedInProfileURL` from `public_identifier` for CRM compatibility.
Exporting LinkedIn Connections to CSV for CRM Import
Hey team, Darian here. Let’s talk about a workflow that saved me a surprising amount of time. For a while, I was manually cross-referencing my new LinkedIn connections with our CRM. It was a tedious, error-prone process that I’d put off until Friday afternoons. I finally got fed up and automated it. This script took me an hour to write, and it’s since saved me at least that much time every single month. It pulls my connections, formats them cleanly, and outputs a CSV ready for any modern CRM. Let’s get it done.
Prerequisites
Before we jump in, make sure you have the following ready. This whole process shouldn’t take more than 20 minutes if you have these lined up.
- Python 3.x: Most modern systems have it. If not, you’ll need to get it installed.
- A LinkedIn Account: You’ll need your username and password. I highly recommend enabling 2FA for security.
- Python Libraries: We’ll need a few packages. You can install them with pip, for example:
pip install python-linkedin-api pandas python-dotenv.
The Step-by-Step Guide
Alright, let’s get into the code. I’ll skip the standard virtual environment setup since you likely have your own workflow for that. The key is to keep these dependencies isolated. Let’s jump straight to the Python logic.
Step 1: Create Your Configuration File
First rule of DevOps: never hardcode secrets. We’ll store our LinkedIn credentials in a `config.env` file in our project directory. This keeps them separate from the script and out of version control.
Your `config.env` file should look like this:
LINKEDIN_USER="your_linkedin_email@example.com"
LINKEDIN_PASS="your_super_secret_password"
Pro Tip: Make sure you add `config.env` to your `.gitignore` file immediately. You do not want to accidentally commit your credentials to a repository. It’s a classic mistake, and it’s a huge security risk.
Step 2: Authentication and Setup
Now for the Python script. Let’s call it `export_connections.py`. We’ll start by importing the necessary libraries and setting up the authentication with the LinkedIn API using the credentials from our `config.env` file.
import os
import pandas as pd
from dotenv import load_dotenv
from linkedin_api import Linkedin
# Load environment variables from config.env
load_dotenv('config.env')
LINKEDIN_USERNAME = os.getenv("LINKEDIN_USER")
LINKEDIN_PASSWORD = os.getenv("LINKEDIN_PASS")
def authenticate_linkedin():
"""Authenticates with LinkedIn and returns an API object."""
print("Attempting to authenticate...")
try:
api = Linkedin(LINKEDIN_USERNAME, LINKEDIN_PASSWORD)
print("Authentication successful.")
return api
except Exception as e:
print(f"Authentication failed: {e}")
return None
# Authenticate
linkedin_api = authenticate_linkedin()
Here, we’re using `python-dotenv` to load our variables, and then we pass them to the `Linkedin` class. I’ve wrapped the authentication in its own function. In my production setups, this helps with error handling and re-authentication logic if a token expires.
Step 3: Fetching Your Connections
With a valid API object, we can now fetch our connections. The API returns a list of dictionary objects, each representing a connection. We’ll grab the first 1000 connections for this example.
def get_connections(api_client):
"""Fetches connections from the LinkedIn API."""
if not api_client:
print("API client is not authenticated. Cannot fetch connections.")
return []
print("Fetching connections... this may take a moment.")
try:
# You can adjust the limit as needed
connections = api_client.get_connections(max_connections=1000)
print(f"Found {len(connections)} connections.")
return connections
except Exception as e:
print(f"Failed to fetch connections: {e}")
return []
# Fetch connections
raw_connections = get_connections(linkedin_api)
Pro Tip: Be mindful of API rate limits. If you have thousands of connections, don’t try to pull them all in one go every five minutes. For larger lists, you might need to implement pagination or run the script less frequently. The library we’re using handles some of this, but it’s good practice to be aware of it.
Step 4: Processing and Exporting to CSV
The raw data from the API is useful, but it’s not quite ready for a CRM. We need to parse it, select the fields we care about, and structure it. I always use the Pandas library for this—it’s incredibly efficient for data manipulation.
def process_and_export_to_csv(connections, filename="linkedin_connections.csv"):
"""Processes connection data and exports it to a CSV file."""
if not connections:
print("No connections to process.")
return
processed_list = []
for conn in connections:
# The 'occupation' field can be long, so we split it for clarity
occupation_parts = conn.get('occupation', 'N/A at N/A').split(' at ')
title = occupation_parts[0]
company = occupation_parts[1] if len(occupation_parts) > 1 else 'N/A'
# public_identifier is the unique part of their profile URL
profile_url = f"https://www.linkedin.com/in/{conn.get('public_identifier')}"
processed_list.append({
'FirstName': conn.get('firstName', ''),
'LastName': conn.get('lastName', ''),
'Title': title,
'Company': company,
'LinkedInProfileURL': profile_url,
})
# Create a DataFrame and save to CSV
df = pd.DataFrame(processed_list)
try:
df.to_csv(filename, index=False, encoding='utf-8')
print(f"Successfully exported data to {filename}")
except Exception as e:
print(f"Failed to write to CSV: {e}")
# Process and export
if raw_connections:
process_and_export_to_csv(raw_connections)
The logic here is straightforward: we loop through each connection, extract the relevant fields like name and occupation, and build a clean dictionary. I’m also constructing the full profile URL, which is a highly valuable field for any CRM. Finally, Pandas takes our list of dictionaries and turns it into a perfectly formatted CSV file with headers.
Common Pitfalls (Where I Usually Mess Up)
Even a simple script has its gotchas. Here are a few I’ve run into:
- Expired Credentials: LinkedIn’s authentication cookie doesn’t last forever. If the script suddenly fails with an authentication error after working for weeks, this is the first thing I check. You’ll need to re-authenticate.
- CRM Field Mismatches: Your CRM is picky. It might expect ‘First Name’ but my script generates ‘FirstName’. Before you import, always open the CSV and check that the column headers match your CRM’s import template exactly.
- Handling Null Data: Not every connection has a listed job title or company. My code uses `.get()` with default values (like ‘N/A’) to prevent the script from crashing when a key is missing. Forgetting this can lead to an ugly `KeyError`.
Conclusion
And that’s it. You now have a reusable script to pull your LinkedIn connections into a format that’s ready for any serious sales or marketing workflow. This is a foundational script. You can easily set it up to run automatically. For instance, I have a similar script running as a weekly job to keep our CRM fresh. A simple cron job like `0 2 * * 1 python3 export_connections.py` would do the trick, running it every Monday at 2 AM. Automating these small, manual data tasks frees you up to focus on the more complex engineering problems. Hope this helps.
🤖 Frequently Asked Questions
âť“ How do I export my LinkedIn connections to a CSV for CRM import using this method?
You need Python 3.x, a LinkedIn account, and specific Python libraries (`python-linkedin-api`, `pandas`, `python-dotenv`). Create a `config.env` file for credentials, then use a Python script to authenticate, fetch connections via the `linkedin_api`, process the data with `pandas`, and export it to a CSV file.
âť“ What are the advantages of this script-based approach over manual export or other tools?
This script offers automated, customizable data extraction and formatting, ensuring precise CRM field matching and saving significant manual effort. It provides more control over data fields and structure compared to LinkedIn’s native export options, which may be limited or less flexible for specific CRM requirements.
âť“ What are common implementation pitfalls when using this LinkedIn connection export script?
Common pitfalls include expired LinkedIn authentication credentials requiring re-authentication, CRM field mismatches where CSV column headers don’t align with CRM import templates, and `KeyError` exceptions if missing data fields are not handled gracefully using `.get()` with default values.
Leave a Reply