Lob's website experience is not optimized for Internet Explorer.
Please choose another browser.

Arrow Up to go to top of page
Hero Image for Lob Deep Dives Blog PostAutomate Direct Mail with Hubspot
Tutorials
July 13, 2022

Automate Direct Mail with Hubspot

Share this post

With so many marketers storing their customer data in Hubspot, Hubspot Workflows have become a popular tool to automate the sending of personalized emails to their audience. However, Hubspot doesn’t natively provide a way to automate sending Direct Mail, which leaves a big hole in any communication strategy. Lob’s Direct Mail APIs can simplify and automate direct mail programmatically. By connecting Lob and Hubspot, you can automate the sending of personalized Direct Mail in the same way many automate their digital channels today.

Here’s a step-by-step guide on how to use Lob API integrations to add direct mail into your Hubspot Workflows to automatically send a postcard.  

Steps to Connect Your Hubspot Workflows to Lob

Create accounts

A developer plan for testing with Lob is free, but you will need to create a Lob account to get your API Keys. If you do not already have one, create a Hubspot account here. You’ll want to enable Operations Hub / Marketing Hub Professional enabled; if not, you can sign up for a free trial, which lasts for 14 days:  Automation> Workflows> Start 14 Day Trial

Create contacts

In Hubspot, you can create contacts 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 use this CSV as an example.

Note that 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’.

Automate Direct Mail with Hubspot image 2

Create a List

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

Automate Direct Mail with Hubspot image 3

Create A Workflow

Select Automation> Workflows

Automate Direct Mail with Hubspot image 4

Click Create Workflow

Automate Direct Mail with Hubspot image 5

Click Contact-based> Blank workflow

Automate Direct Mail with Hubspot image 6

Set Up Trigger

In this example, we'll set up the trigger to be adding a contact to the list. That is, when we add a contact to our list, we want something to happen.

List Memberships> Select the List you previously created> is member of list

Automate Direct Mail with Hubspot image 7
Automate Direct Mail with Hubspot image 8
Automate Direct Mail with Hubspot image 9

Create a resulting Action

Beneath Contact enrollment trigger, click +

Automate Direct Mail with Hubspot image 10

Under Workflow, select Custom Code

Automate Direct Mail with Hubspot image 11

For Language 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, Property to Include in Code should include:

Automate Direct Mail with Hubspot image 12

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

(To learn more about Lob’s code explore the API docs here.)

Automate Direct Mail with Hubspot image 13

Sample 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')
   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 = json.dumps({
       "description": "HUBSPOT DEMO POSTCARD",
       "to": {
       "name": firstname + ' ' + lastname,
       "address_line1": address,
       "address_line2": address_line2,
       "address_city": city,
       "address_state": state,
       "address_zip": zip_code
       },
       "front": "HUBSPOT_CREATIVE",
       "back": "HUBSPOT_CREATIVE",
       "size": "4x6",
       "metadata": {
           "customer_id": hs_object_id,
           "campaign": "HUBSPOT_TEST",
           "phone": phone,
           "email": email,
           "front": "demo_hubspot_front_creative",
           "back": "demo_hubspot_back_creative",
           "address_city": city,
           "address_state": state,
           "address_zip": zip_code
       }
   })

   headers = {
     'Authorization': 'Basic [BASE64-ENCODED API_KEY:]',
     'Content-Type': 'application/json'
   }

   session = retry_session(retries=5)
   r = session.post(url, headers=headers, data=payload)
   j = r.json()
   print(j)

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 [BASE64-ENCODED API_KEY:] with your Test API Key, Base64 encoded. IMPORTANT: ENSURE YOU ARE USING YOUR TEST API KEY. For example, if your API key is test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc, you can enter test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc: into a Base64 Encoder tool, the result of which will be something like dGVzdF8wZGM4ZDUxZTBhY2ZmY2IxODgwZTBmMTljNzliMmY1YjBjYzo=.

Note that we added a “:” to the end of the API key before encoding it, please ensure you do the same. Thus the value within your Python script will now look something like:  'Authorization': 'Basic dGVzdF8wZGM4ZDUxZTBhY2ZmY2IxODgwZTBmMTljNzliMmY1YjBjYzo=',

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",

(For more about designing/formatting creative for Lob, go here.)

Make sure to hit ‘Save’.

Test!

Then you can re-open your custom code, scroll to the bottom of the panel, and click Test Action. Select a contact, and click Test to fire off a test request.

Automate Direct Mail with Hubspot image 14

You can then log into your Lob account to verify that your postcard was generated; you should see it your dashboard under Postcards (Test). The below is the result of the example creative.

Automate Direct Mail with Hubspot image 15

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.

When you are ready, you can replace the Test API key in your Python code with your Base64 Encoded Live API Key, and your Automation is live. (You will need to add payment information to your Lob account should you actually wish to print and send mailpieces.)

Wrap up

In addition to offering automation for your digital channels, you can also automate your direct mail sends with Hubspot by integrating Lob's Direct Mail APIs. Give it a whirl!

Continue Reading