LogoLogo
API DocsTemplate GalleryProduct UpdatesContact Us
  • 📬Print & Mail
    • Ready to get started?
      • Pricing details
      • Fast Track Guide
    • Integrations
      • API integrations
        • Action IQ
        • Adobe Marketo Engage
        • Blueshift
        • Braze
        • Customer.io
        • Hubspot
        • Iterable
        • Jotform
        • Klayvio
        • Listrak
        • Make
        • Optimove
        • Salesforce
        • Salesforce Marketing Cloud
        • Segment
        • Simon Data
        • Zapier
      • No-code integrations
        • Agile CRM
        • Freshsales Suite
        • HubSpot
        • Microsoft Dynamics 365
        • Pipedrive
        • Salesforce
        • Salesforce Marketing Cloud
        • Zoho
      • Creative conversion tools
        • Figma plugin
    • Reaching your audience
      • Lob Audience
        • Target Audiences
        • Lookalike Audiences
        • Purchasing Audiences
      • All about addresses
      • Campaign audience guide
      • Additional Lob NCOA functionality
    • Designing mail creatives
      • Artboard layout
      • Creative formatting
        • PDF preflight checklist
        • Exporting PDFs from InDesign
        • Rendering errors
      • Mail piece design specs
        • Postcards
        • Self-Mailers
        • Letters
        • Letter envelopes
        • Letter add-ons
        • Checks
        • Snap packs
        • Booklets
        • Custom mail
      • Maximizing engagement
        • Dynamic personalization
          • Advanced templating (Handlebars)
            • Dynamic table tutorial
        • Adding QR codes
        • Short URLs
        • Informed Delivery
    • Building a mail strategy
      • One-time campaigns or triggered sends?
      • Choosing a delivery strategy
      • Managing mail settings
        • Using metadata
        • Declaring mail use type
      • USPS Promotions Through Lob
        • Tactile, Sensory & Interactive Promotion
        • Integrated Technology Promotion
        • First Class Mail Advertising Promotion
        • Add-Ons
      • Mailing class and postage
        • Certified Mail or Registered Mail
      • International mail
    • Send mail!
      • Launch your first campaign
      • Send mail via Print & Mail API
      • Send campaigns via the Campaigns API
      • USPS Secure Destruction
    • Getting data & results
      • Tracking your mail
      • Mail analytics
      • Measuring attribution
      • Using webhooks
      • Exporting mail data
  • 🏠Address Verification
    • Ready to start AV?
      • US AV product suite
      • International AV suite
      • AV pricing
    • AV best practices
    • AV integrations & libraries
      • AV Elements
      • Shopify App: Address Cleanser
    • AV FAQs
  • 💻Developer Docs
    • API quickstart guide
    • SDKs & libraries
    • Postman & similar tools
    • Error reference
    • Upgrading API version
    • Technical use case guides
      • Mass deletion setup
      • NCOA responses
      • Override cancellation window
      • Visibility of address changes
      • Ingesting tracking events with webhooks
  • 🔑Account Management
    • Signing into Lob
    • API keys
    • Account settings
      • Account-level data logs
    • User settings
    • Billing
      • Lob Credits
      • Lob Payment Methods
      • Sales Tax FAQ
        • Applicable sales tax by state
          • ​Subscriptions and Services
          • Lob Audience
          • Delivery Location for Operational Mail
          • Customer Billing Address for Operational Mail
          • Delivery Location for Marketing Mail
          • Postage Exemption
          • Professional Services
        • Tax exemption guide
  • 📞Resources
    • Getting support
    • Security & privacy
    • Data retention
    • Sustainability
    • Private labeling Lob
    • Direct mail glossary
Powered by GitBook
On this page
  • Overview
  • Video walkthrough
  • Step-by-step walkthrough
  • Create accounts
  • Create contacts
  • Create a List
  • Create a Workflow
  • Set Up Trigger
  • Create a resulting Action
  • Add your API Keys
  • Update Creative
  • Testing
  • Utilizing workflow

Was this helpful?

Export as PDF
  1. Print & Mail
  2. Integrations
  3. API integrations

Hubspot

PreviousCustomer.ioNextIterable

Last updated 8 months ago

Was this helpful?

Overview

Hubspot is a popular marketing automation software tool. In early 2022, Hubspot added Custom Code functionality to their Workflows, enabling a direct connection to Lob. Hubspot’s Workflows can be used to automatically trigger a mailpiece to be sent, via Lob.

(Looking for a no-code integration option? .)

Video walkthrough

Step-by-step walkthrough

Create accounts

Create contacts

We will be creating a list in the next step, so for this walkthrough, do not select “Create a List from this Import”.

In the Map File screen, for ‘Address2’, select ‘Create A New Property’, and name it ‘Address2’.

Create a List

Contacts > Lists > Create List > Select ‘Static’ List > Contact-based > Name your List

Create a Workflow

Select Automation > Workflows

Click Create Workflow

