Bird
Raised Fist0
Rest APIprogramming~20 mins

Webhook payload design in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Webhook Payload Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this webhook payload validation code?

Consider a webhook payload validation function that checks if the payload contains the required fields event and timestamp. What will be the output of the following code when given the payload {"event": "user_signup", "timestamp": 1680000000}?

Rest API
def validate_payload(payload):
    required_fields = ['event', 'timestamp']
    for field in required_fields:
        if field not in payload:
            return f"Missing field: {field}"
    return "Payload valid"

payload = {"event": "user_signup", "timestamp": 1680000000}
print(validate_payload(payload))
APayload valid
BMissing field: timestamp
CMissing field: event
DKeyError
Attempts:
2 left
💡 Hint

Check if all required keys exist in the payload dictionary.

🧠 Conceptual
intermediate
1:30remaining
Which option best describes the purpose of including a signature in a webhook payload?

Why do webhook payloads often include a signature or hash value?

ATo encrypt the payload data for privacy
BTo compress the payload for faster transmission
CTo verify the payload integrity and authenticity
DTo format the payload as JSON
Attempts:
2 left
💡 Hint

Think about how the receiver can trust the source of the webhook.

🔧 Debug
advanced
2:00remaining
What error does this webhook payload parsing code raise?

Given the following Python code that parses a webhook payload, what error will it raise?

Rest API
import json

def parse_payload(payload_str):
    data = json.loads(payload_str)
    return data['user']['id']

payload = '{"user": null}'
print(parse_payload(payload))
AKeyError
BTypeError
CValueError
DAttributeError
Attempts:
2 left
💡 Hint

Consider what happens when you try to access a key on a NoneType object.

📝 Syntax
advanced
1:30remaining
Which option produces the correct JSON payload with nested data?

Which of the following strings is a valid JSON payload representing a webhook event with nested user data?

A{"event": "order_created", "user": {"id": 123, "name": Alice}}
B{'event': 'order_created', 'user': {'id': 123, 'name': 'Alice'}}
C{"event": "order_created", "user": {id: 123, name: "Alice"}}
D{"event": "order_created", "user": {"id": 123, "name": "Alice"}}
Attempts:
2 left
💡 Hint

Remember JSON requires double quotes for keys and string values.

🚀 Application
expert
1:30remaining
What is the number of top-level keys in this webhook payload?

Given the following webhook payload, how many top-level keys does it contain?

Rest API
{
  "event": "payment_received",
  "timestamp": 1680001234,
  "data": {
    "payment_id": "abc123",
    "amount": 49.99,
    "currency": "USD"
  },
  "metadata": {
    "customer_id": "cust789",
    "notes": "First payment"
  }
}
A4
B3
C2
D5
Attempts:
2 left
💡 Hint

Count only the keys at the outermost level of the JSON object.

Practice

(1/5)
1. What is the main purpose of a webhook payload in a REST API?
easy
A. To store user login credentials securely
B. To create a user interface for the API
C. To send event details from one app to another automatically
D. To encrypt data between client and server

Solution

  1. Step 1: Understand webhook payload role

    A webhook payload is a message sent automatically when an event happens, sharing event details.
  2. Step 2: Compare options with webhook purpose

    Only To send event details from one app to another automatically describes sending event details automatically, which matches webhook payload use.
  3. Final Answer:

    To send event details from one app to another automatically -> Option C
  4. Quick Check:

    Webhook payload = event details sent automatically [OK]
Hint: Webhook payloads always send event info automatically [OK]
Common Mistakes:
  • Confusing payload with user authentication
  • Thinking payload creates UI
  • Assuming payload encrypts data
2. Which of the following is the correct JSON structure for a webhook payload that includes event name and timestamp?
easy
A. [ "event": "user_signup", "timestamp": "2024-06-01T12:00:00Z" ]
B. { "event": "user_signup", "timestamp": "2024-06-01T12:00:00Z" }
C. { event = "user_signup", timestamp = "2024-06-01T12:00:00Z" }
D. "event": "user_signup", "timestamp": "2024-06-01T12:00:00Z"

