Modules@robinpath/webhook
webhook

@robinpath/webhook

0.1.1Node.jsPublic

Send and verify webhooks with HMAC signatures for RobinPath

Webhook

Send webhooks with HMAC signatures, verify incoming webhook payloads, and prevent replay attacks

Package: @robinpath/webhook | Category: Web | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the webhook module when you need to:

  • Send a webhook POST request with optional HMAC signature -- Use webhook.send to perform this operation
  • Create an HMAC signature for a webhook payload -- Use webhook.sign to perform this operation
  • Verify a webhook HMAC signature using timing-safe comparison -- Use webhook.verify to perform this operation
  • Verify a webhook timestamp is within acceptable tolerance to prevent replay attacks -- Use webhook.verifyTimestamp to perform this operation
  • Parse a raw webhook body based on content type -- Use webhook.parsePayload to perform this operation

Quick Reference

FunctionDescriptionReturns
sendSend a webhook POST request with optional HMAC signature{status, statusText, ok, headers, body}
signCreate an HMAC signature for a webhook payloadSignature in format 'algorithm=hex'
verifyVerify a webhook HMAC signature using timing-safe comparisonTrue if signature is valid
verifyTimestampVerify a webhook timestamp is within acceptable tolerance to prevent replay attacks{valid, ageMs, toleranceMs}
parsePayloadParse a raw webhook body based on content typeParsed payload
buildPayloadBuild a standardized webhook payload with event name, data, timestamp, and ID{event, data, timestamp, id}
headersBuild webhook headers including signature and timestampHeaders object ready for HTTP request
isValidUrlCheck if a string is a valid HTTP/HTTPS webhook URLTrue if valid HTTP(S) URL

Functions

send

Send a webhook POST request with optional HMAC signature

Module: webhook | Returns: object -- {status, statusText, ok, headers, body}

webhook.send "https://example.com/hook" $data {"secret": "whsec_abc"}
ParameterTypeRequiredDescription
urlstringYesTarget webhook URL
payloadanyYesData to send
optionsobjectNo{secret, method, contentType, headers, algorithm, signatureHeader, event}

sign

Create an HMAC signature for a webhook payload

Module: webhook | Returns: string -- Signature in format 'algorithm=hex'

webhook.sign $payload "whsec_abc"
ParameterTypeRequiredDescription
payloadanyYesThe payload to sign (string or object)
secretstringYesThe webhook secret
algorithmstringNoHash algorithm (default: sha256)

verify

Verify a webhook HMAC signature using timing-safe comparison

Module: webhook | Returns: boolean -- True if signature is valid

webhook.verify $body "whsec_abc" $signatureHeader
ParameterTypeRequiredDescription
payloadanyYesThe received payload
secretstringYesThe webhook secret
signaturestringYesThe received signature
algorithmstringNoHash algorithm (default: sha256)

verifyTimestamp

Verify a webhook timestamp is within acceptable tolerance to prevent replay attacks

Module: webhook | Returns: object -- {valid, ageMs, toleranceMs}

webhook.verifyTimestamp $timestamp 60000
ParameterTypeRequiredDescription
timestampnumberYesTimestamp in milliseconds
toleranceMsnumberNoTolerance in ms (default 300000 = 5min)

parsePayload

Parse a raw webhook body based on content type

Module: webhook | Returns: any -- Parsed payload

webhook.parsePayload $rawBody "application/json"
ParameterTypeRequiredDescription
bodyanyYesRaw request body
contentTypestringNoContent-Type header (default: application/json)

buildPayload

Build a standardized webhook payload with event name, data, timestamp, and ID

Module: webhook | Returns: object -- {event, data, timestamp, id}

webhook.buildPayload "order.created" $orderData
ParameterTypeRequiredDescription
eventstringYesEvent type name
dataanyYesEvent data
optionsobjectNo{id, metadata}

headers

Build webhook headers including signature and timestamp

Module: webhook | Returns: object -- Headers object ready for HTTP request

webhook.headers "whsec_abc" $payload "order.created"
ParameterTypeRequiredDescription
secretstringYesWebhook secret for signing
payloadanyYesThe payload being sent
eventstringNoEvent type header (optional)

isValidUrl

Check if a string is a valid HTTP/HTTPS webhook URL

Module: webhook | Returns: boolean -- True if valid HTTP(S) URL

webhook.isValidUrl "https://example.com/hook"
ParameterTypeRequiredDescription
urlstringYesURL to validate

Error Handling

All functions throw on failure. Common errors:

ErrorCause
URL is requiredCheck the error message for details
@desc "Send and validate result"
do
  set $result as webhook.send "https://example.com/hook" $data {"secret": "whsec_abc"}
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Create a new item with send

Create a new resource and capture the result.

set $result as webhook.send "https://example.com/hook" $data {"secret": "whsec_abc"}
print "Created: " + $result

2. Multi-step Webhook workflow

Chain multiple webhook operations together.

@desc "Send, sign, and more"
do
  set $r_send as webhook.send "https://example.com/hook" $data {"secret": "whsec_abc"}
  set $r_sign as webhook.sign $payload "whsec_abc"
  set $r_verify as webhook.verify $body "whsec_abc" $signatureHeader
  print "All operations complete"
enddo

3. Safe send with validation

Check results before proceeding.

@desc "Send and validate result"
do
  set $result as webhook.send "https://example.com/hook" $data {"secret": "whsec_abc"}
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/webhook

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.4 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

web