There are some processes that no-code workflow actions can't reach: complex calculation logic, communication with external APIs, conditional judgments that combine multiple properties, and unique business rules that are not included in HubSpot's standard actions. In this chapter How custom code actions work - How to use JavaScript and Python (Beta) - 10 code samples that frequently occur in practice - Security and error handling - Best practices for production operationsExplain systematically.
Custom Code Actions allow you to embed JavaScript or Python code blocks in your workflow.Ability to run on HubSpot's serverless environment (equivalent to AWS Lambda)is. Advanced processing can be achieved by simply writing code on the HubSpot portal's workflow UI without preparing an external server.
| Comparison items | JavaScript(Node.js) | Python(Beta) |
|---|---|---|
| Availability | ✓ GA (official release/stable) | △ Beta (from 2025, please be careful when using it in production) |
| Node/Python version | Node.js 18.x | Python 3.9 |
| standard library | Node.js standard modules (https, crypto, etc.) | Python standard library + requests, pandas, numpy |
| Applications suitable for | API integration/JSON manipulation/string processing/asynchronous processing | Data conversion, statistical calculation, data processing using pandas |
| Recommended users | Front-end engineer/RevOps general | Data Engineer/Analyst/Python Experienced |
| Recommended Winner | 20 seconds | 30 seconds |
| secrets (environment variables) | ✓ Compatible | ✓ Compatible |
Python support is still in Beta, so JavaScript is recommended for production-critical workflows.Choose Python only if you have specific needs such as processing large amounts of numerical data with pandas or linking with machine learning models.However, troubleshooting will be easier if you use JavaScript for everything else.
JavaScript for custom code actions has a fixed structure.What is Data Studio? Differences from ETL tools and architectureThere are three steps. The starting point is to fully understand this basic pattern.
Before writing the code, on the workflow editorDeclare "Input Fields" and "Output Fields" in advanceSet a calendar reminder to check the expiry date of your API key/credentials every quarter
| Setting items | place | Setting details | Points to note |
|---|---|---|---|
| Input Fields | Code editor left panel “Inputs” | Select the properties you want to pass to your code (e.g. email, company, custom_score) | Undeclared properties cannot be retrieved with event.inputFields |
| Output Fields | Code editor left panel "Outputs" | Mapping variable name (e.g. total_score) and write destination property (e.g. custom property "total_score") | The key name of outputFields in the code and the variable name of the Output declaration must match exactly. |
| Secrets (environment variables) | Settings → Private App / Secrets | Register confidential information such as API key with "secret name" and refer to it in the code with process.env.secret name | Don't write API keys directly in your code. It will be leaked the moment you push it to GitHub. |
Python support, available starting in 2025, is a great addition for data engineers and analysts.Pre-installed libraries such as pandas, numpy, requests, etc.It is possible to convert CSV data and perform aggregation using data frames directly within the workflow.
Below is a summary of the most commonly used custom code action patterns in actual HubSpot operations. Each pattern is a practical sample that can be copied and used as is.
Custom code actions are powerful, butWhen an error occurs, the entire workflow stops, confidential information is leaked, and execution costs explode.There are also risks involved. Be sure to review the following best practices before deploying to production.
| rule | Bad example (NG) | Good example (OK) |
|---|---|---|
| Manage API keys | Directly in the code: const key = "sk-abc123..." |
Register and see Secrets: process.env.OPENAI_KEY |
| HubSpot API token | Use hard-coded or wide-privileged Private App Tokens | Create a dedicated Private App Token with only the scopes you need and store it in Secrets |
| Error log contents | Output secret information to the log with console.log(apiKey) | Workflow “Fill Smart Property” action |
| External communication destination | Incorporating user input values directly into the URL (SSRF risk) | Correspondence URLs must be hard-coded or checked against allowlists before use. |
① Always conduct tests in a sandbox——It is strictly prohibited to suddenly run code on the production portal. First check the operation in HubSpot's sandbox (Developer Sandbox) and then apply it to production.
② Add a comment to the WF name/code——Leave a comment saying ``Who wrote this logic, when, and why?'' I will forget it myself after 3 months. It becomes unreadable when another administrator takes over.
③ Check the execution log regularlyBinary data is not synchronized. Sync URL only
④ Pay attention to the upper limit on the number of executions (100,000 times/month)——Professional plan allows you to run custom code actions up to 100,000 times per month. When applying to a large number of records at once, check the number of records in advance. You can check the remaining number of times in "Settings → Usage Status".
① Have you declared Input / Output Fields on the UI? / ② Have you set null checks and default values for all input values? / ③ Have you registered API keys and other confidential information in Secrets and referenced them in process.env? / ④ Have you handled errors with try-catch? / ⑤ Have you obtained the intended results by running the test in a sandbox? / ⑥ Did you try just one case using the "Test Contact" function of the workflow before applying it to production? Clear all 6 items before deploying to production.
Complex calculations, external API communication, and conditional branching of multiple properties that cannot be handled with standard actions can be implemented in code. However, first check whether it can be achieved using standard actions before using it. Avoid abuse due to high maintenance costs.
JavaScript is stable and can be used in production. Python is used only in cases where numerical calculations using pandas are required. Because it is a beta feature, use Python in critical workflows only after understanding the risks. Be aware of timeout and memory limits for both.
Hardcoding API keys into your code is the biggest security risk. The code is visible to all workflow administrators. All confidential information should be registered in Secrets and referenced in process.env.XXX. This is not a "recommendation" but an "absolute rule".
HubSpot always has records with blank properties. If you process it as null/undefined, a TypeError will occur and the entire workflow will stop. Don't forget to set default values for all input values and try-catch external API calls.
A bug in a custom code action could cause it to write incorrect values to tens of thousands of records at the moment of bulk application. Be sure to check the operation in the sandbox, and in the actual production, expand it step by step from 1 item to 10 items to 100 items, confirm the operation, and then apply it to all items.
Calculate ICP score based on imputed values