Include serverless functions in projects (BETA)
In the context of HubSpot projects, serverless functions provide the core functionality for your UI extensions.
Within your project file structure, serverless functions live in the
src/app directory within a .
.functions folder. At the most basic level, this folder should include:
serverless.jsonfile that registers and configures your functions.
2023.2, which was released on November 21, 2023. The documentation below reflects the most up to date serverless function configuration and best practices. Serverless functions created before this will need to be updated to be compatible with the new platform version.
Each serverless function exports a
main function that gets called when HubSpot executes it. The function receives the
context argument, which is an object that contains data based on how the function is being used. This includes context about where the card is loaded as well as the authenticated user and account.
Learn more about fetching user, account, and card location context.
Below is an example of data included in
Below is an example of a function that returns a 200 status code and a Hello World message:
.functions directory, configure the serverless function by including a
serverless.json file. In this file, you'll configure your serverless function to reference the
.js function file to execute or the public URL to request.
Please note: to call an endpoint in a serverless function, your account must have a CMS Hub Enterprise subscription and a Sales Hub or Service Hub Enterprise subscription.
If needing to authenticate requests, you can include secrets in this array. Learn more about managing secrets.
An object containing configuration when you want to hit a public URL. Requires:
When calling an endpoint, you can call the function using any domain connected to your account with the following URL structure:
https://<domain>/hs/serverless/<endpoint-path-from-config>. For example, if both website.com and subdomain.brand.com are connected to the account, you could call the function using
In the URL to call the function, the endpoint path is global rather than scoped to the app or project. If you have identical endpoint paths across multiple apps or projects, the most recently deployed endpoint function will take precedence.
Please note: serverless functions have a response limit of 15 seconds. Functions that take longer to execute will fail.
When developing a private app with projects, each private app comes with a private access token that you can use to authenticate calls to HubSpot's APIs. Calls authenticated with private app access tokens count against your API call limits. You can also authenticate calls using secrets, which you'll manage through the CLI.
For example, when creating a UI extension with Node.js, you'll first include the HubSpot node API client, then instantiate it within
If your serverless function requires secrets, include a
secrets field in the
serverless.json configuration file. Follow the steps below to make them accessible when the function is deployed and when running the local development server.
- Create your secrets by running
hs secrets add <secret name>. HubSpot will securely store this secret on its backend and inject them into the runtime environment when a function is invoked in production.
- In your
serverless.jsonfile, list the names of the secrets needed by each function. Do not include
PRIVATE_APP_ACCESS_TOKEN, as this is automatically created for you and already available in the serverless function.
- To make secrets available for local development, create a
.envfile in the
app.functionsdirectory. HubSpot will never retrieve your secrets outside of its protected infrastructure, so you'll need to specify the secret values that you want to use when the function is executed locally. If your function uses
PRIVATE_APP_ACCESS_TOKEN, you'll need to copy the private app's access token from HubSpot and store it in the
.envfile for local access. The following snippet demonstrates what an example
.envfile might look like:
- After saving secrets to the
.envfile, you can access them in your function using
- To update a secret's value, you can run the
hs secrets updatecommand. If you're using a Node runtime of 14 or higher, updated secret values will automatically be updated in your deployed function within one minute, meaning you won't have to build and deploy to get the updated secret.
- To limit exposure of a secret, it's strongly recommended to never include it in console statements to prevent it from being recorded in logs.
- If your project is linked to a GitHub repository, be sure to never commit the
.envfile when uploading to GitHub. You can include an entry for
.gitignorefile to ensure that it is omitted from your commits.
Keep the following recommendations in mind while you develop and test your serverless function:
To ensure that variables are correctly assigned and initialized with every function invocation, you should opt to assign variables within the function itself. This practice prevents potential issues related to stale or persistent variable states, which can lead to unexpected behaviors. See the example below for additional context:
hsprojects.json configuration file includes a
platformVersion field which specifies which platform version to run the project on. It's strongly encouraged to use the latest platform version to ensure that your project and its assets are up to date with the latest improvements, optimizations, and feature enhancements. In addition, some previously available features may not be available in older versions due to deprecation.
The platform version also dictates which version of Node the project runs on. The latest version,
2023.2, uses Node18, and doesn't support older versions of Node.
By default, HubSpot provides a small number of NPM dependencies in addition to the Node.js standard library. To add your own dependencies, you can list the package in
dependencies within the
package.json file. When the app is built, dependencies will be bundled with your function code. All dependencies must be published to NPM and be public.
For example, if you wanted to add the lodash library in a serverless function, you would first update
package.json to include the dependency:
In HubSpot, you can view a serverless function's log history, including both successful requests and errors. When a CRM card fails to load, HubSpot will also display an error message in the card on the CRM record that links to that app's CRM card logs.
To access a serverless function's logs in HubSpot:
- In your HubSpot account, navigate to CRM Development.
- In the left sidebar menu, navigate to Private apps.
- Select the private app that contains the serverless function.
- Click the Logs tab, then the CRM cards tab to view serverless function logs.
- To view logs for a specific request, use the search bar to search by request ID, or click a request.
You can also include
console.log() in your serverless function code for debugging purposes, then view its output in the function log details sidebar.
Log messages are produced every time HubSpot executes a serverless function. To view a serverless function's logs for both API endpoint and app functions in the CLI, run the
hs project logs command. This guide you through selecting the project, app, and function name or endpoint to get logs for. Learn more about using the hs project logs command.
There are two types of log messages produced:
- Log messages that record the execution of a function, along with its status and timing. For example:
2021-04-28T19:19:21.666Z - SUCCESS - Execution Time: 279ms
A log output for the above code would then produce the following:
2021-04-28T19:15:13.200Z INFO Log some debug info
2021-04-28T19:15:14.200Z ERROR An error occurred
Thank you for your feedback, it means a lot to us.