Get your API key

Lesson 3 of 5

Webhooks: when other apps call you

Polling vs webhooks, and the four rules of a safe webhook: a public HTTPS URL, a signature check, a fast 2xx reply, and processing each event only once.

7 min Log in to open
One step at a time, like flipping pages.

By the end of this lesson you will know what a webhook is, when you need one, and the four rules every webhook in your app must follow, so you can check the one your agent writes.

You need: lesson 1 done; no code today · Time: 7 minutes

  1. So far your app did the asking. But some news happens at a time you don't control: a customer pays, a student sends your bot a message, someone fills your form. How does your app find out?

    • Polling: your app keeps asking "anything new? anything new?", like refreshing your bank app every minute to see if the salary came.
    • Webhook: you give the other service an address once, and it calls you the moment something happens, like the bank sending you an SMS when the salary arrives.

    A webhook is a request that another service sends to your server when an event happens. Your server is the one answering this time.

    Two side-by-side diagrams. Left, Polling: your app asks a payment service "Paid yet?" three times, getting "no", "no", then "yes". Right, Webhook: once at setup your app says "tell me at /api/paid"; it stays quiet; then the payment service sends POST "paid" plus an event id, and your app answers 200 OK fast
    Polling keeps asking. A webhook waits to be called.
    Why?

    Polling wastes calls (and often money or rate limits), and you still learn late. Webhooks are instant, but they bring new responsibilities, because now strangers on the internet can send requests to your server too.

Log in free to read the rest of this lesson

Every lesson is free. A free account unlocks all of them and saves your progress.

Still to come in this lesson:

  1. Where you will meet webhooks
  2. Rule 1: a public HTTPS address
  3. Rule 2: check it really came from them
  4. Rules 3 and 4: reply fast, process each event once
  5. Make the agent prove all four rules
  6. Recap