Hubspotsherpa

How to Use Serverless Functions in HubSpot

⚠️ Note that this post hasn't been updated for at least a year and the information may be outdated, proceed with caution! (Last updated: February 2, 2025 )

As technology continues to evolve, companies are constantly developing innovative solutions with the future in mind. One such advancement in web development is serverless functions.

Have you ever struggled with server management while hosting your website? As your site grows, maintaining, updating, and managing your server can become increasingly time-consuming and complex.

Now, imagine if all of that was handled for you—allowing you to focus entirely on writing your code. Sounds great, right? This is exactly where serverless functions come in!

In this guide, I’ll walk you through the key aspects of serverless functions in HubSpot and show you how to create them effortlessly within HubSpot CMS.

What are Serverless Functions in HubSpot?

Serverless functions in HubSpot let you write server-side code that interacts with HubSpot and third-party services via APIs, enabling dynamic components.

Built into HubSpot, serverless functions run backend code without requiring server space. They work with HubDB to store data and execute in Node.js.

After facing challenges with my first serverless function, I created this guide to help fellow HubSpot developers. Let’s dive in!

Why use Serverless Functions?

  • Serverless functions enable faster, simpler, and automated code deployment.
  • They reduce the risk of downtime during deployment.
  • These functions improve application performance and enhance the user experience.
  • Serverless architecture is cost-effective, as most platforms follow a pay-as-you-go model, ensuring you only pay for what you use.
  • With lower overhead, serverless functions make it easier to scale as your business grows.

How to create Serverless Functions?

Open Design Tools
  • Navigate to Design Tools in your HubSpot account.

Create a Functions Folder
  • Click File and create a new folder named functions.

How o create serverless funcitons in hubspota

Add Necessary Files
Inside the functions folder, create:
      • serverless.json (defines function endpoints)
      • hello-world.js (holds backend logic)

How o create serverless funcitons in hubspota
 
serverless.json
{
  "runtime": "nodejs18.x",
  "version": "1.0",
  "environment": {},
  "endpoints": {
    "hello-world": {
      "method": "GET",
      "file": "hello-world.js"
    }
  }
}
hello-world.json
exports.main = (context, sendResponse) => {
  sendResponse({
    body: {
      response: "Hello, World!"
    },
    statusCode: 200
  });
};
Hello, World! (GET)

A simple function that returns JSON, and console.logs "Hello, World!". This function is designed to get you up and running with your function as quick as possible, showing how to return data from your function, and log data as part of the function execution.

Hitting the endpoint

The endpoint is executed with the GET request method:

  GET /_hcms/api/hello-world
In vanilla JavaScript, this might look like:
fetch('/_hcms/api/hello-world')
.then(response => response.json())
.then(data => {
    console.log(data)
})
.catch(error => console.error(error))
How o create serverless funcitons in hubspota
Outpute :

Hello, World!

How o create serverless funcitons in hubspota

 

Key Considerations:

  1. Performance and Limitations

    • Serverless functions are fast and designed for quick responses, ideal for front-end interactions.
    • HubSpot functions are limited to:
      • 50 secrets per account
      • 128MB memory
      • 100 endpoints per account
      • Must use application/JSON content type.
  2. Execution Limits

    • Max execution time: 10 seconds per function.
    • Total execution time per account: 600 seconds per minute.

    Examples of usage within limits:

    • 60 executions, each taking 10 seconds
    • 6,000 executions, each taking 100 milliseconds

    Exceeding these limits results in a 429 error, with logs available for tracking execution time.

Got Questions? We’ve Got Answers!

What are Serverless Functions in HubSpot?

They allow you to run backend code on-demand without managing servers, enabling dynamic interactions with HubSpot APIs and third-party services.

How do Serverless Functions improve performance?

They automatically scale based on demand, offering faster deployment and improved performance without worrying about server capacity.

Are there limitations with Serverless Functions?

Yes, with a 10-second execution limit, 600 seconds total execution per minute, 128MB memory, and a max of 100 endpoints and 50 secrets per account.

How do I test a Serverless Function?

Test by calling the function's endpoint in the browser or using a template with the fetch API, and view the response in the browser console.

How do I handle errors?

Functions that exceed limits return a 429 error. Check logs for details and adjust your code accordingly to handle errors gracefully.

Contact

Work with a HubSpot CMS Expert

  • Tailored HubSpot themes, user-friendly reusable templates, and custom modules designed for your needs.
  • Providing technical support and expert guidance to help you master the HubSpot CMS.