Skip to content

Verify HubSpot webhooks

Verify that a webhook really came from HubSpot before acting on it.

Identifier 'hubspot'
Import import { verifyHubSpot } from 'verihook/hubspot'
Headers x-hubspot-signature-v3
x-hubspot-request-timestamp
or x-hubspot-signature (v1/v2)
Signature v3: base64 HMAC-SHA256 of <method><url><body><timestamp>. v1/v2: hex SHA-256 of the client secret with the body (and method and URL).
Replay window 300 seconds (v3) (options.tolerance)
result.eventType the first event’s subscriptionType, e.g. contact.creation

The app’s Client secret: in your HubSpot developer account, open the app → Auth.

Store it in an environment variable (HUBSPOT_CLIENT_SECRET below) and never commit it.

import { verifyHubSpot } from 'verihook/hubspot';
export async function POST(request: Request) {
const result = await verifyHubSpot(request, process.env.HUBSPOT_CLIENT_SECRET!);
if (!result.valid) {
console.warn(result.code, result.reason, result.hint);
return new Response('Invalid signature', { status: 401 });
}
console.log(result.eventType, result.event);
return Response.json({ received: true });
}

request can be a Fetch Request or { headers, body, url? } with the raw body. verifyWebhook('hubspot', request, secret) from verihook does the same. Read why the raw body matters if verification fails behind a body parser.

app/api/webhooks/hubspot/route.ts
import { createWebhookHandler } from 'verihook/next';
export const POST = createWebhookHandler('hubspot', process.env.HUBSPOT_CLIENT_SECRET!, async (payload, result) => {
// Runs only for a valid signature
console.log(result.eventType, result.event);
});

Each adapter responds 401 to an invalid signature, 413 to a body over maxBodySize (2 MB by default) and 200 to a duplicate when you pass a dedupeStore.

  • v2 and v3 sign the public URL. Behind a proxy, forward x-forwarded-proto and x-forwarded-host, or pass { url }.

signWebhook from verihook/testing builds a correctly signed HubSpot request for your tests:

import { signWebhook } from 'verihook/testing';
import { verifyHubSpot } from 'verihook/hubspot';
const hook = await signWebhook('hubspot', {
secret: 'hubspot_client_secret',
payload: { id: 'evt_test' },
});
const result = await verifyHubSpot(
{ headers: hook.headers, body: hook.body, url: hook.url },
hook.secret,
);
// result.valid === true

From the command line, npx verihook simulate hubspot --url http://localhost:3000/webhooks/hubspot sends one to your local server.

See Testing your handlers for supertest and Fetch Request examples.

Failed results carry a code and often a hint with the likely cause. See Troubleshooting for each error code.

HubSpot’s webhook documentation