Verify and Validate an Event Notification

You can verify the creation and receipt of an event notification using either a test endpoint you create or a public site such as webhook.site. You can use API Explorer to generate events that webhooks can subscribe to. For more information about using API Explorer, see API Explorer Overview.

Link to section

Overview

To verify your event notification subscription using webhook.site:

  1. Go to webhook.site in a browser, copy the provided unique URL to the clipboard, and leave the page open.
  2. Create a webhook using Subscribe to Event Notifications.
  3. Use the unique URL you copied from webhook.site for your notification URL, and then choose Select All under Events.
  4. Create an event in API Explorer. Creating a customer using the Customers API is a simple way to create an event because it requires only one of the five identity values, such as family name (last name), a given name (first name), or an email address. This generates the customer.created event.
  5. Return to the webhook.site page to view the event notification.

After you verify that your event notification subscription is working, you need to add code to your notification URL so that your application can process the event information. Because your notification URL is public and can be called by anyone, you must validate each event notification to confirm that it came from Square. A non-Square post can potentially compromise your application. All webhook notifications from Square include an x-square-hmacsha256-signature header. The value of this header is an HMAC-SHA-256 signature generated using your webhook signature key, the notification URL, and the raw body of the request. You can validate the webhook notification by generating the HMAC-SHA-256 value in your own code and comparing it to the signature of the event notification you received.

Important

A malicious agent can compromise your notification endpoint by using a timing analysis attack to determine the key you're using to decrypt and compare webhook signatures. You should use a constant-time crypto library to prevent such attacks by masking the actual time taken to decrypt and compare signatures.

The following functions generates an HMAC-SHA256 signature from your signature key, the notification URL, and the event notification body. You can then compare the result with the event notification's x-square-hmacsha256-signature.

Important

When testing your webhook event notifications, make sure that you're using the raw request body without any whitespace. For example, {"hello":"world”}.

Link to section

Webhook event notification validation - Node.js

Link to section

Webhook event notification validation - Python

Link to section

Webhook event notification validation - C#

Link to section

Webhook event notification validation - Go

Link to section

Webhook event notification validation - Java

Link to section

Webhook event notification validation - PHP

Link to section

Webhook event notification validation - Ruby

On this page