0
0
Rest APIprogramming~30 mins

Webhook payload design in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Webhook Payload Design
📖 Scenario: You are building a simple webhook system for an online store. When a new order is placed, the system sends a notification with order details to a partner service. You need to design the webhook payload that contains the order information.
🎯 Goal: Create a webhook payload as a JSON object that includes order details such as order ID, customer name, list of items with quantity and price, and total amount.
📋 What You'll Learn
Create a dictionary called order with exact keys and values for order details
Add a variable called currency with the value "USD"
Use a dictionary comprehension to create a new dictionary items_summary with item names as keys and total price (quantity * price) as values
Print the final webhook payload as a JSON string
💡 Why This Matters
🌍 Real World
Webhook payloads are used to send real-time notifications between web services, such as notifying a shipping partner when a new order is placed.
💼 Career
Understanding how to design and format webhook payloads is important for backend developers and API engineers working with integrations and event-driven systems.
Progress0 / 4 steps
1
Create the order data structure
Create a dictionary called order with these exact entries: "order_id": 12345, "customer": "John Doe", and "items" which is a list of dictionaries with these exact items: {"name": "T-shirt", "quantity": 2, "price": 15.0} and {"name": "Jeans", "quantity": 1, "price": 40.0}.
Rest API
Need a hint?

Use a dictionary with keys order_id, customer, and items. The items key holds a list of dictionaries for each product.

2
Add currency configuration
Create a variable called currency and set it to the string "USD".
Rest API
Need a hint?

Just create a simple variable named currency and assign the string "USD" to it.

3
Create items summary with dictionary comprehension
Use a dictionary comprehension to create a new dictionary called items_summary where each key is the item name from order["items"] and the value is the total price calculated as quantity * price.
Rest API
Need a hint?

Use a dictionary comprehension with for item in order["items"]. The key is item["name"] and the value is item["quantity"] * item["price"].

4
Print the final webhook payload
Print the final webhook payload as a JSON string. Create a dictionary called webhook_payload with keys: order_id, customer, currency, and items_summary. Then print it using print() and json.dumps() with indentation of 2 spaces.
Rest API
Need a hint?

Import the json module. Create webhook_payload dictionary with the required keys and values. Use print(json.dumps(webhook_payload, indent=2)) to display the JSON string nicely.