Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

Webhook Event Processor

Build what Stripe and PayPal integrations run in production: receive a batch of JSON events, validate them, compute per-user balances, and answer with a proper API response.

Advanced 4 steps 130 XP ~50 min

How it works in the real world

When a customer pays through Stripe, Stripe POSTs a webhook — a JSON event — to the shop's server. The server must:

  1. Parse — decode the JSON payload into Python objects.
  2. Validate — real traffic contains junk: unknown event types, impossible amounts. Filter, never crash.
  3. Process — apply business logic: payments add to a balance, refunds subtract.
  4. Respond — return a JSON summary with an honest status code so the sender knows what happened.

This parse → validate → process → respond loop is the beating heart of every backend integration you'll ever build.

Build progress0 / 4 steps
1

Parse the payload

Parse the JSON in raw with json.loads. Print how many events arrived and the type of the first one: 5 then payment.

Blank · autosaved
PYstep_1.py
Run your code to check this step…
2

Validate the events

An event is valid when its type is payment or refund AND its amount is > 0. Build the valid list; print how many are valid and how many were rejected: 3 then 2.

Blank · autosaved
PYstep_2.py
Run your code to check this step…
3

Process: per-user balances

Payments add to a user's balance; refunds subtract. Compute balances from the valid events and print each user as user: balance (first-seen order): ana: 90 then bo: 80.

Blank · autosaved
PYstep_3.py
Run your code to check this step…
4

Respond like an API

Build the response dict — {"status": 200, "processed": 3, "skipped": 2, "balances": {...}} — and print it with json.dumps. Expected exactly:
{"status": 200, "processed": 3, "skipped": 2, "balances": {"ana": 90, "bo": 80}}

Blank · autosaved
PYstep_4.py
Run your code to check this step…
Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path