Automate Direct Mail with Hubspot
Tags: python, crm, hubspot, tutorial
Published: 2022-07-26 20:26:13

With so many marketers storing their customer data in [Hubspot](https://www.hubspot.com/), 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 integrating with CRMs. 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 integrate Lob into your automated Hubspot workflows to automatically send a postcard.  

## 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](https://dashboard.lob.com/login) to get your API Keys. If you do not already have one, [create a Hubspot account here](https://app.hubspot.com/signup-hubspot/crm?uuid=ff52f537-4146-48e8-8cd2-8545c26b767d&step=landing_page). You’ll want to enable Operations Hub / Marketing Hub Professional trial (note this lasts 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. (Ensure they have address parameters.)

![Contacts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/byq6fsi9efhfixcaho91.png)

From there, walk through the flow to import a file from your computer: Start an Import> File From Computer> One File> One Object> Contacts. You can use [this CSV file](https://s3.us-west-2.amazonaws.com/public.lob.com/solutions/Hubspot_demo/Hubspot_demo_Contacts_10.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’. 

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

![List](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rauz5j2rw7562cabzmvr.png)

### Create A Workflow
Select Automation> Workflows

![workflows](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9atoq9wesprr4z97d6pe.png)

Click Create Workflow

![create workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iojmipsfe354h1uzup2c.png)

Click Contact Based> Blank Workflow

![contact-based workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ha37ustdejttjg1sx1y5.png)

### Set Up Trigger
For example: List Memberships> Select List you previously created> Is Member of List

![trigger1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yarvpv96wu3te5bjs0np.png)

![trigger2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vr59v8m0picmb004wjl.png)

![trigger3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cooc4dcr9qhmoa82vp7z.png)

### Create a resulting Action
Click + button on Workflow, beneath the Trigger

![action](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2lc8235ejrvft3648k3.png)

Select Custom Code

![custom code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1c8ggywgc292h8oxdvxo.png)

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:

![map properties](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rx4yroa11balftc8senh.png)

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](https://docs.lob.com/#tag/Postcards).)

![code in UI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/boml02yi0dgvthpr3ohl.png)

**Example Python Code:**
{%gist https://gist.github.com/lobot/3fa178988b66b7ac638ff62e438a541d.js %}

### 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](https://www.base64encode.org/), 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](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](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](https://help.lob.com/mail-piece-design-specs/postcards-self-mailers).) 

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. 

![testing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4syt7i9f3cttuo6c1z0.png)

You can then log into your Lob account to verify that your postcard was generated. (The below is the result of the example creative.)

![postcard front and back](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ymvkvig7wol9ecw030b.png)

### 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.

## Wrap up
Lob can expand the capabilities of Hubspot to both personalize, and automate your direct mail sends. Give it a whirl!