Click Contact Based > Blank Workflow

Set Up Trigger

For example, List Memberships > Select List you previously created > Is Member of List

Create a resulting Action

Click + button on Workflow, beneath the Trigger

Select Custom Code

Here you can select Python or NodeJS. For this example, we will select Language: Python. Then we will map Lob’s API fields to Hubspot properties, and enter our custom code. For this example, Map Properties To Include in Code as follows:

Finally, enter your Python code, with properties defined as per the prior step. See example code below to test with.

Example Python code

import requests
import json
import base64
import os
from hubspot import HubSpot
from hubspot.crm.contacts import ApiException
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry


def retry_session(retries, session=None, backoff_factor=1):
    session = session or requests.Session()
    retry = Retry(
        total=retries,
        read=retries,
        connect=retries,
        backoff_factor=backoff_factor,
        status_forcelist=[500, 502, 503, 504, 429],
        allowed_methods=['POST']
    )
    adapter = HTTPAdapter(max_retries=retry)
    session.mount('http://', adapter)
    session.mount('https://', adapter)
    return session


def main(event):
    address = event.get('inputFields').get('address')
    address2 = event.get('inputFields').get('address2')
    city = event.get('inputFields').get('city')
    state = event.get('inputFields').get('state')
    zip_code = event.get('inputFields').get('zip_code')
    email = event.get('inputFields').get('email')
    hs_object_id = event.get('inputFields').get('hs_object_id')
    phone = event.get('inputFields').get('phone')
    firstname = event.get('inputFields').get('firstname')
    lastname = event.get('inputFields').get('lastname')

    url = "https://api.lob.com/v1/postcards"

    payload = {
        "description": "HUBSPOT DEMO POSTCARD",
        "to[name]": firstname + ' ' + lastname,
        "to[address_line1]": address,
        "to[address_line2]": address2,
        "to[address_city]": city,
        "to[address_state]": state,
        "to[address_zip]": zip_code,
        "front": "https://s3.us-west-2.amazonaws.com/public.lob.com/solutions/retail_pdfs/4x6+Retail+front.pdf",
        "back": "https://s3.us-west-2.amazonaws.com/public.lob.com/solutions/retail_pdfs/4x6+Retail+back.pdf",
        "size": "4x6",
        "metadata[customer_id]": hs_object_id,
        "metadata[campaign]": "HUBSPOT_TEST",
        "metadata[phone]": phone,
        "metadata[email]": email,
        "metadata[front_creative_name]": "demo_hubspot_front_creative",
        "metadata[back_creative_name]": "demo_hubspot_back_creative",
        "metadata[address_city]": city,
        "metadata[address_state]": state,
        "metadata[address_zip]": zip_code
    }

    session = retry_session(retries=5)
    r = session.post(url, auth=requests.auth.HTTPBasicAuth(YOUR_API_KEY, ''), data=payload)
    j = r.json()
    print(j)

If copying and pasting the Python code above, it is advised to run it through a Python code validator to ensure that no formatting issues occur as a result.

Add your API Keys

Next we need API credentials from our Lob account. Retrieve these credentials from your Lob dashboard by clicking on the Settings menu on the sidebar, then clicking on the API Keys tab.

In the example Python code, replace YOUR_API_KEY with your Test API Key.

ENSURE YOU ARE USING YOUR TEST API KEY

Update Creative

You should also replace the front and back "HUBSPOT_CREATIVE" with your own HTML, template IDs, or links to hosted static creatives. For example:

"front": "https://s3.us-west-2.amazonaws.com/public.lob.com/solutions/retail_pdfs/4x6+Retail+front.pdf",
"back": "https://s3.us-west-2.amazonaws.com/public.lob.com/solutions/retail_pdfs/4x6+Retail+back.pdf",

Testing

You can then log into your Lob account to verify that a mailpiece was generated.

Utilizing workflow

Once you’ve tested, you can Publish your workflow. From there, it will run whenever the trigger you set is fired. For example, if you followed the above example, you can now add some contacts to your list. This should automatically create mailpieces for each person on the list.

Now you can replace the Test API key in your Python code with your Base64 Encoded Live API Key, and your Automation is live. If you are using other test resources like address IDs or template IDs, those will also need to be transferred to your live environment.

A developer plan for testing with Lob is free, but you will need to to get your API Keys.

If you do not already have one, . You’ll want to enable Operations Hub / Marketing Hub Professional trial (note this lasts 14 days): Automation > Workflows > Start 14 Day Trial

You can create manually, or import from a CSV by clicking Contacts > Import. From there, walk through the flow to import a file from your computer: Start an Import > File From Computer > One File > One Object > Contacts. Ensure your contacts have address parameters; you can as an example.

Make sure to hit ‘Save’. Then you can open your custom code back up, scroll to the bottom of the panel, and click Test Action. Select a contact, and click Test to fire off a test request.

📬
create a Lob account
create a Hubspot account here
use this CSV
See here