Verify and Validate an Event Notification

Learn about the third step for setting up Square webhooks, which is to verify that you receive the event notification and validate that the notification originated from Square.

Link to section

Verify that you receive a 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.

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 subscription by following the steps in Subscribe to Event Notifications. For testing purposes, choose Select All under Events.
  3. Enter the unique URL you copied from webhook.site as your notification URL.
  4. Trigger an event from API Explorer. For example, generate a customer.created event by calling the CreateCustomer endpoint in the Customers API and providing a first or last name, company name, email address, or phone number.
  5. Return to the webhook.site page to view the event notification.

After you verify that your webhook subscription is working, you need to add code to your notification URL so that your application can process the event.

Link to section

Validate that the notification is from Square

Your notification URL is public and can be called by anyone, so you must validate each event notification to confirm that it originated from Square. A non-Square post can potentially compromise your application. Requests that fail validation cannot be trusted and should be discarded.

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:

  • The signature key for your webhook subscription.
  • The notification URL for your webhook subscription.
  • The raw body of the request.

To validate the webhook notification, generate the HMAC-SHA-256 signature in your own code and compare it to the x-square-hmacsha256-signature of the event notification you received. If needed, you can find your signature key and notification URL for your application's webhook subscription under Webhooks in the Developer Console.

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.

Link to section

Validation examples

The following examples show how to use the WebhooksHelper utility in the Square SDKs to generate an HMAC-SHA256 signature from a signature key, notification URL, and raw event notification body. The generated signature is then compared with the event notification's x-square-hmacsha256-signature sent in the test cURL request.

Important

When testing your webhook event notifications, make sure to use the raw request body without any whitespace. For example, {"hello":"world"}.

Link to section

Node.js

The following examples use the Node.js SDK. For installation instructions, see Square Node.js SDK Quickstart.

Copy
Expand
Link to section

Python

The following example uses the Python SDK. For installation instructions, see Square Python SDK Quickstart.

Copy
Expand
Link to section

C#

The following example uses the .NET SDK. For installation instructions, see Square .NET SDK Quickstart.

Copy
Expand
Link to section

Go

The following example uses the Go SDK. For installation instructions, see Square Go SDK Quickstart.

Copy
Expand
Link to section

Java

The following example uses the Java SDK. For instructions to run this example, see Setup steps for Java.

Copy
Expand

This exercise requires Oracle Java SE Development Kit (Java version 8 or later) and Apache Maven for dependency management.

  1. Create a directory for your project and create the pom.xml configuration file.

    Copy
    mkdir webhook-example && cd webhook-example && touch pom.xml
  2. Paste the following content into pom.xml, and then replace SDK_VERSION_HERE with the target SDK version. For example:

    <version>${CONNECT_JAVA_SDK_CURRENT_VERSION}</version>

    Copy
    Expand
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.square.examples</groupId> <artifactId>webhook-example</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.squareup</groupId> <artifactId>square</artifactId> <version>SDK_VERSION_HERE</version> </dependency> </dependencies> </project>
  3. Create the Server.java file in the package directory.

    Copy
    mkdir -p src/main/java/com/square/examples && touch src/main/java/com/square/examples/Server.java
  4. Paste the example server code into Server.java.

  5. Run the following commands from the /webhook-example directory:

    1. Download project dependencies.

      Copy
      mvn dependency:copy-dependencies
    2. Start the server.

      Copy
      javac -cp "target/dependency/*" src/main/java/com/square/examples/Server.java && java -cp "target/dependency/*:src/main/java" com.square.examples.Server
  6. After the server starts, send the test cURL command from a separate terminal.

    Copy
    curl -vX POST localhost:8000 -d '{"hello":"world"}' -H "X-Square-HmacSha256-Signature: 2kRE5qRU2tR+tBGlDwMEw2avJ7QM4ikPYD/PJ3bd9Og="
Link to section

PHP

The following examples use the PHP SDK. For installation instructions, see Square PHP SDK Quickstart.

Copy
Expand
Link to section

Ruby

The following example uses the Ruby SDK. For installation instructions, see Square Ruby SDK Quickstart.

Copy
Expand