Webhooks

Did you know we'll do all this for you?

Use webhooks to be notified about events that happen in your LessChurn account. A webhook can be sent for each detour taken by one of your customers. You may want to receive a webhook for each detour or may prefer to ignore certain detours,
but you will certainly want to be notified when a customer deletes their account.

Configuring a webhook

Receiving a webhook

Configuring your server to receive a new webhook is no different from creating any page on your website. With PHP, you might create a new .php file on your server; with a framework like Sinatra, you would add a new route with the desired URL. Remember, with webhooks, your server is the server receiving the request.

Webhook data is sent as JSON in the request's body. The full detour details are included and can be used directly. You can always resend a webhook from the webhooks page.

Webhooks are called synchronously when your customer takes a detour. That means that your customers are waiting for your web server to finish responding to the webhook before we redirect them back to your site. If your detour is set to send a webhook, but your server is down, your customer will be waiting until the webhook request times out. We do this to ensure that when your customer is returned to your site, the state in Stripe or BrainTree matches the state in your app. If you don't like this you should set your webhook responses to return a response immediately, before handling the request.

Webhook content

You can view the content for the webhook for each Detour on the Churn Form page. They're all going to look something like this:

Receiving webhooks with a CSRF-protected server

If you're using Rails, Django, or another web framework, your server may automatically check that POST request bodies it receives contain a CSRF token. This is an important security feature that helps protect you and your users from cross-site request forgery. However, it may also prevent your server from receiving legitimate webhooks. You may need to exempt from CSRF protection the Rails route or Django view you use to receive webhooks.
Rails:

import json
# Webhooks are always sent as HTTP POST requests, so we want to ensure
# that only POST requests will reach your webhook view. We can do that by
# decorating `webhook()` with `require_POST`.
#
# Then to ensure that the webhook view can receive webhooks, we need
# also need to decorate `webhook()` with `csrf_exempt`.
@require_POST
@csrf_exempt
def webhook(request):
# Process webhook data in `request.body`

Responding to a webhook

Unlike Stripe, LessChurn does not check the response your server sends, nor does it resend webhooks automatically until a proper response is received (but you can resend them from the Webhooks page). LessChurn stores the response status and body to help debugging, but other than that the response is ignored.

Viewing webhooks

LessChurn stores every webhook sent and the response from your web server. You can view them on the Webhooks page. If you click on the data column you can drill into that webhook and see the details for each time the webhook was sent.

Resending a webhook

You can resend any webhook by clicking on "Resend" when you're viewing a webhook on the Webhooks page.