Skip to content

annatel/captain_hook

Repository files navigation

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, "~> 3.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
    Queuetopia.Migrations.up()
    Padlock.Mutexes.Migrations.V1.up()
    CaptainHook.Migrations.up()
  end

  def down do
    Queuetopia.Migrations.down()
    Padlock.Mutexes.Migrations.V1.down()
    CaptainHook.Migrations.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_patterns: [%{pattern: "*"}]
})

# 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_ref",
    %{"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.