CaptainHook

GitHub Workflow Status GitHub issues License Hex.pm Hex.pm

Ordered signed webhook notifications. Support multiple endpoints for each webhook.

Installation

CaptainHook is published on Hex.

The package can be installed by adding captain_hook to your list of dependencies in mix.exs:

def deps do
  [
    {:captain_hook, "~> 1.8.0"}
  ]
end

After the packages are installed you must create a database migration for each versionto add the captain_hook tables to your database:

defmodule CaptainHook.TestRepo.Migrations.CreateCaptainHookTables do
  use Ecto.Migration

  def up do
    CaptainHook.Migrations.V1.up()
    CaptainHook.Migrations.V2.up()
    CaptainHook.Migrations.V3.up()
    CaptainHook.Migrations.V4.up()

    CaptainHook.Migrations.V2Data.up()
  end

  def down do
    CaptainHook.Migrations.V1.down()
    CaptainHook.Migrations.V2.down()
    CaptainHook.Migrations.V3.down()
    CaptainHook.Migrations.V4.down()
  end
end

This will run all of CaptainHook's versioned migrations for your database. Migrations between versions are idempotent and will never change after a release. As new versions are released you may need to run additional migrations.

Now, run the migration to create the table:

mix ecto.migrate

Usage

webhook_endpoint = CaptainHook.create_webhook_endpoint(%{
  webhook: "my_webhook_name", 
  url: "https://webhook.site/538bb308-4dd8-4008-a19b-4e4a5758ef29",
  livemode: true,
  enabled_notification_types: [%{name: "*"}]
})

# Get the webhook_endpoint secret in order to verify the webhook signature
%CaptainHook.WebhookEndpoint{secret: secret} = 
  CaptainHook.get_webhook_endpoint(webhook_endpoint.id, includes: [:secret])

# Notify - it will enqueue the notification and send it in its turn
{:ok, CaptainHook.WebhookNotification{} = webhook_notification} = 
  CaptainHook.notify(
    "my_webhook_name", 
    true, 
    "notification_type", 
    %{"my" => "data", "to" => "report"}
  )

CaptainHook client

Want to verify the authenticity of a captain_hook request ? you can use the CaptainHookClient.

The docs can be found at https://hexdocs.pm/captain_hook.