Every website eventually needs a piece of logic that cannot run in the browser: a call to an external API with a key you cannot expose, a form that writes to a system the visitor should never touch directly, a calculation that depends on data the page cannot see. The classic answer is to stand up a server somewhere and maintain it forever. On Content Hub, there is a different answer: serverless functions, small pieces of backend code that HubSpot hosts, runs and scales for you, called from your pages through an endpoint.
They are powerful and they are bounded, and knowing the boundaries is what separates a function that quietly serves a site for years from one that fails under the first real load. In this guide, I will cover what a serverless function is on HubSpot, how it is configured and reached, how secrets keep credentials out of the code, the execution limits that shape what you should and should not put in one, and the line past which the logic belongs in an external service instead. Functions are one piece of the larger map of development on HubSpot Content Hub, next to themes, modules and HubDB, and they make more sense once the rest of that map is familiar.
Part of the guide: HubSpot implementation: the guide and checklist
What a serverless function is on HubSpot
A serverless function is JavaScript that runs on HubSpot's infrastructure in response to a request, with no server for you to provision. You write the function, declare it in a configuration file, and HubSpot exposes it at an endpoint your pages can call. It has access to secrets you store securely, so it can talk to the HubSpot APIs and to external systems with credentials the browser never sees. The common uses are exactly the ones a browser cannot do safely: secure API calls, writing to the CRM, and building interactive experiences on top of HubDB data.
There are two flavors depending on where you build. Functions created through the design manager live in a folder with a .functions suffix and are reached under a path beginning with /_hcms/api/. Functions built as part of a project on the current developer platform are configured in the project and reached under a different path, /hs/serverless/. The concepts are the same; the configuration format and the endpoint path differ, so check which flavor your project uses before copying an example.
The configuration file and the endpoint
A serverless.json file is what turns a folder of code into callable functions. It declares the runtime, Node.js, an optional environment object, the list of secret names the functions may read, and the endpoints object that maps each endpoint path and HTTP method to the file and exported function that handles it. One file can declare many endpoints, and the endpoint name becomes part of the URL your front end calls.
{ "runtime": "nodejs20.x", "version": "1.0", "secrets": ["EXTERNAL_API_KEY"], "endpoints": { "quote": { "method": ["POST"], "file": "quote.js" } } }
Two rules about this file prevent the most common mistakes. Secret values never go in it, only secret names; the values live in HubSpot's secret store. And in project-built functions, the private app access token is injected for you automatically, so you do not list it in the secrets array. The front end then calls the endpoint with a normal fetch, and the function runs on HubSpot's side with the credentials it needs.
Secrets: credentials the browser never sees
The whole point of moving logic to a serverless function is that the browser cannot be trusted with a credential, so the function has to get its credentials from somewhere safe. That is the secrets store. You create a secret with hs secrets add followed by the secret name, HubSpot stores the value, and at runtime it injects the value into the function's environment for the secrets named in the configuration. The code reads it as an environment variable and never contains it.
This gives you rotation for free. When a key changes, you update the secret in one place and every function that references it picks up the new value, without editing code or redeploying. An account can hold a limited number of secrets, so name them by purpose rather than creating one per function, and treat the list in serverless.json as the explicit grant of which function may read what.
The limits that shape what belongs in a function
Serverless functions have hard limits, and they are the design constraint, not an afterthought. Each execution has a maximum of ten seconds. Each account has a budget of six hundred execution seconds per minute across all its functions, which is sixty functions running the full ten seconds, or six thousand running a tenth of a second. Each function gets 128 megabytes of memory, payloads are capped at six megabytes per invocation, and log output is limited to about four kilobytes.
Read those numbers as a definition of the tool. A function is for a fast, bounded piece of work: validate and forward a submission, fetch a record and shape it, call one external API and return the result. It is not for a batch job, a long sync, or anything that waits on a slow system, because the ten seconds includes every network call you make. Set explicit timeouts on outbound requests well under the limit, log only what you need, and if the work does not fit, the limit is telling you it belongs elsewhere.
A tip from someone who has been burned: the six hundred seconds per minute is an account-wide budget, not a per-function one. A single function that takes nine seconds and gets called by every page load can exhaust the whole account's budget and take down every other function on the site. Measure how long each function actually runs and how often it is called, and keep the product of the two far below the budget.
When the logic belongs in an external service
There is a clear line past which a serverless function is the wrong tool. If the work regularly approaches the ten-second ceiling, if it needs to process many records, if it must wait on slow external systems, or if it needs retries, queues and state between runs, it belongs in an external service: a queue and a worker, an automation platform, or a conventional backend. The function can still be the front door that receives the request and hands it off, but the heavy work runs where there is no execution ceiling. And when the trigger is a CRM event and not a request from a page, the closer fit is usually custom code actions in workflows, which come with their own secrets, limits and retry behavior.
Availability matters here too. Serverless functions on Content Hub are an Enterprise-tier feature, and invoking a function by a public URL is tied to that tier. If your account is on Professional, the logic has to live in an external service from the start, triggered from the page or from a workflow. Confirm the tier before you design an architecture around functions.
Why this matters for your operation
Serverless functions let a marketing site do things a brochure cannot: a pricing calculator that calls the real pricing system, a registration form that writes to the CRM with validation, a dynamic page that queries data the browser could not be trusted with. All of that without a server for IT to maintain, which is why the feature exists. Used within its limits, it is the cheapest backend a site can have.
Used past its limits, it is the most frustrating one, because the failures are quiet: a function that times out under load, a budget exhausted by one chatty endpoint, a secret pasted into code because the store felt like extra work. The discipline is small, keep functions fast and bounded, keep credentials in secrets, watch the account budget, and move heavy work out, and it is what keeps the backend invisible in the good sense.
In practice: the calculator that took down the forms
A company built a pricing calculator as a serverless function that called an external pricing API on every keystroke. Each call took several seconds, the API was slow, and the function had no timeout. On a busy afternoon the calculator alone consumed the account's execution budget, and the registration form, which also ran through a function, started failing for visitors who had nothing to do with pricing.
The fix had two parts. The calculator gained a client-side debounce so it called the function once per pause instead of once per keystroke, and the function gained a tight timeout on the external call. The pricing lookups were then moved to a cached external service the function could answer from in milliseconds. The account budget dropped to a fraction of its ceiling, the forms came back, and the calculator got faster. The functions were never the wrong tool; the unbounded work inside one was.
Checklist for serverless functions on HubSpot
- Is your account on the tier that supports serverless functions and public endpoints?
- Does serverless.json declare the runtime, the secret names and the endpoints, with no secret values in it?
- Are credentials stored with hs secrets add and read as environment variables, never in code?
- Does every function finish well under ten seconds, with explicit timeouts on outbound calls?
- Is the product of execution time and call frequency kept far below the six hundred seconds per minute account budget?
- Have you moved batch, long-running or retry-heavy work to an external service, with the function only as the front door?
Frequently asked questions
What can a HubSpot serverless function do?
Run backend JavaScript on HubSpot's infrastructure in response to a request from your pages: call external APIs with credentials the browser never sees, write to the CRM, and build interactive experiences on HubDB data, without a server to maintain.
What are the execution limits?
Ten seconds per execution, six hundred execution seconds per minute across the whole account, 128 megabytes of memory, six megabytes per payload and about four kilobytes of log output. Design functions to be fast and bounded.
How do I store an API key for a function?
With hs secrets add and the secret name. HubSpot stores the value and injects it at runtime for functions that list that name in serverless.json. The key never appears in the code or the config file.
Which URL does my function answer at?
Design manager functions answer under /_hcms/api/ and project-built functions under /hs/serverless/, each followed by the endpoint name. Check which flavor your project uses.
Do serverless functions require an Enterprise subscription?
Yes. Serverless functions on Content Hub are an Enterprise-tier feature, and invoking a function by a public URL is tied to that tier. Confirm the tier before designing around them.
When should I not use a serverless function?
When the work approaches the ten-second ceiling, processes many records, waits on slow systems, or needs queues and retries. Then the function should only receive the request and hand it to an external service that does the heavy work.
Let's talk? Want backend logic on your HubSpot site that is secure, fast and within its limits? At Insight Sales we design and build serverless functions and the services behind them. Talk to our team and find out how we can help.