How to connect to Salesforce API from Python using ODBC

Introduction

Connecting Python to Salesforce via ODBC enables seamless interaction with CRM data—retrieving leads, managing accounts, and automating business processes directly from your Python applications. This guide walks you through the installation, configuration, and execution steps required for a reliable integration using the pyodbc module alongside the ZappySys Salesforce ODBC Driver.

In this tutorial, you’ll learn how to connect to the Salesforce API using Python via ODBC, leveraging the powerful capabilities of the ZappySys ODBC PowerPack.

Prerequisites

Steps

Step 1: Install pyodbc in Python

  1. Ensure Python is installed. If not, download it from the official website.

  2. Open your terminal or command prompt and run:

    python -m pip install pyodbc
    

    This installs the pyodbc library, which enables Python to interact with ODBC drivers.

Step 2: Generate your Salesforce security token

  1. Log in to Salesforce.

  2. Click your profile icon, go to Settings > Personal Information > Reset My Security Token, and click Reset Security Token.

  3. Check your email inbox—your token will be sent there.

Step 3: Set up the ODBC Data Source

  1. Open the ODBC Data Source Administrator by searching for “ODBC” on your system.

  2. Under the User DSN tab, click Add and choose ZappySys Salesforce Driver.

  3. In the connection settings, enter your Salesforce username, password, and security token, then click Test Connection.

  4. Go to the Preview tab, select a table, and click Preview Data to confirm the connection.

Step 4: Read Salesforce data in Python

Create a script named salesforce_example.py:

import pyodbc

def fetch_salesforce_data():
    try:
        # In connection string use the same data source name we used in the previous step
        conn = pyodbc.connect('DSN=ZappySys Salesforce Driver')
        cursor = conn.cursor()

        print("------------ Salesforce & Python ----------------")

        cursor.execute("SELECT * FROM User")
        rows = cursor.fetchall()
        columns = [column[0] for column in cursor.description]

        # Iterate through each row and print as a dictionary
        for row in rows:
            row_dict = dict(zip(columns, row))
            # Example output:
            # {'Id': '005...', 'Username': 'jsmith', 'Email': 'jsmith@example.com'}
            print(row_dict)

    except pyodbc.Error as e:
        print("Error during connection or query execution:", e)

    finally:
        try:
            cursor.close()
            conn.close()
        except:
            pass

if __name__ == "__main__":
    fetch_salesforce_data()

Run the script to see your Salesforce records printed in the console:

Step 5: Use a whole Connection String (Optional)

To avoid setting up DSNs manually, you can use a connection string directly:

  1. Click Copy Settings in the ODBC UI after configuring your DSN.
  2. Replace the DSN=... in your script with the copied string:
conn = pyodbc.connect('DRIVER={ZappySys Salesforce Driver};User=you@example.com;Password=your_password+token;...')

Video tutorial

Conclusion

Following this step-by-step guide, you can successfully integrate Python with Salesforce using the ZappySys Salesforce ODBC Driver. This method allows you to automate workflows, retrieve and process CRM data, and perform analytics using Python. Download, explore the ZappySys ODBC PowerPack and get going!

References

Contact us

If you encounter any issues or have specific questions, reach out to our support team via live chat or support ticket.