Link Search Menu Expand Document

Build a webhook endpoint

Write the code that properly handles webhook notifications.

The first step to begin receiving event notifications with webhooks is to build a webhook endpoint on your server.

What is 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 one: Identify the events to monitor

Identify the type of events and the event objects your webhook endpoint needs to parse.

Step two: Create a webhook endpoint

Set up an HTTP endpoint on your local machine that can accept unauthenticated webhook requests with a POST method. You can use ngrok to expose your local server to the public internet when testing.

Step three: Handle requests from Tilled

Your endpoint must be configured to read event objects for the type of event notifications you want to receive. Tilled 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 a type, id, account_id, and related Tilled resource nested under data. Your endpoint must check the event type and parse the payload of each event.

{
  "id": "evt_qLX9Fqyspi8bk0j06yc7s",
  "account_id": "acct_QvlHDyOkQ44HFHsZGs0Gi",
  "type": "payment_intent.succeeded",
  "data": {
    "id": "pi_Hf068QvxJax26OBIKgmw9",
    "status": "succeeded",
    ...
  }
}

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 customer’s order as paid in your accounting system.

Built-in retries

Tilled webhooks have built-in retry methods for 3xx, 4xx, or 5xx response status codes (multiple attempts over several hours). If Tilled doesn’t quickly receive a 2xx response status code for an event, we mark the event as failed. You can query for missed events to reconcile the data over any time period.

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

Step five: Get started

Sample code (Node)

// This example uses Express to receive webhooks
const express = require('express');
const app = express();

app.post('/webhook', express.json({type: 'application/x-www-form-urlencoded'}), (request, response) => {
  const event = request.body;
  // Handle the event
  switch (event.type) {
    case 'payment_intent.succeeded':
      const paymentIntent = event.data;
      // Then define and call a method to handle the successful payment intent.
      // handlePaymentIntentSucceeded(paymentIntent);
      break;
    case 'payment_method.attached':
      const paymentMethod = event.data;
      // Then define and call a method to handle the successful attachment of a PaymentMethod.
      // handlePaymentMethodAttached(paymentMethod);
      break;
    // ... handle other event types
    default:
      console.log(`Unhandled event type ${event.type}`);
  }

  // Return a response to acknowledge receipt of the event
  response.json({received: true});
});

app.listen(8000, () => console.log('Running on port 8000'));

Copyright © 2022 Tilled