Why am I Receiving an Error When Trying to Use the HubSpot Python API Client in a Python Code Step?

This topic was automatically generated from Slack. You can find the original thread here.

I am trying to use the hubspot python api client in a python code step. I am using the magic comments to import it but I am getting this error thrown:

Error
Command failed: python3 -m pip install --target=/tmp/__pdg__/dist/python -r requirements.txt --upgrade WARNING: The directory '/home/sbx_user1051/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag. ERROR: Invalid requirement: 'simplejson, delorean, hubspot-api-client,' (from line 2 of requirements.txt) [notice] A new release of pip is available: 23.0.1 -> 23.3.1 [notice] To update, run: pip install --upgrade pip

Here is my code, it currently just throws the error without running:

# pipedream add-package simplejson, delorean, hubspot-api-client, 

import json
from hubspot import HubSpot
from hubspot.crm.contacts import BasicApi, SimplePublicObjectInput
from hubspot.crm.associations import BatchApi, BatchInputPublicAssociationCreate

def handler(pd: Pipedream):
    try:
        # Get the OAuth token from the connected HubSpot app
        token = pd.inputs["hubspot_developer_app"]["$auth"]["oauth_access_token"]

        # Initialize the HubSpot client with the OAuth token
        api_client = HubSpot(api_key=token)
        print("Successfully authenticated with the HubSpot API.")
        contacts_api = BasicApi(api_client)
        associations_api = BatchApi(api_client)

        # Fetch all contacts from HubSpot
        existing_contacts = contacts_api.get_all(properties=["linkedin_profile"])
        existing_contacts_dict = {contact.properties["linkedin_profile"]: contact.id for contact in existing_contacts.results}

        # Extract the LinkedIn profile of the incoming contact
        incoming_contact_linkedin = pd.event.body["linkedin_profile"]

        # Check if the incoming contact exists
        if incoming_contact_linkedin in existing_contacts_dict:
            # If the contact exists, update it
            contact_id = existing_contacts_dict[incoming_contact_linkedin]
            contact_update = SimplePublicObjectInput(properties=pd.event.body)
            contacts_api.update_by_id(contact_id, contact_update)
            print(f"Updated existing contact with ID {contact_id}")
        else:
            # If the contact doesn't exist, create it
            contact_create = SimplePublicObjectInput(properties=pd.event.body)
            created_contact = contacts_api.create(contact_create)
            print(f"Created new contact with ID {created_contact.id}")

            # Associate the contact with a company
            company_id = pd.event.body["hubspot_company_id"]
            association_create = BatchInputPublicAssociationCreate(
                inputs=[{"from": {"id": created_contact.id}, "to": {"id": company_id}, "type": "contact_to_company"}]
            )
            associations_api.create_batch(association_create)
            print(f"Associated contact with company ID {company_id}")

    except Exception as e:
        print(f"Error: {e}")

For reference, the hubspot python api is installed using pip like:

pip install --upgrade hubspot-api-client

And used like this:

from hubspot import HubSpot

api_client = HubSpot(access_token='your_access_token')

# or set your access token later
api_client = HubSpot()
api_client.access_token = 'your_access_token'

###

from hubspot.auth.oauth import ApiException
from hubspot.crm.contacts import SimplePublicObjectInputForCreate
from hubspot.crm.contacts.exceptions import ApiException

etc. 

What am I am doing wrong here? Please help me figure out this problem, think step by step.

You should visit https://pipedream.com/pricing for the latest pricing, but I’ll do my best to answer. The error message you’re seeing is due to an incorrect format in the magic comment for package installation. The magic comment should be # pipedream/requirements.txt simplejson delorean hubspot-api-client, not # pipedream add-package simplejson, delorean, hubspot-api-client,. The packages should be space-separated, not comma-separated. However, it’s important to note that Pipedream’s Python environment currently does not support installing packages via pip. The Python environment comes with a set of pre-installed packages, and currently, there’s no way to install additional packages. The HubSpot API client you’re trying to use is a Python package that needs to be installed via pip, which is not supported in Pipedream’s Python environment. As an alternative, you can use HTTP requests to interact with the HubSpot API directly. You can use the requests package, which is one of the pre-installed packages in Pipedream’s Python environment, to make these HTTP requests. Here’s an example of how you can make a GET request to the HubSpot API: ```
import requests # Get the OAuth token from the connected HubSpot app token = pd.inputs[“hubspot_developer_app”][“$auth”][“oauth_access_token”] # Define the headers for the request headers = { “Authorization”: f"Bearer {token}“, “Content-Type”: “application/json” } # Make the GET request to the HubSpot API response = requests.get(“https://api.hubapi.com/crm/v3/objects/contacts”, headers=headers) # Print the status code and the data from the response print(f"Status code: {response.status_code}”) print(f"Data: {response.json()}")

I’ll update the docs on this, but you’ll need to add each requirement on its own line:

# pipedream add-package simplejson
# pipedream add-package delorean
# pipedream add-package hubspot-api-client

Let me know if that works!

Thanks that fixed it! Appreciate the help