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.
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!
{ "runtime": "nodejs18.x", "version": "1.0", "environment": {}, "endpoints": { "hello-world": { "method": "GET", "file": "hello-world.js" } } }
exports.main = (context, sendResponse) => { sendResponse({ body: { response: "Hello, World!" }, statusCode: 200 }); };
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.
The endpoint is executed with the GET request method:
GET /_hcms/api/hello-world
fetch('/_hcms/api/hello-world') .then(response => response.json()) .then(data => { console.log(data) }) .catch(error => console.error(error))
Hello, World!
Performance and Limitations
application/JSON
content type.Execution Limits
Examples of usage within limits:
Exceeding these limits results in a 429
error, with logs available for tracking execution time.