Webhooks and Event-Driven Automation
Lesson opener
In one line: A webhook is a push. The sender calls your URL when an event happens. Your handler must tolerate a second delivery of the same event.
Why it matters: Northline's ERP pushed "goods received" twice after a timeout. The handler posted two receipts. The item's on-hand doubled, and the webshop promised stock that was one receipt, not two.
Before this lesson: ERP integrations.
What you will be able to do
- Contrast pull (you call them on a schedule) with push (they call you when something happens).
- Name an event as a fact that already happened, with its own id.
- Accept a webhook only with the verification that provider documents.
- Store the event id so a repeat does not run the business action twice.
Prerequisites
Lessons 2 and 7. You will not register a public URL in this lesson.
Push, pull, events, triggers
Pull: your job calls the API on a clock. Lesson 6 is pull. You control the schedule. You can miss a change until the next run.
Push: the system of record calls your webhook URL when an event happens. You learn sooner. You do not control the sender's retry. Senders retry when your reply is slow or lost. A second delivery is normal, not a bug you may ignore.
An event in this course has:
event_id, unique for that occurrence.type, such asgoods.received.occurred_at, when the ERP recorded it.- The identifiers of the business objects, such as
receipt_idandsku.
The event is not the receipt itself. The handler loads the receipt from the system of record if it needs the full payload. That way a thin push cannot drift away from the ERP.
Verification. Use the signature, secret, or token that the sender's documentation specifies. If the documentation does not specify a check, do not invent one and do not treat the body as proof of origin. Refuse the call and ask the owner for a documented check. This course does not name a single algorithm as the one every vendor uses.
Acknowledgement. Reply 2xx only after you have stored the event_id. Do the slow work after that, or be ready to finish it inside the sender's documented time limit. A 2xx means "I have this event," not "the warehouse has finished putting stock away" unless your contract says so.
Automation trigger. A stored new event may start a defined action: refresh the webshop quantity for that SKU, or open a task for a person. The trigger is a rule you wrote. It is not "run whatever the JSON says."
seen = set()
def accept_event(event_id, verified):
if not verified:
return 401
if event_id in seen:
return 200
seen.add(event_id)
return 202
assert accept_event("evt-1", False) == 401
assert accept_event("evt-1", True) == 202
assert accept_event("evt-1", True) == 200
assert len(seen) == 1202 Accepted means the event is stored and the work may finish later. The duplicate returns 200 and does not store a second row. Either code is a 2xx so the sender can stop retrying. The important part is that the business action runs once.
Teacher walkthrough: goods received
Illustrative event:
{
"event_id": "evt-1",
"type": "goods.received",
"occurred_at": "2026-09-30T09:15:00+05:30",
"receipt_id": "R-19",
"sku": "FX-100"
}- The handler checks the verification the ERP's webhook docs describe. Fail closed with 401 if it does not pass.
evt-1is new. Store it. Reply 202.- The action reads receipt R-19 from the ERP and updates the webshop copy of on-hand. It does not add the quantity a second time inside the handler. The ERP remains the system of record.
- The sender times out and posts
evt-1again. The handler finds the id, replies 200, and does not read-and-add again. - Validation: on-hand increased once for R-19. The event table has one row for
evt-1.
A second workplace case
A pull job every 15 minutes reads receipts, and a webhook also pushes them. Both paths use receipt_id as the business identity. The webhook makes the copy fresh. The pull repairs a missed push. Neither path inserts a second receipt for R-19. Two transports, one identity.
Failure example: trust the body, add the quantity
Symptom: Each delivery of the webhook adds qty from the JSON onto the webshop count. Two deliveries double the stock.
Diagnosis: The handler treated a retried notification as a new receipt and used the push body as the system of record. The root cause is missing event identity and a write that should have been a refresh from the ERP.
Correction: Store event_id. On a repeat, do not run the action again. When the action runs, set the webshop copy from the ERP's current on-hand. Do not add the event's quantity on top.
Validation: Deliver the same event twice. The webshop quantity equals the ERP quantity, not twice the receipt.
Guided practice: three deliveries
Scenario: Events arrive in this order. Verification passes for the first and third. The second fails verification. Figures are illustrative.
evt-9, typegoods.received, receipt R-20.evt-9again, verification fails.evt-10, typeorder.cancelled, order S-8801.
Hints:
- Apply
accept_eventonly when you also consider verification. The second call in the story fails verification even though the id is known. - A failed verification must not mark the id as seen if you have not stored it yet. In this story the first call already stored
evt-9. - The cancel is a different action.
Complete solution:
The first call stores evt-9 and returns 202. The refresh from the ERP runs once. The second call fails verification and returns 401 even though the id is known. Do not delete the stored event because a failed retry arrived. The third call stores evt-10 and returns 202. Its action marks the webshop's copy of S-8801 as cancelled only after the ERP read confirms the cancellation. Validation: one stock refresh for R-20, one cancellation task for S-8801, and the 401 did not create a third business action.
Independent practice
Describe one event you would accept, or Northline's goods-received event.
Acceptance criteria:
- You say whether the flow is push, pull, or both, and why.
- The event has an id and a type.
- You name the verification from the sender's docs, or you refuse the call until that exists.
- You state what a second delivery does.
- The business write uses the system of record, not a running total inside the handler.
- You do not claim one signature algorithm is universal.
Common mistakes
- Adding the event quantity on every delivery.
- Replying 200 before the event id is stored, then losing the process and also telling the sender not to retry.
- Accepting an unverified body because the JSON "looks right."
- Inventing a signature scheme the provider does not document.
Quick check
- Who opens the connection in a push?
- Why do senders deliver the same event twice?
- What do you do if the provider documents no verification?
- What does a repeat of a stored
event_idchange in the business data? - Why read the receipt from the ERP instead of trusting the webhook body as the new on-hand?
Answers:
- The sender. Your URL receives the call.
- Because a timeout or a non-2xx makes them retry. A duplicate is expected.
- Refuse the call. Do not invent a check and do not accept the body as proof.
- Nothing. You acknowledge and skip the action.
- The ERP is the system of record. The webhook is a signal that something changed.
Lesson summary
Pull on a clock when you control the schedule. Push when you need the moment of change. Verify as documented, store the event id, and run the business action once against the system of record.
Bridge to the next lesson
Events and night jobs both land in analytics. The next lesson is how those calls become a batch or a stream into a warehouse: ETL, ELT, and the counts that tell you the load worked.