Code samples

Receiving a webhook

When you are subscribing to one or more webhooks you need to specify the target url where you will be listening to the events that Betterez are sending. Creating a webhook endpoint on your server is no different from creating any other HTTP end point. Depending the technology you are using, you will have to create a route, a handler and the things you want to do with the event data.

Additionally, for your security, you might want to verify the webhook signature to confirm that Betterez is sending the event with no alterations.

For some web frameworks you will have to specify the url in its router file/module.

NodeJS/Express

const app = require('express')();

app.use(require('body-parser').raw({type: '*/*'}));

app.post('/my/webhook/url', function(req, res) {
  const event_json = JSON.parse(req.body);

  // do your stuff here with event_json

  res.send(200);
});

Elixir/Phoenix

defmodule MyWeb.WebhooksController do
  use MyWeb, :controller

  def post(conn, params) do
    
    # do your stuff here with params

    send_resp(conn, :no_content, "")
  end
end

Python/Django

import json
from django.http import HttpResponse

def webhooks_view(request):
  event_json = json.loads(request.body)

  # do your stuff here with event_json

  return HttpResponse(status=200)

Ruby/Sinatra

require 'json'

post '/my/webhook/url' do
    event_json = JSON.parse(request.body.read)

    # do your stuff here with event_json

    status 200
end

PHP

$input = @file_get_contents('php://input');
$event_json = json_decode($input);

// do your stuff here with $event_json

http_response_code(200);

.NET

using System;
using System.IO;
using Microsoft.AspNetCore.Mvc;

namespace workspace.Controllers {
    [Route("/my/webhook/url")]
    public class WebhookController : Controller {
        [HttpPost]
        public void Index() {
            var event_json = new StreamReader(HttpContext.Request.Body).ReadToEnd();

            // do your stuff here with event_json
        }
    }
}