Pardot Salesforce Python API

626 Views Asked by At

I'm having this requirement to get data out from Pardot Saleforce objects using Python API.

Can someone please share any available snippets to get data from all the Pardot objects(tables) using Python.

1

There are 1 best solutions below

0
On

I am working on a Pardot sync solution using pypardot4 (kudos to Matt for https://github.com/mneedham91/PyPardot4), which involves retrieving data through the API (v4). Here are some snippets for Visitors API, but you can use the same for almost any Pardot APIs (except Visit...):

from pypardot.client import PardotAPI

# ... some code here to read API config ...
email = config['pardot_email']
password = config['pardot_password']
user_key = config['pardot_user_key']

client = PardotAPI(email, password, user_key)
client.authenticate()

# plain query
data = client.visitors.query(sort_by='id')
total = data['total_results']
# beware - max 200 results are returned, need to implement pagination using offset query paramerter

# filtered query
data = client.visitors.query(sort_by='id', id_greater_than=last_id)

Also I have used some introspection to iterate through API config data I have set up like this:

apiList = config['apiList']

# loop through the apis, and call their query method
for api in apiList:
    api_name = api['apiName']
    api_object_name = api['clientObject']
    api_object = getattr(client, api_object_name)
    method = 'query'

    if api.get('noSortBy', False) == False:
        data = getattr(api_object, method)(created_after=latest_sync, sort_by='created_at')
    else:
        # The API is not consistent, the sort_by criteria is not supported by all resources
        data = getattr(api_object, method)(created_after=latest_sync)

And a snippet from the apiList config JSON:

    "apiList":[
        {
            "apiName": "campaign",
            "clientObject": "campaigns"
        },
        {
            "apiName": "customField",
            "clientObject": "customfields"
        },
        {
            "apiName": "customRedirect",
            "clientObject": "customredirects"
        },
        {
            "apiName": "emailClick",
            "clientObject": "emailclicks",
            "noSortBy": true
        },
...

Notice the noSortBy field and how it's handled in the code.

Hope this helps!