Integrating Hubspot Forms with Gatsby

Date: December 9th, 2019

Topics: Gatsby, React, Hubspot

Hubspot is a really awesome CRM tool that connects potential customers to any number of digital touchpoints in your websites & applications. Recently I was tasked with a Hubspot integration on a Gatsby build for a client.

For this project, we needed to hook up their forms feature to forms on the site. Typically when integrating with a third party form provider, there are a couple routes. You can either use embeddable code provided by the vendor, or if available, post data from your own UI elements to some API. Hubspot has both options, but their form embed code is just a javascript tag, and I'd like to load as few vendor scripts into the site as possible. Plus I prefer the control of writing my own components - so let's demo how to post form data to their API.

In this demo I'm just going to post straight to the Hubspot endpoint because no auth is required, but there actually is a web client on Github if you prefer that. I messed around with it for a few minutes but kept hitting some odd response codes. It'll probably work just fine for someone with a little more patience.

One dependency we'll be adding to the gatsby project is gatsby-plugin-hubspot -- which just loads Hubspot's basic tracking pixel elegantly. You'll want this for a couple reasons I'll outline in the snippet below. You'll also need to create the actual form you want to post data to in the Hubspot form builder.

Here's a stripped down demo of what my contact form component looks like. Hope this helps!

import React, { Fragment, useState } from 'react';
import { graphql, useStaticQuery } from 'gatsby';

// You'll need some polyfilled fetching library, 
// if thats your jam.  In addition, something to easily
// grab cookies from the browser. 

import fetch from 'isomorphic-unfetch';
import Cookies from 'js-cookie';

// This is what a headless CMS query could
// look like for the purposes of the demo. I 
// set this up so my client could change out forms 
// from an admin panel 

const contactQuery = graphql`
    cmsContact {

export default function ContactForm() {
  const [loading, setLoading] = useState(false);
  const [success, setSuccess] = useState(false);
  const [error, setError] = useState(false);
  const [firstname, setFirstname] = useState('');
  const [lastname, setLastname] = useState('');
  const [message, setMessage] = useState('');
  const {
    cmsContact: { formSuccessMessage, formErrorMessage, formId, portalId },
  } = useStaticQuery(contactQuery);

  const submitForm = e => {
    if (e) e.preventDefault();
		// In Gatsby, we're building our static site in a Node
		// environment, rather than a browser environment, so anything
		// browser related needs to be wrapped in some version of the
		// following isBrowser variable. 
		// What we're getting here is some contextual data to send 
		// along to Hubspot so it can organize and track forms as 
		// they relate to specific users in the CRM.  The Hubspot 
		// plugin we installed earlier provides this hutk value as a cookie.
		// pageName & pageUri should be pretty self explanatory. 
    const isBrowser = typeof window !== 'undefined';
    const hutk = isBrowser ? Cookies.get('hubspotutk') : null;
    const pageUri = isBrowser ? window.location.href : null;
    const pageName = isBrowser ? document.title : null;
    const postUrl = `${}/${portalId}/${formId}`;

		// This object shape is what's required in the Hubspot API 
		// documentation.  There's an additional legalConsentOptions object 
		// I've left out here, but would be important if you're working on a 
		// legitimate organization's site & they want to be safe from any sort 
		// of GDPR guff. 
		// Read more here:

    const body = {
      fields: [
          name: 'firstname',
          value: firstname,
          name: 'lastname',
          value: lastname,
          name: 'message',
          value: message,
      context: {

		// These specific headers are required for whatever reason,
		// so don't forget to include them. 
    fetch(postUrl, {
      method: 'post',
      body: JSON.stringify(body),
      headers: new Headers({
        'Content-Type': 'application/json',
        Accept: 'application/json, application/xml, text/plain, text/html, *.*',
      .then(res => res.json())
      .then(() => {
      .catch(err => {
	// Finally, the data-form-id and data-portal-id attributes 
	// don't actually do anything for us in terms of getting our 
	// data from A to B.  However, if these aren't included on the 
	// form element, Hubspot gets a little confused when labelling 
	// submissions in the dashboard, so be sure to include these attrs 
	// if you don't want your submission names looking like styled-component
	// class names a la "New Submission from Contact__Form__xydhf2_kskl"

  return (
          {success && (
          {!success && (
                  <Label htmlFor="contact-first-name">First name *</Label>
                    onChange={e => setFirstname(}
                  <Label htmlFor="contact-last-name">Last name *</Label>
                    onChange={e => setLastname(}
                <Label htmlFor="contact-message">Message *</Label>
                  onChange={e => setMessage(}
              {error && (
              <Button type="submit">