The raw request body as a string
The signature from the x-square-hmacsha256-signature header
Your webhook signature key from Square, or a list of keys while you rotate it (any match is valid)
OptionalnotificationUrl: string
The URL where the webhook was sent (optional, for additional verification)
Verification result with valid flag and optional error
import { verifySignature } from '@bates-solutions/squareup/server';
const result = verifySignature(
rawBody,
req.headers['x-square-hmacsha256-signature'],
process.env.SQUARE_WEBHOOK_KEY!
);
if (!result.valid) {
return res.status(401).json({ error: result.error });
}
// While rotating the key, accept the new and the old one
verifySignature(rawBody, signature, [process.env.SQUARE_WEBHOOK_KEY!, process.env.SQUARE_WEBHOOK_KEY_OLD!]);
Verify a Square webhook signature using HMAC-SHA256