# Webhooks

Tillhub uses webhooks to notify your application when an event happens in your account. Webhooks are particularly useful for asynchronous events like when a new transaction has been created or a product has been updated.

# How Tillhub uses webhooks

A webhook enables Tillhub to push real-time notifications to your app. Tillhub uses HTTPS to send these notifications to your app as a JSON payload via POST method. You can then use these notifications to execute actions in your backend systems.

# Steps to receive webhooks

You can start receiving event notifications in your app using the steps in this section:

  1. Identify the events you want to monitor and the event payloads to parse.
  2. Create a webhook endpoint as an HTTP endpoint (URL) on your local server.
  3. Handle requests from Tillhub by parsing each event object and returning 2xx response status codes.
  4. Deploy your webhook endpoint so it’s a publicly accessible HTTPS URL.
  5. Register your publicly accessible HTTPS URL in the Tillhub dashboard.

# How to create a webhook endpoint

Creating a webhook endpoint is no different from creating any other page on your website. It’s an HTTP or HTTPS endpoint on your server with a URL. If you’re still developing your endpoint on your local machine, it can be HTTP. After it’s publicly accessible, it must be HTTPS. You can use one endpoint to handle several different event types at once, or set up individual endpoints for specific events.

# Step 1: Identify the events to monitor

Use the Tillhub API reference guide to identify the Tillhub events and their event objects your webhook endpoint needs to parse.

# Step 2: Create a webhook endpoint

Set up an HTTP endpoint on your local machine that can accept unauthenticated webhook requests with a POST method. For example, this route in Express is a map to a Nodejs webhook function:

In this example, the /tillhub_webhooks route is configured to accept only POST requests and expects data to be delivered in a JSON payload.

# Step 3: Handle requests from Tillhub

Your endpoint must be configured to read event objects for the type of event notifications you want to receive. Tillhub sends events to your webhook endpoint as part of a POST request with a JSON payload.

# Check event objects

Each event is structured as an event object with following contract:

interface IWebhookRequest<T> {
  // Webhook UUID
  webhookId: UUID,
  // Received event UUID
  eventId: UUID,
  // Received event entity type (e.g. transaction, product, etc.)
  eventEntity: string,
  // Received event type (e.g. create, update, delete, etc.)
  eventType: string,
  // Received event version
  eventVersion: number,
  // Received event entity UUID
  entityInstanceId: UUID,
  // Date and time event was received
  timestamp: string,
  // Event payload
  payload: T
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Example:

{
  "webhookId": "ff30cfdf-5167-4f72-b4fc-c1d72db3c07e",
  "eventId": "c6fe918a-7bdc-4ccd-af36-95c03853233d",
  "eventEntity": "transaction",
  "eventType": "create",
  "eventVersion": 0,
  "entityInstanceId": "10b06f38-031d-4e0c-8706-0474c8c5b61c",
  "timestamp": "2022-12-13T08:45:00.881Z",
  "payload": {...}
1
2
3
4
5
6
7
8
9

Your endpoint must check the event type and parse the payload of each event.

# Return a 2xx response

Your endpoint must quickly return a successful status code (2xx) prior to any complex logic that could cause a timeout. For example, you must return a 200 response before updating a product in your system.

express.post('/tillhub_webhooks', (request, response) => {
  const payload = request.body.payload
  try {
    verifyPayloadSignature(payload)
  } catch (err) {
    return response.status(400).send('Invalid Signature')
  }

  try {
    verifyPayload(payload)
  } catch (err) {
    return response.status(400).send('Invalid Payload')
  }

  response.send('Successful')

  handleEvent(payload)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

In the above example, the nodejs function checks that the event type was received, and returns a 200 response.

# Built-in retries

Tillhub webhooks have built-in retry methods for 3xx, 4xx, or 5xx response status codes. If Tillhub doesn’t quickly receive a 2xx response status code for an event, we mark the event as failed. we retry sending the event with an exponential backoff retry delay and after multiple failed retry, we stop trying to send it to your endpoint.

Use webhook signatures to verify that Tillhub generated a webhook request and that it didn’t come from a server acting like Tillhub.

# Step 5: Register your webhook on Tillhub Dashboard

Webhooks Overview can be accessed in Tillhub Dashboard (opens new window) from Settings -> Webhooks. On this page you can view, create, edit, and expand your webhooks details.

Webhooks Overview

# Creating a new webhook

By clicking on the new button, you will be redirected to the following page. After filling the required information and clicking on the save button, we will start sending the events to your desired endpoint.

Create Webhook

# Viewing and modifying your webhook

After clicking on the webhook you will be redirected to the webhook details page. on the top part of this page, you can modify the webhook details. On the bottom part, you can view the events that belong to this webhook.

Webhook Details

On the right side of each event, there's an indicator for result of each event. The Green bar Indicates that the event was successfully processed while the Red bar Indicates that sending event was not successful.

You can retry a single event by clicking on the Resend button on the right side. Also, you can choose multiple events to retry or setting up a filter to Resend all the events.

When clicking on a detail, you can see the payload that was sent to the client and the responses received from the client. The Blue bar is for the payload that was sent to the client, and after that are the responses from the client that can also be Red bar or Green bar . By clicking on them, on the right we can see the JSON object that was sent or the JSON object that was received from the responses.

Example of payload with the Blue bar Webhook Requests Details

Example of error in response with the Red bar Webhook Requests Details

Example of success in response with the Green bar Webhook Requests Details