POST working in Postman - Need to transfer to Workflow
SOLVE
Good Afternoon,
I have a post call working in POSTMAN and would like to use a webhook or custom-code solution in a Hubspot workflow. I created the secret using a private app w/limited scope/permissions.
I'm not entirely sure what to put in the code section of HS. My Postman has no auth but 34 headers and a small body. Below shows the body as JSON and I have options Text/JS/JSON/HTML/XML.
(the working POST in PM is pre-filling a test MS form)
I see that hubspot has two language options of Node.JS 20.x and Python 3.9. I tried what I thought to be too easy and set my PM bodyto JS and copied the body over to HS but no dice there.
I know little about the POST, I actually submitted the MS form in chrome, recorded the action, copied the curl and imported it to PM. It actually worked first time.
Can someone provide some guideance how to get a working POST in PM to work in HS? Would the HS postman integration help?
Firstly, if your previous post contains any potentially sensitive information (e.g. an API access token), I'd recommend removing these from the post and, if relevant, rotating the API access token in the external system.
Your Python code would need to conform to HubSpot's requirement to use a function ("main") that takes the workflow "event" as an argument. Additionally, I'd recommend trying to widdle that request down to the bare minimum accepted by the external service's API (Microsoft Office Forms?) -- you'd need to review the relevant API docs for this.
If you're interested, I'd be happy to offer some additional consultation on this -- feel free to send me a DM and we can pick this up there.
All the best,
Zach
--
Zach Klein HubSpot Integrations & App Developer Meanjin / Brisbane, Australia
Here is the 'Python - Request' code snippet conversion from my PM POST. It's resulting in an error which is shown farther below .
It should be noted that I am testing it using a ticket but not actually pulling any properties out of the ticket. I hope that's not causing an error. I'm just using the variable properties as working in PM.
WARNING: The logs for this function have exceeded the 4KB limit.
...
2024-12-11T20:26:02.890Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module"," at _loadUserApp (file:///var/runtime/index.mjs:1084:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)"," at async start (file:///var/runtime/index.mjs:1282:23)"," at async file:///var/runtime/index.mjs:1288:1"]}
INIT_REPORT Init Duration: 178.25 ms Phase: init Status: error Error Type: Runtime.UserCodeSyntaxError
2024-12-11T20:26:04.029Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module"," at _loadUserApp (file:///var/runtime/index.mjs:1084:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)"," at async start (file:///var/runtime/index.mjs:1282:23)"," at async file:///var/runtime/index.mjs:1288:1"]}
INIT_REPORT Init Duration: 1392.40 ms Phase: invoke Status: error Error Type: Runtime.UserCodeSyntaxError
START RequestId: cd443788-5f3e-4145-ba16-cf857d906214 Version: $LATEST
Memory: 65/128 MB
Runtime: 1422.91 ms
POST working in Postman - Need to transfer to Workflow
SOLVE
I think the error I posted was wrong. I had HS CC set for JS and not Python. The error is definitely showing something about MAIN but when I copy/paste it here it will no longer let me post. So seems that's missing. I will pick away at this a bit, see if I can resolve the error.
Firstly, if your previous post contains any potentially sensitive information (e.g. an API access token), I'd recommend removing these from the post and, if relevant, rotating the API access token in the external system.
Your Python code would need to conform to HubSpot's requirement to use a function ("main") that takes the workflow "event" as an argument. Additionally, I'd recommend trying to widdle that request down to the bare minimum accepted by the external service's API (Microsoft Office Forms?) -- you'd need to review the relevant API docs for this.
If you're interested, I'd be happy to offer some additional consultation on this -- feel free to send me a DM and we can pick this up there.
All the best,
Zach
--
Zach Klein HubSpot Integrations & App Developer Meanjin / Brisbane, Australia
I personally prefer using Python in HubSpot Workflow Custom Code Actions. As such, here's an example using Python's "requests" library that might help you get started:
import requests
import os
def main(event):
# Use inputs to get data from any action in your workflow and use it in your code instead of having to use the HubSpot API.
email = event["inputFields"]["email"]
endpoint = "https://webhook.site/abc-123"
params = {
"email": email
}
headers = {
"header-x": "abc",
"header-y": "xyz"
}
response = requests.post(endpoint, headers=headers, params=params)
print(response.status_code)
print(response.json())
# Return the output data that can be used in later actions in your workflow.
return {
"outputFields": {
"response__status_code": response.status_code,
"response__body__message": response.json()["message"]
}
}
From there, it'll just be a matter of making the necessary updates as per your successful Postman request -- Postman makes this pretty easy via the "Code snippet" > "Python - Requests" translation feature.
I hope this proves helpful. Please let me know if you have any follow-up questions.
All the best,
Zach
--
Zach Klein HubSpot Integrations & App Developer Meanjin / Brisbane, Australia