CMS Source Code

Basic Features

The CMS Source Code API allows you to interact with the files stored in your HubSpot Developer File System. These files include all of the templates, modules, CSS, JS, and other CMS assets seen in the Design Manager. Through this API, you can:

  • Upload new files to their HubSpot account, or upload changes to existing files.
  • Download or delete CMS assets from their account.
  • Fetch metadata for each file or folder in the Design Manager.
  • Validate file contents for use in HubSpot CMS, including HubL syntax.
  • Upload zip files as file packages and extract them inside the account.

With this API you can edit all your CMS assets locally as well as build tooling to help you move even faster. 

Environment and path

The Source Code API endpoints use the environment and path parameters to identify files in the CMS Developer File System. These parameters are generally specified in the endpoint path itself, as in /cms/v3/source-code/{environment}/content/{path}.

The environment parameter refers to whether you are interacting with the unpublished or live version of each file. For unpublished changes, use draft. For live changes, use published.

Note that uploading to published is equivalent to pressing the “Publish” button in the Design Manager. As such, whatever is currently in draft will be cleared.

The path parameter refers to the location of the file in the CMS Developer File System. Top level assets are not preceded by a / character as you would see in a UNIX based operating system. When uploading a new file, this should be the desired location where the file should be created. When retrieving or uploading changes to an existing file, this should match the path of the existing file.

We use the local file formats for all asset types, which means that certain types of assets are broken up into multiple files. For instance, a module is actually represented as a directory ending in the .module suffix, so to retrieve the HTML for a module, one would need to use the path foo.module/module.html. See the local development documentation for further information.

Downloading a file

You can download a file from their HubSpot account using the /cms/v3/source-code/{environment}/content/{path} endpoint.

To do this, use an HTTP GET request which accepts the application/octet-stream content type. This will allow you to download the actual binary file contents of the given path.

Example:

  • Downloading a file:
    GET /cms/v3/source-code/published/content/my-file.js
    Accept: application/octet-stream

Note that you cannot download the entire contents of a folder. Instead, you must fetch the folder metadata (see below) and retrieve each of its children individually.

Fetching file and folder metadata

A developer can fetch metadata (such as path, filename, and created/updated timestamps) for a file or folder in their HubSpot account using the /cms/v3/source-code/{environment}/metadata/{path} endpoint.

To do this, use an HTTP GET request which accepts the application/json content type. This will allow you to fetch a JSON object containing the relevant metadata for that file.

Folder metadata will be indicated by the folder: true property. The children array will show the names of files and subfolders within the folder. These filenames can be used to traverse the folder tree: simply fetch one folder metadata and recursively fetch the children of the folder and all subfolders.


Example:

  • Fetching the metadata for a folder:
    GET /cms/v3/source-code/published/metadata/my-folder
    Accept: application/json

Uploading a file

You can upload a local file to their HubSpot account using the /cms/v3/source-code/{environment}/content/{path} endpoint.
The file must be uploaded via an HTTP PUT request using the multipart/form-data content type. The binary file data must be included as a field named file.

Examples:

  • Uploading a new file:
    PUT /cms/v3/source-code/published/content/my-new-file.html
    Content-Type: multipart/form-data
    Form Data: { file: [binary file data] }
  • Updating an existing file draft:
    PUT /cms/v3/source-code/draft/content/path/to/existing-file.html
    Content-Type: multipart/form-data
    Form Data: { file: [binary file data] }

HubSpot currently supports the following CMS asset file types:

  • css
  • js
  • json
  • html
  • txt
  • md
  • jpg
  • jpeg
  • png
  • gif
  • map
  • svg
  • ttf
  • woff
  • woff2
  • zip

Validating file contents

You can validate the contents of a local file using the /cms/v3/source-code/{environment}/validate/{path} endpoint.

This can be used to validate HubL in a template/module or JSON for a theme or module. This displays the warnings and errors you would see within the Design Manager.

Note that invalid files will be rejected if you try and publish them directly. It is recommended to validate files first before publishing.

The file must be sent via an HTTP POST request using the multipart/form-data content type. The binary file data must be included as a field named file. If there are validation errors, you will receive a 400 response with the list of relevant errors.

Example:

  • Uploading a new file:
    POST /cms/v3/source-code/published/validate/my-file.html
    Content-Type: multipart/form-data
    Form Data: { file: [binary file data] }

Deleting a file

You can delete a file from their HubSpot account using the /cms/v3/source-code/{environment}/content/{path} endpoint via an HTTP DELETE request.

Deleting from the published environment will remove the file entirely, while deleting from the draft environment will simply clear out any unpublished changes. Note that deleting published files will immediately impact live content if used anywhere, so make sure to remove all existing references to the file before deleting.

Example:

  • Deleting a file:
    DELETE /cms/v3/source-code/published/content/my-file.html

Extracting a file package

You can extract a zip file in their HubSpot account using the /cms/v3/source-code/extract/{path} endpoint via an HTTP POST request.

The path in this case must be a zip file already uploaded to their account. The extraction process is asynchronous and can take up to a minute depending on how many and how large the compressed files are. The contents of the zip are extracted in place to the same folder that contains the zip file, and the original zip file is not deleted automatically upon successful extraction.

Example:

  • Uploading a zip file:
    PUT /cms/v3/source-code/published/content/my-package.zip
    Content-Type: multipart/form-data
    Form Data: { file: [binary file data] }
  • Extracting a zip file:
    POST /cms/v3/source-code/extract/my-package.zip

Was this page helpful? *
This form is for feedback on our developer docs. If you have feedback on the HubSpot product, please share it in our Idea Forum instead.