Create your first serverless function on the HubSpot CMS, and get a response from it.
hs accounts list
. Learn more about getting started with local development.Create a project locally
cd
command.hs project create
to create a new project.cd
command. For example, if you named your project my new project:Add a serverless function to the project
src
directory and a hsproject.json
configuration file. To add a serverless function to the project, create an app
directory inside the src
directory, then add the following directories and files within it:app/app.json
: the app configuration file.app/app.functions
: the serverless function directory. You can use any name you’d like, as long as it ends in .functions
.app/app.functions/function.js
: the JavaScript code that will be executed when the function is invoked.app/app.functions/serverless.json
: the serverless function configuration file.app/app.functions/package.json
: includes necessary dependencies.app.json
, function.js
, serverless.json
, and package.json
files. Under each .json
code block, you’ll also find a table containing field definitions.Field | Type | Description |
---|---|---|
name | String | The name of the app, which will display in HubSpot’s UI. |
description | String | The app’s description, which will display in HubSpot’s UI. |
scopes | Array | The scopes that the app has access to for authenticating requests with the private app access token. The two scopes above are the minimum required scopes. No additional scopes are added for this tutorial, as you won’t need to use the private app access token for the request you’ll be making. |
uid | String | The app’s unique identifier. This can be any string, but should meaningfully identify the app. HubSpot will identify the app by this ID so that you can change the app’s name locally or in HubSpot without removing historical or stateful data, such as logs. |
public | String | Set to false for private apps. |
Upload the project to HubSpot
hs project upload
.hs project open
to view the project in HubSpot.Test the function
https://<domain>/hs/serverless/<endpoint-path-from-config>
.<domain>
: you can use any domain connected to the account. For example, if both website.com and subdomain.brand.com are connected to the account, you could call the function using https://website.com/hs/serverless/<path>
or https://subdomain.brand.com/hs/serverless/<path>
.<endpoint-path-from-config>
: the value in the path
field in serverless.json
.https://<domain>/hs/serverless/fetch-quote
.<hubId>.hs-sites.com
(e.g., 123456.hs-sites.com
). Your browser should display the data returned by the Zen Quotes API.Invoke the function from a website page
hs project create
.
cd
command.
hs create template "serverless-template"
to create a new template named serverless-template
.<domain>
in the function endpoint URL (fetch('http://<domain>/hs/serverless/fetch-quote')
) with a domain that’s connected to your HubSpot account.hs upload
to upload it to HubSpot. Follow the terminal prompts to select the local source and destination path. For this tutorial, you can just press Enter for each prompt to use the default paths.hs open website-pages
.hs open --list
.fetch('http://<domain>/hs/serverless/fetch-quote')
) to use the domain that you select in this step.module.html
file, and the JavaScript into the module.js
file.