Webhook Integration

Configure asynchronous webhooks in moclojer to trigger background requests to external APIs. Simulate real-world integrations with conditional triggers, delays, and template support.

Webhooks allow moclojer to send asynchronous background requests to external APIs when an endpoint receives a request. This enables realistic simulation of real-world integrations like notifications, logging, and third-party callbacks.

🎯 How Webhooks Work

When an endpoint with a webhook configuration receives a request:

  1. moclojer responds immediately to the client with the configured response

  2. In parallel, moclojer sends an asynchronous HTTP request to the webhook URL

  3. The webhook executes independently without blocking the original response

sequenceDiagram
    participant Client
    participant moclojer
    participant Webhook API

    Client->>moclojer: POST /with-webhook
    moclojer->>Client: 200 OK (immediate response)
    Note over moclojer: Response sent!

    par Async Webhook
        moclojer-->>Webhook API: POST (background request)
        Note over Webhook API: Processes webhook
        Webhook API-->>moclojer: Response (ignored)
    end

πŸ“ Basic Configuration

What happens:

  1. Client sends POST to /orders

  2. moclojer responds with 201 Created immediately

  3. moclojer sends POST to https://api.example.com/notifications in the background

πŸ”§ Webhook Configuration Options

Required Fields

Field
Description
Example

url

Target webhook endpoint URL

https://api.slack.com/webhooks/...

method

HTTP method

POST, PUT, PATCH

Optional Fields

Field
Default
Description

sleep-time

60

Delay in seconds before sending webhook

if

true

Conditional expression to trigger webhook

body

(empty)

Request body (supports templates)

headers

{}

Custom headers

⏱️ Delayed Webhooks

Simulate processing delays with sleep-time:

Use case: Simulate payment processing that takes time to complete.

πŸŽ›οΈ Conditional Webhooks

Use if conditions to trigger webhooks based on request data:

Supported Operators

Operator
Description
Example

=

Equals

json-params.status = "active"

>

Greater than

json-params.amount > 100

<

Less than

json-params.age < 18

>=

Greater or equal

json-params.score >= 50

<=

Less or equal

query-params.limit <= 100

Accessing Request Data in Conditions

🌐 Real-World Examples

Example 1: Slack Notification

Example 2: Analytics Tracking

Example 3: Multi-Service Integration

πŸ§ͺ Testing Webhooks

Using Request Bin

  1. Create a request bin at webhook.site

  2. Copy the unique URL

  3. Configure your webhook:

  1. Make a request to your endpoint

  2. View the webhook payload at webhook.site

Local Testing with ngrok

βœ… Best Practices

Do:

  • βœ… Use sleep-time to simulate realistic processing delays

  • βœ… Add conditions with if to trigger webhooks selectively

  • βœ… Include correlation IDs for tracing (use {{json-params.correlation_id}})

  • βœ… Test webhooks with request bins before production integration

  • βœ… Use template variables to pass dynamic data

Don't:

  • ❌ Rely on webhook responses (they're ignored by design)

  • ❌ Use webhooks for critical synchronous operations

  • ❌ Set sleep-time too high in tests (slows down test suites)

  • ❌ Expose sensitive credentials in webhook URLs

πŸ”’ Security Considerations

πŸ“Š Use Cases

Scenario
Configuration

Order confirmation email

sleep-time: 0, immediate notification

Payment processing

sleep-time: 60-300, simulate processing time

Conditional alerts

if: json-params.priority = "high"

Multi-step workflows

Chain webhooks with delays

Third-party integrations

Slack, Discord, analytics platforms

🚨 Important Notes

Asynchronous Execution: moclojer does not wait for webhook responses. The webhook executes in the background and its response is ignored.

No Retries: Failed webhooks are not automatically retried. For production use, implement retry logic in your actual backend.

Template Support: Webhooks support all template variables: path-params.*, query-params.*, json-params.*, header-params.*, and {{now}}.

πŸ“š See Also

Last updated

Was this helpful?