Class: Anthropic::Resources::Beta::Webhooks

Inherits:
Object
  • Object
show all
Defined in:
lib/anthropic/resources/beta/webhooks.rb,
sig/anthropic/resources/beta/webhooks.rbs

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Webhooks

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Webhooks.

Parameters:



43
44
45
# File 'lib/anthropic/resources/beta/webhooks.rb', line 43

def initialize(client:)
  @client = client
end

Instance Method Details

#parse_unverified(payload) ⇒ Anthropic::Models::Beta::BetaWebhookEvent

Parses a webhook payload into an event without verifying its signature. Prefer unwrap() unless you have already verified the signature yourself.

Parameters:

  • payload (String)

    The raw webhook payload as a string

Returns:



13
14
15
16
# File 'lib/anthropic/resources/beta/webhooks.rb', line 13

def parse_unverified(payload)
  parsed = JSON.parse(payload, symbolize_names: true)
  Anthropic::Internal::Type::Converter.coerce(Anthropic::Models::Beta::BetaWebhookEvent, parsed)
end

#unwrap(payload, headers:, key: @client.webhook_key) ⇒ Anthropic::Models::Beta::BetaWebhookEvent

Verifies the webhook signature from the webhook-id, webhook-timestamp and webhook-signature headers using your webhook signing key, then parses the payload into an event. Fails if the signature is missing or invalid.

Parameters:

  • payload (String)

    The raw webhook payload as a string

  • headers (Hash{String=>String})

    The raw HTTP headers that came with the payload

  • key (String, nil) (defaults to: @client.webhook_key)

    The webhook signing key

Returns:



29
30
31
32
33
34
35
36
37
38
# File 'lib/anthropic/resources/beta/webhooks.rb', line 29

def unwrap(payload, headers:, key: @client.webhook_key)
  if key.nil?
    raise ArgumentError.new("Cannot verify a webhook without a key on either the client's webhook_key or passed in as an argument")
  end

  ::StandardWebhooks::Webhook.new(key).verify(payload, headers)

  parsed = JSON.parse(payload, symbolize_names: true)
  Anthropic::Internal::Type::Converter.coerce(Anthropic::Models::Beta::BetaWebhookEvent, parsed)
end