← WritingCode

A contact form on a Cloudflare Worker that sets up deals in HubSpot

My Consulting page's form runs on one small Cloudflare Worker in front of a static site, and sets up a contact and a deal in HubSpot automatically.

6 min read

Until now, every button on my Consulting page was a plain email link, and an enquiry only made it into HubSpot if I copied it in myself. Now there's a short form at the bottom of the page, and when someone sends it, the contact and a deal are set up in HubSpot automatically, in the first stage of my pipeline.

This site is entirely static, with no server and no database, and the form is one small script on Cloudflare Workers® sitting in front of it. That's the kind of creative solution I want to show here: hosting a static website and still giving it some dynamic functionality and integrations, without a server to run, and with no cost for any of it, on the Cloudflare free plan and HubSpot's free tier.

The path an enquiry takes: the form on the Consulting page posts to /api/contact/, the site's Worker checks the fields and the spam traps, then searches HubSpot for the contact by email, creates the contact if it's new, and opens a deal on it in the first stage of the pipeline. Every other request goes straight to the static site.

Why a Worker, and why now

I changed my mind about having a form because I realized, after I'd moved the site to Cloudflare, that there was a solution for this that didn't cost anything. I probably could have solved it with a very small cost in AWS, using Lambda functions or something like them, but AWS is just so complicated. Cloudflare is a bit newer for me, and I was exploring it as a product, and I was really happy with what I was able to achieve. I liked that there was a no-cost solution that would ultimately give me better insights into running my business.

The site used to have HubSpot's own forms, and they protected it pretty well, but I removed the HubSpot integration from the site when I did the redesign recently, because I really wanted to slim the front end down a little bit. I didn't want to rewrite the template in HubSpot, and the integration never really did feel like it fit the site overall. I wanted full control over it, so I pulled it all out after the rewrite, in favor of Plausible Analytics and a simpler marketing site overall. This form gets the enquiries back into HubSpot without putting any of HubSpot's code back on the page.

One small script in front of a static site

The site is built with Jekyll, and since the move to Cloudflare it's served as static assets by a Worker. Until now that Worker had no script at all. The form gave it its first one, and the config makes sure that script only ever sees requests under /api/:

"main": "workers/site/index.js",
"assets": {
  "directory": "./_site",
  "binding": "ASSETS",
  "run_worker_first": ["/api/*"]
}

Every page, every header and redirect rule, and the 404 page are still served by the assets exactly as they were before, so the rest of the site doesn't know the script exists.

One detail is worth passing on, because it would have been easy to miss. The form posts to /api/contact/, with a trailing slash, because the site has a rule that adds the slash to any URL without one. That rule answers with a 301, and a 301 turns a POST into a GET, so a form posting to /api/contact would quietly lose everything the visitor typed.

What the form asks for

It asks for a name, an email address, a subject and a description of the project, and those four are required. Company is optional, and so is a short dropdown for when you need it: Immediate, Within the next 30 days, Within the next 3 months, or Unsure.

The description has to be at least 100 characters, so every deal starts with something to go on, and a count under the box shows how far along you are. The email address is checked as you leave the field, with the page's own pattern, because a browser's built-in check lets something like ada@example straight through.

Checking everything twice

Everything the page checks, the Worker checks again, because anything can post to a URL. The tests make sure the page and the Worker agree on the email pattern and the timeline's choices, so the two can't drift apart.

Without HubSpot's forms, the spam protection is the Worker's job too, and it's a few quiet checks. The request has to come from the site itself, a hidden field people never see has to stay empty, and the form has to be sent at least three seconds after the page loaded, since a person takes longer than that and a script usually doesn't. A bot that trips one of these is told its message went through, so it has nothing to learn from.

The form works without JavaScript too. With script, it sends in the background and shows the thank-you line in place. Without it, the form posts normally and the Worker answers with a redirect, to a small thank-you page if it worked, or to the Contact page, which has my email address, if it didn't.

Setting up the contact and the deal

Once a message passes, the Worker makes up to three calls to HubSpot's API. It searches for the contact by email first. If they're new, it creates the contact as a new lead, with me as the owner. If they're already in HubSpot, it leaves the record alone, apart from filling in a company if the record doesn't have one, because the CRM is where I keep what I already know about them.

How the form's fields land in HubSpot: name, email and company go on the contact. The deal is named with the company, or the person's name, then a colon and the subject, and its description holds who sent it, the timeline and the project description.

Then it opens a deal on that contact, in the first stage of my custom software pipeline. The deal is named for the company, or the person if they didn't give one, followed by their subject, and its description starts with who sent it and their timeline before their own words. That way I can read the whole enquiry from the deal without going anywhere else. You can try it here, and nothing you type leaves the page:

Try it. Nothing leaves this page.

Deals

First stage

A preview of the deal the form sets up. The real form is on the Consulting page.

Keeping it private

The Worker talks to HubSpot with an access token limited to the four scopes it needs: reading and writing contacts, and reading and writing deals. The token never goes in the repo. It's a secret on the Worker, set once from the command line:

npx wrangler@4 secret put HUBSPOT_ACCESS_TOKEN

Because the form carries people's own words about their projects, nothing is logged, and the Worker's logs and traces are switched off.

All of it is covered by 27 tests that run on every pull request with HubSpot stubbed out, so nothing reaches my real account while I'm working on it. After each deploy, one last step checks that /api/contact/ answers a plain GET with a 405, which tells me the form's script is live.

Where it lands

HubSpot gives me a single place to keep track of my consulting activity, and I've been using it for a really long time, since the end of 2016. I love having that Kanban overview board of potential deals for staying organized, and now an enquiry from the Consulting page shows up there as a new card, in the first column, without me copying anything in.


Cloudflare and Cloudflare Workers are trademarks and/or registered trademarks of Cloudflare, Inc. in the United States and other jurisdictions.

A playbook for launching your next app

20 Million Impressions

How I went from an idea on a walk to 20 million App Store impressions 53 days later.

Thinking about your next app? This is the playbook for taking it from idea to launch.

$24.99 $49.99 · 50% off at launch