Workflow Custom Code POST API - Registered contact in external (3rd party) system

Hi all,

We are premium users of your system and Hubspot’s startup program members.

Within our Automation → Workflow, We are trying to set custom code using an API to one of our partners.

The API simply allows us to registred new users within their system as well.

To complete that; we are using their API, which is a simple POST.
Upon completion, we are getting their status along with the user’s registered code.

If case of failure, they simply provide us with a message that contains a ley with the failure reason.

What we would like to achieve:
1. Get the relevant user data from our HubSpot contact list
2. Execute the POST using the contact’s data from step 1 above.
3. Upon completion and response from their system (API), update a specific contact’s fields which will then help us to decide if and how to proceed with the workflow.

To execute the POST and generate the code for it I have used this online tool, [https://reqbin.com/1ayiqdcg] which the API works perfectly.

Now I need your assistance to integrate our Python code into the Hubspot Custome Code section.

This is the generated code:

import requests
from requests.structures import CaseInsensitiveDict

url = “https://<aaaaaaaa>”

headers = CaseInsensitiveDict()
headers[“api-key”] = “<api key>”
headers[“Content-Type”] = “application/json”

data = “”"
{
“firstName”: “<hubspot contact’s first name>”,
“lastName”: “<hubspot contact’s last name>”,
“email”: “<hubspot contact’s email>”,
“phoneNumber”: “<hubspot contact’s phone number>”,
“abc123”: “<hubspot contact’s special field A>”,
“xyz”: “<hubspot contact’s special field B>”
}
“”"

resp = requests.post(url, headers=headers, data=data)

print(resp.status_code)

This is the response in case the user successfully generated

{
“status”: “Success”, # this value should be placed within our Hubspot contact field ‘last-api-status’

“clientId”: “62924883f64f63127b90c0ff” # this value should be placed within our hubspot contact field ‘last-api-client-id’
}

This is the response (one of the options) in case the user failed to be generated

{
“message”: {
“key”: “backend:alreadyRegistered”
# in this case i’d like to update the contact field ‘last-api-status’ with the value of “Error”
# and to update the contact field ‘api-status-reason’ with the value of “alreadyRegistered” (from the key above)
},
“index”: “2”
}

@JBeatty , any chance you can assist with this one?

Hi @GGuy,

First, rotate your API key for that service now, the link you provided includes the API key for any person to see.

To actually convert this code to a custom code action you can simply write the following code:

import os
import requests
from requests.structures import CaseInsensitiveDict

def main(event):

 url = "https://<aaaaaaaa>"

 headers = CaseInsensitiveDict()
 headers["api-key"] = "<api key>"
 headers["Content-Type"] = "application/json"

 data = """
 {
 "firstName": "<hubspot contact's first name>",
 "lastName": "<hubspot contact's last name>",
 "email": "<hubspot contact's email>",
 "phoneNumber": "<hubspot contact's phone number>",
 "abc123": "<hubspot contact's special field A>",
 "xyz": "<hubspot contact's special field B>"
 }
 """

 resp = requests.post(url, headers=headers, data=data)

 print(resp.status_code)

 return {
 "outputFields": resp.json()
 }

Then you can define an output field of clientId and then one of status, then in your workflow, you can do a value equals branch off of status being “Success” or something else, then if it is “Success” you can copy clientId into the field, otherwise you can set your other field to be “Error”.

Hi Joshua,

First, thank you so much for your help.

Second, this is exactly the code I have ended up with, though:

1. What is the syntax to place the firstname from HubSpot within the data JSON payload?

what should be in here instead of the: <hubspot contact’s first name>

data = """
 {
 "firstName": "<hubspot contact's first name>",

2. How can I manipulate the output JSON code to subtract only specific text

Such as:

  • In case of success: (or =200)
    • status": "Success
    • clientId": “abc12345def789”
  • In case of an error: (!=200)
    • backend:alreadyRegistered

3. Is it possible from the custom code here to already use the value and place it in a specific contact property field? for example, I have a field name “api-client-id” which I want to set its value to the clientId that came back through the API

(I can do it using the copy value within workflow but prefer from the code directly if possible)

Hi @GGuy,

1. Look at the documentation here.

2. The format of the output fields is defined here. I am not entirely familiar with python’s JSON implementation but by matching that structure you should be able to get your outputs and add following workflow actions to get your desired behavior.

3. The only way to do it directly within the code would be to make an API call to Hubspot, I would suggest not doing that to avoid rate limits, etc. So an output field followed by a copy action is your best bet.

I have to do something similar. When a user submits a Hubspot contact form I need the data to be saved in HS and I need to post some of that data to a 3rd-party API. My customers have the Marketing Starter package, so webhooks are out as I understand it. And I need to send the 3rd party API key in the post header. Do you know if this sounds doable? Thanks for any info -

Steve

Hi @SRalston05 ,

If you set up a public app(Doesnt actually need to be public), you can enable webhooks in any org.

Best,