Getting started with local development
The CMS CLI connects your local workflow to HubSpot, allowing you to utilize version control, your favorite text editor and various web development technologies when developing on the HubSpot CMS.
In this guide, we'll walk through installing the basics, fetching a project from your HubSpot account, and then sync changes back to your Design Tools. Starting a brand new project or website? Go through the quick start guide to building a website on the HubSpot CMS instead to get set up with local development and deploy a boilerplate theme.
To use the local tooling, you'll need to:
- To install the HubSpot tools globally, run npm install -g @hubspot/cms-cli in your command line. To install the tools in your current directory, run npm install @hubspot/cms-cli.
Create a folder for your local project. For example, run mkdir local-cms-dev in the command line, which will create a directory. Then, run cd local-cms-dev to navigate to that directory.
Run hs init to connect the tools to your HubSpot account. This command will walk you through the following steps:
- First, you’ll enter a name for the account. For example, you might use "sandbox" if you're using a developer sandbox or "company.com" if you’re using a full customer account. This name will be used when running commands.
- You’ll need a personal CMS access key to enable authenticated access to your account via the local development tools. In the first step, you’ll be prompted to press Enter when you’re ready to open a secure page in your default browser. This page will allow you to view or generate your personal access key, if necessary. (Note: You’ll need at least the Design Manager permission in order to complete this tutorial.) Copy your access key and paste it in the terminal.
Once you've completed this simple init flow, you'll see a success message confirming that a configuration file, hubspot.config.yml, has been created in your current directory.
Your hubspot.config.yml will look something like this:
defaultPortal: DEV portals: # Sample apikey entry, manually entered - name: DEV portalId: 123 authType: apikey apiKey: 'xxxxxx-xxxxx-xxxxx-xxxxx-xxxxx' # Sample personal access key entry, generated from running `hs auth personalaccesskey` - name: PROD portalId: 345 authType: personalaccesskey personalAccessKey: >- xxxxxxxxx-xxxxxxx-xxxxxxx-xxxxxxx-xxxxxxxx auth: tokenInfo: accessToken: >- xxxxxxxxx-xxxxxxx-xxxxxxx-xxxxxxx-xxxxxxxx expiresAt: '2020-02-24T19:38:39.164Z'
In the above example, you'll notice multiple entries are supported in the hubspot.config.yml file. You can optionally add a name to an account entry. When running commands, either name or portalId can be used in the --portal= option (ex. --portal=123 and --portal=DEV will both interact with account ID 123 based on the above example). If you set a top-level defaultPortal, you can exclude the --portal= option from your commands, but commands will reference that default portal.
Using the fetch command, run hs fetch --portal=<name> <src> <dest> to get an entire projects folder tree and files from your HubSpot account into your local environment. The src is the project path in your HubSpot Design Tools, and the dest is the path to the local directory you would like the files to be placed, relative to your current working directory. To fetch the project to your current working directory, you can leave out the <dest> argument.
Here is an example of how you would fetch a project from your HubSpot account:
hs fetch cms-project
This is the Design Tools project we fetched to our local environment:
Using the upload command, run hs upload --portal=<name> <src> <dest> to upload the project in your local environment to your HubSpot account. This will copy the contents of the <src> and upload them to your HubSpot account in the <dest>. If you're creating a new project in your Design Tools, the <dest> can be a net new folder in HubSpot.
Here is an example of how you would upload the example project into your HubSpot account:
hs upload cms-project cms-project
Run hs watch --portal=<name> <src> <dest> to watch your local directory and automatically upload changes to your HubSpot account on file saves.
Here is an example of how you would run that command with the watch command:
hs watch cms-project cms-project