Skip to content
Thinqzo

[email protected]

Cloudflare Pages Functions for static-site form backends

Engineering

How we wired three form endpoints (newsletter, contact, design partner application) to Zoho CRM and Campaigns using Cloudflare Pages Functions. With Turnstile verification, shared helpers, and graceful degradation.

When we shipped the marketing site for CredoStar, our credentialing product, the pages were static but the forms on them were not. There are three: a newsletter signup, a contact form and a design partner application. Each one has to validate its input, check for bots, push the submission to a CRM and send a response back to the browser.

We built these on Cloudflare Pages Functions. This post walks through the pattern and the specific code we shipped.

What Pages Functions are

A Cloudflare Pages project can include a functions/ directory. Any .js file in there with a default export becomes an HTTP endpoint. The path mirrors the filesystem: functions/api/newsletter.js serves /api/newsletter.

Under the hood these are Cloudflare Workers attached to your Pages project. They deploy with the site, read the same environment variables and write to the same logs, so you don’t need a separate Workers project. For a marketing site, that is all the backend we needed.

The endpoint shape

Each endpoint follows the same steps:

export async function onRequestPost({ request, env }) {
  // 1. Parse JSON body
  // 2. Validate required fields
  // 3. Verify Cloudflare Turnstile token
  // 4. Log the submission (always)
  // 5. Push to Zoho (best-effort)
  // 6. Return success or error
}

The order matters. We log before pushing to Zoho, so if Zoho is down the submission is still in Cloudflare’s function logs. The CRM push is best-effort.

Cloudflare routes by method from the export name: onRequestPost handles POST, onRequestGet handles GET, and so on. A request with a method we didn’t export a handler for never reaches our code, so we didn’t write a method-not-allowed branch ourselves.

Shared helpers

The three endpoints have three things in common:

  1. Building JSON responses (jsonResponse(body, status)).
  2. Verifying Cloudflare Turnstile tokens (call siteverify, return ok or error).
  3. Minting a Zoho OAuth access token from a refresh token.

These live in functions/api/_lib/. The underscore prefix tells Cloudflare not to route them as endpoints. Each endpoint imports what it needs:

import { jsonResponse } from './_lib/response.js'
import { verifyTurnstile } from './_lib/turnstile.js'
import { mintZohoToken, ZOHO_CRM_REGION_HOSTS } from './_lib/zoho.js'

Before we pulled these out, the Zoho token code was copied byte for byte into each endpoint. Now it exists in one place.

Turnstile with a fallback

Cloudflare Turnstile is a bot check that doesn’t make people solve a CAPTCHA. The widget in the browser produces a token, and the server checks that token against Cloudflare’s siteverify endpoint.

Our verifyTurnstile helper has one deliberate quirk. If TURNSTILE_SECRET_KEY is not set, it returns {ok: true, skipped: true} instead of failing. That let us deploy the site before we had provisioned the Turnstile widget in the Cloudflare dashboard. The forms worked end to end without bot protection at first, and once we added the widget, the same code started enforcing it.

export async function verifyTurnstile(env, token, request) {
  if (!env.TURNSTILE_SECRET_KEY) {
    return { ok: true, skipped: true }
  }
  if (!token) return { ok: false, error: 'Missing Turnstile token' }
  // ... siteverify call ...
}

Letting a feature switch itself off when its config is missing saved us more than once during the first deploy. We’d use the same approach for any optional backend.

Zoho OAuth and regions

Zoho uses the standard refresh-token grant. What caught us out is that Zoho runs several data centers, each with its own accounts host and API host, and the region has to match. A token minted on accounts.zoho.com (US) does not work against zohoapis.in (India).

We keep a ZOHO_REGION environment variable (us, eu, in, etc.), and the helper maps it to the right hosts:

export const ZOHO_CRM_REGION_HOSTS = {
  us: 'https://www.zohoapis.com',
  eu: 'https://www.zohoapis.eu',
  in: 'https://www.zohoapis.in',
  // ...
}

export function zohoAccountsHost(region) {
  return `https://accounts.zoho.${region === 'us' ? 'com' : region}`
}

The accounts host uses the region’s TLD, except for the US, which uses .com. It took about ten minutes to work out, and we’d rather not work it out again.

Zoho refresh tokens also carry scope. A refresh token minted with ZohoCRM.modules.leads.CREATE cannot read from Zoho Campaigns. We mint one token with the scopes for both products and use it everywhere.

Zoho Campaigns and the v1.1 endpoint

The Zoho Campaigns docs point you to an endpoint called listsubscribe for adding a contact to a mailing list, so we tried that first.

It returned HTTP 401 with an HTML body, even though our OAuth token and scope were correct. As far as we can tell, that endpoint still expects the older authtoken scheme and was never updated for OAuth.

addlistsubscribersinbulk does the same job (add an email to a list) and accepts OAuth, so that is the one we use. We didn’t find this difference mentioned in the docs.

The response has its own trap. Contacts that Zoho’s anti-spam filter rejects come back in an ignored_contacts array, with HTTP 200. The request succeeded, but the contact wasn’t added. We log that case as a warning rather than a failure.

Logging as the safety net

Before doing anything else, every endpoint writes the full request to Cloudflare’s function logs: timestamp, IP, user agent and every submitted field.

So if Zoho is down, the credentials are wrong or the integration breaks for some other reason, we still have the submission. We can pull it from the logs and enter it by hand.

Cloudflare keeps these logs for a limited window, so they are a short-term net, not an archive. On a low-volume marketing site that has been enough. Each log line has a prefix ([contact], [newsletter-signup], [design-partner-application]), so we can search by form and follow up separately if we need to.

We’d apply this to any best-effort integration: treat the log line as the source of truth and the integration as a convenience.

What we did not need

We considered a few other pieces and left them out:

  • No queue. Pages Functions handle our submission volume synchronously without trouble. A queue would add moving parts we don’t need until we see several requests per second.
  • No database. Zoho is the system of record. The function translates our form schema into Zoho’s API.
  • No retry logic. If a Zoho push fails, we log it and move on. The submission is in our function logs, and we can re-run it by hand. Exponential backoff for an HTTP call that almost always succeeds would be over-engineering.
  • No authentication on the endpoints. These are public forms. Turnstile handles bots; API keys wouldn’t help here.

Deploy

Deploying means pushing to main. Cloudflare’s CI runs npm run build, deploys the static output to Pages and deploys the function bundle to the same project. It takes about 90 seconds from push to live.

Environment variables live in the Cloudflare dashboard under the Pages project’s settings. Secrets are marked as encrypted and are injected into the Workers runtime when the function runs.

For a marketing site with three forms, this is as much backend as we needed, and our traffic is far below the point where we’d have to rethink it. If you’re building a similar site, the pattern is worth copying. The Pages Functions documentation is thin in places, but the pieces themselves have held up well for us.

For the broader stack write-up, see Why we built CredoStar’s marketing site on Astro 5 and Cloudflare Pages.