0
0
Rest APIprogramming~5 mins

Webhook payload design in Rest API

Choose your learning style9 modes available
Introduction

A webhook payload is the information sent from one app to another when something happens. Designing it well helps apps understand and use the data easily.

When you want to notify another app instantly about an event, like a new user signup.
When you need to send data updates from your app to a partner app automatically.
When you want to trigger actions in another system based on your app's events.
When building integrations that rely on real-time data exchange between services.
Syntax
Rest API
{
  "event": "string",
  "timestamp": "ISO 8601 string",
  "data": {
    "key1": "value1",
    "key2": "value2",
    "...": "..."
  }
}

The payload is usually a JSON object.

Include an event name, a timestamp, and a data section with details.

Examples
This payload tells the receiver a new user was created with their ID and email.
Rest API
{
  "event": "user.created",
  "timestamp": "2024-06-01T12:00:00Z",
  "data": {
    "user_id": "12345",
    "email": "user@example.com"
  }
}
This payload notifies that an order was paid, including order details.
Rest API
{
  "event": "order.paid",
  "timestamp": "2024-06-01T15:30:00Z",
  "data": {
    "order_id": "98765",
    "amount": 49.99,
    "currency": "USD"
  }
}
Sample Program

This Python program creates a webhook payload with the event name, current time, and data, then prints it as formatted JSON.

Rest API
import json

def create_webhook_payload(event, data):
    from datetime import datetime, timezone
    payload = {
        "event": event,
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "data": data
    }
    return json.dumps(payload, indent=2)

# Example usage
payload_json = create_webhook_payload("user.created", {"user_id": "12345", "email": "user@example.com"})
print(payload_json)
OutputSuccess
Important Notes

Always use a clear event name to describe what happened.

Use ISO 8601 format for timestamps to avoid confusion across time zones.

Keep the data section simple and only include necessary information.

Summary

Webhook payloads are JSON objects that tell another app about an event.

Include event name, timestamp, and data for clarity and usefulness.

Design payloads to be easy to read and parse by the receiving app.