HubSpot serverless functions - get rid of middleware when transferring data between systems

HubSpot serverless functions - get rid of middleware when transferring data between systems

HubSpot serverless functions are a great way to simplify the process of transferring data between systems. By eliminating the need for middleware, companies can save time and money by reducing the complexity of data transfer. With HubSpot serverless functions, companies can quickly and securely move data from one system to another without worrying about setting up complex middleware solutions.

This lets them focus on their core business operations instead of worrying about data transfer. Additionally, these functions also provide a secure environment for data transfer since they are hosted on HubSpot's secure cloud infrastructure.

What is a serverless function?

Serverless functions allow businesses to quickly deploy code without managing servers, allowing them to focus on their core business. Serverless functions are a single-purpose, programmatic feature of serverless computing — also called “serverless” — a cloud computing execution model where the cloud provider provisions computing resources on demand for its customers and manages all architectures, including cloud infrastructure.

With HubSpot's Serverless Functions, businesses can easily scale up and down as needed, rapidly responding to changes in the market and customer demand.

HubSpot serverless functions weaknesses

They are intended to be fast and to help you be independent without paying or acquiring a server. But they also have limits. According to the HubSpot website, they are limited to:

  • 50 secrets per account.
  • 128MB of memory.
  • Up to 100 endpoints per HubSpot account.
  • the contentType application/json when calling a function.
  • 6MB per invocation payload when uploading a file.
  • 4KB for the amount of data that can be logged.
  • Each function has a maximum of 10 seconds of execution time.
  • Each account is limited to 600 total execution seconds per minute.

Types of serverless functions

You can create two types of them: endpoint functions and app functions. They will be defined in the serverless.json file.

  1. API endpoint function - this creates public API endpoints under the domains connected to your HubSpot account.
  2. App function - is used only by a private app extension. It can’t be accessed publicly; you need to authenticate the app functions with the private app’s access token.

Resources needed to get started

While thinking of using it, you need to make sure that you have all the resources necessary to perform this task:

  • Access to a CMS Hub Enterprise account or a CMS Developer Sandbox account.
  • The knowledge and the usage of serverless functions are.
  • You are customed with the HubSpot local development tools.
  • Check and update, if necessary, your version of the HubSpot local development tools. Make sure you have the latest one.
  • An important step is to ensure you use your local development tools authenticated with your HubSpot account.

How to create a serverless function

To create a serverless function, first, you must create the file that ensures functionality, meaning you must create the JavaScript file.

The function receives two arguments you must take under consideration:

  1. Context - contains data sent to the function. Depending on the usage of the function, the data is different. After defining which are the propertiesToSend in the card’s JSON configuration and the context, they are passed to the serverless function.
  2. Callback – contains data received after the function’s task is completed. This is the answer to my previous action.

How to configure a HubSpot serverless function

According to the HubSpot website, you can configure the following properties of the serverless.json file:

runtime String The runtime environment. Supports the following  Node.js versions:
  • Node 16 (nodejs16.x)
  • Node 14 (nodejs14.x)
version String The version of HubSpot's serverless function schema. The current version is 1.0 ("1.0").
secrets Array Values used for authentication, such as API keys. This value refers to the name of the secret, not its value. Do not put secret values directly in this file.
environment Object Configuration variables are passed to the execution function as environment variables at runtime. You might use this to add logic for using a testing version of an API instead of the real thing based on an environment variable.
endpoints Object Use this property to create public API endpoints under the domains connected to your HubSpot account.
appFunctions Object Use this property to reference the serverless function as a part of an app extension.

How to start

When thinking of creating a serverless function, you may want to follow the next steps:

  1. Create a project folder
    • Open your hubspot.config.yml file and check if you are connected with your account.
    • Go to your local folders on your computer.
    • Open the folder that contains your hubspot.config.yml file.
    • Here, create a serverless-tutorial folder.
    • Run the watch command so that any edits you do will be automatically uploaded to the design manager:
  2. Create a functions folder
    • Go to the serverless-tutorial folder.
    • Create a my-first-function.functions folder. Files stored here are not publicly accessible.
  3. Create a configuration file
    • Go to my-first-function.functions folder.
    • Create a new file named serverless.json.
    • Write the code below and save the file:
    Cofiguration File
  4. Create a function file
    • Go to my-first-function.functions folder.
    • Create a new file named congratulation.js and write the code below. This is the file that will execute and perform a task.
    • Function File
  5. Register your function in your serverless.json 
    • Go to my-first-function.functions folder.
    • Open the file serverless.json.
    • Find your "endpoints" object.
    • Update the object accordingly:
    • Function Register
  6. Test your function

Access serverless functions

After you have checked the above steps and are sure you have all the resources needed, you can start using them. You can find the serverless functions stored in the developer file system and access them only from the design manager. You can find them inside a functions folder by looking for the suffix .functions in the name.

Please note that all the files stored in this folder are not publicly accessible. You can lock a folder within the design manager by right-clicking the folder and selecting the Lock folder option. This way is the safest method to disable the possibility of accidental edits.

In your functions folder, you can consider having the following:

  • serverless.json file – the config file specifies the runtime environment and any environment variables you plan to use in your functions.
  • .js file – which is your actual serverless function. It must be mapped to an endpoint defined in the previous file.
  • README markdown file – which contains what the functions are for, how they work, and the process build for them.

Using HubSpot serverless functions eliminates the middleware that can appear when you transfer data between your proprietary system and HubSpot. A HubSpot integration helps you focus on the company’s growth.

Book a meeting!



Find more of our latest software development tutorials, launch announcements, and career opportunities.
Read our blog