Solution

  1. Step 1: Identify valid JSON syntax

    JSON objects use curly braces {}, keys and values separated by colon :, keys in double quotes, and comma between pairs.
  2. Step 2: Check each option's syntax

    { "event": "user_signup", "timestamp": "2024-06-01T12:00:00Z" } uses correct JSON object syntax. [ "event": "user_signup", "timestamp": "2024-06-01T12:00:00Z" ] uses square brackets which denote arrays, not key-value pairs. { event = "user_signup", timestamp = "2024-06-01T12:00:00Z" } uses equals sign instead of colon. "event": "user_signup", "timestamp": "2024-06-01T12:00:00Z" is missing braces.
  3. Final Answer:

    { "event": "user_signup", "timestamp": "2024-06-01T12:00:00Z" } -> Option B
  4. Quick Check:

    Valid JSON object = { "event": "user_signup", "timestamp": "2024-06-01T12:00:00Z" } [OK]
Hint: JSON objects use braces and colons, not brackets or equals [OK]
Common Mistakes:
  • Using square brackets instead of braces for objects
  • Using equals sign instead of colon
  • Omitting braces around JSON
3. Given this webhook payload JSON:
{"event": "order_created", "timestamp": "2024-06-01T15:30:00Z", "data": {"order_id": 123, "amount": 49.99}}

What value will the receiving app find for data.amount?
medium
A. 49.99
B. order_created
C. "amount"
D. 123

Solution

  1. Step 1: Locate the data.amount field in JSON

    Inside the "data" object, the key "amount" has the value 49.99 (a number).
  2. Step 2: Match the value to options

    49.99 matches the numeric value 49.99. Other options are either keys or unrelated values.
  3. Final Answer:

    49.99 -> Option A
  4. Quick Check:

    data.amount = 49.99 [OK]
Hint: Look inside nested 'data' object for 'amount' key [OK]
Common Mistakes:
  • Confusing order_id with amount
  • Selecting string key instead of value
  • Choosing event name instead of data field
4. You receive this webhook payload:
{"event": "payment_failed", "timestamp": "2024-06-01T16:00:00Z", "data": {"payment_id": 789}}

What is wrong with this payload?
medium
A. Payment ID should be a string, not a number
B. Timestamp format is incorrect
C. Event name should be inside data object
D. Missing closing brace for the JSON object

Solution

  1. Step 1: Check JSON completeness

    The payload ends without a closing brace '}' for the outermost JSON object, making it invalid JSON.
  2. Step 2: Verify other parts

    Timestamp format is ISO 8601 and correct. Event name is correctly outside data. Payment ID can be number or string depending on design.
  3. Final Answer:

    Missing closing brace for the JSON object -> Option D
  4. Quick Check:

    JSON must have matching braces [OK]
Hint: Always check for matching braces in JSON payloads [OK]
Common Mistakes:
  • Ignoring missing braces
  • Assuming timestamp format is wrong
  • Thinking event must be inside data
5. You want to design a webhook payload for a "user_deleted" event that includes the user ID and deletion reason. Which JSON payload design is best for clarity and easy parsing?
hard
A. { "event": "user_deleted", "timestamp": "2024-06-01T17:00:00Z", "data": { "user_id": 456, "reason": "requested by user" } }
B. { "user_deleted": true, "user": 456, "reason": "requested by user" }
C. { "event": "user_deleted", "user_id": 456, "reason": "requested by user" }
D. { "event": "user_deleted", "timestamp": "2024-06-01T17:00:00Z", "user": { "id": 456, "reason": "requested by user" } }

Solution

  1. Step 1: Check for standard webhook fields

    { "event": "user_deleted", "timestamp": "2024-06-01T17:00:00Z", "data": { "user_id": 456, "reason": "requested by user" } } includes event name, timestamp, and a nested data object holding user_id and reason, which is clear and standard.
  2. Step 2: Compare other options for clarity and parsing

    { "user_deleted": true, "user": 456, "reason": "requested by user" } lacks event and timestamp keys properly named. { "event": "user_deleted", "user_id": 456, "reason": "requested by user" } mixes data fields at root level, less organized. { "event": "user_deleted", "timestamp": "2024-06-01T17:00:00Z", "user": { "id": 456, "reason": "requested by user" } } nests reason inside user object, which is less intuitive.
  3. Final Answer:

    { "event": "user_deleted", "timestamp": "2024-06-01T17:00:00Z", "data": { "user_id": 456, "reason": "requested by user" } } -> Option A
  4. Quick Check:

    Standard event + timestamp + data object = { "event": "user_deleted", "timestamp": "2024-06-01T17:00:00Z", "data": { "user_id": 456, "reason": "requested by user" } } [OK]
Hint: Use event, timestamp, and data object for clear payloads [OK]
Common Mistakes:
  • Omitting timestamp
  • Mixing data fields at root level
  • Nesting unrelated fields inside user
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard