Framework.Effects.Webhook (Framework v0.5.0)

View Source

Webhook sending and template rendering for Framework effects.

Provides webhook sending capabilities with URL templating, payload templating, and custom header support. Maintains backward compatibility with simple URL configuration.

Configuration

Simple Configuration (Backward Compatible)

config :framework, :webhook_endpoints, %{
  "user.created" => "https://api.example.com/webhooks/user_created"
}

Enhanced Configuration (with Templating)

config :framework, :webhook_config, %{
  # Template directory for webhook payloads
  templates_dir: "priv/webhook_templates",

  # Enhanced endpoint configuration
  endpoints: %{
    "user.created" => %{
      url: "https://api.example.com/tenants/{{tenant_id}}/webhooks/user_created",
      headers: %{
        "X-Tenant-ID" => "{{tenant_id}}",
        "Authorization" => "Bearer {{api_token}}"
      },
      template: "user_created"  # Optional: use JSON template file
    },
    "billing.charge" => %{
      url: "https://{{tenant_slug}}.billing.com/webhooks/charge",
      headers: %{"X-API-Key" => "{{billing_api_key}}"},
      template: "billing_charge"
    }
  }
}

Template System

Webhook payload templates are JSON files with variable interpolation:

  • Template files: templates_dir/<template_name>.json
  • Variables in templates use {{variable_name}} syntax
  • All JSON values can be templated (strings, nested objects, arrays)

URL and Header Templating

Both URLs and custom headers support {{variable_name}} substitution:

  • URL: "https://api.{{environment}}.com/webhooks/{{event_type}}"
  • Headers: "X-Tenant-ID": "{{tenant_id}}"

Summary

Functions

Get webhook configuration for a given webhook name.

Render a webhook payload template with variable substitution.

Render webhook headers with variable substitution.

Render webhook payload using templates or direct payload.

Render a webhook URL with variable substitution.

Send a webhook using the framework's webhook configuration.

Functions

get_webhook_config(webhook_name)

Get webhook configuration for a given webhook name.

Supports both simple string URLs (backward compatibility) and enhanced template configs.

render_payload_template(template_name, model, templates_dir, locale \\ "en-US")

Render a webhook payload template with variable substitution.

Locale-aware Template Resolution

Templates are searched in order:

  1. Locale-specific: templates_dir/{locale}/{template_name}.json
  2. Default: templates_dir/{template_name}.json

Example:

  • Locale "es-ES": priv/webhook_templates/es-ES/user_created.json
  • Fallback: priv/webhook_templates/user_created.json

render_webhook_headers(headers, model)

Render webhook headers with variable substitution.

render_webhook_payload(arg1, payload, model)

Render webhook payload using templates or direct payload.

Supports locale-aware template resolution if locale is provided in model.

render_webhook_url(url_template, model)

Render a webhook URL with variable substitution.

send_webhook(webhook_name, payload, model, opts \\ [])

Send a webhook using the framework's webhook configuration.

Parameters

  • webhook_name: Webhook identifier (e.g., "user.created", "billing.charge")
  • payload: Base payload data (merged with template if specified)
  • model: Variables for template rendering (URLs, headers, payload templates)
  • opts: Additional options
    • :external_id - Idempotency key for logging
    • :correlation_ids - Request correlation context

Returns

  • :ok on successful send
  • {:error, reason} on failure