What is Webhook? How it works and why it is important?
Webhook
Automation

What is Webhook? How it works and why it is important?

lets understand about webhook that what is it and how it works and why it is important with some code example and real world usecases through in this tidbit
DeveriponDeveripon5 min read

Webhooks: The Superhero of Modern Web Development 🚀

Today we're diving into Webhooks - a technology that has revolutionized modern web development. If you've ever wondered how Facebook sends you instant notifications, or how online payment systems work in real-time, this article is for you.

What is a Webhook? 🤔

A Webhook is an automatic notification system. Simply put, it's a "tell me when something happens" system.

Understanding with Real Life Examples:

Like ordering pizza:

text
1
2
3
4
5
6
You order a pizza 🍕
↓
Restaurant is preparing it 👨‍🍳
↓
When pizza is ready, restaurant calls you 📞

Webhooks work exactly the same way:

text
1
2
3
4
5
6
User makes a payment 💳
↓
Stripe is processing it ⚡
↓
When payment succeeds, Stripe calls your server 🔔

How Do Webhooks Work? ⚙️

1. Setup Phase:

You tell the service provider (like Stripe):

text
"Hey Stripe, when payment completes, call this endpoint:
https://my-website.com/webhook/stripe"

2. Event Happens:

text
User makes a payment → Stripe processes the payment

3. Automatic Notification:

text
Payment successful → Stripe automatically sends POST request to your server

4. Your Server Response:

text
Your server receives notification → Takes instant action (create booking, send email, etc.)

Understanding with Code Examples 💻

Backend (NestJS):

typescript
@Controller('webhook')
export class WebhookController {
  
  @Post('stripe')
  async handleStripeWebhook(@Body() data: any) {
    console.log("🔔 Notification received from Stripe!");
    
    if (data.type === 'payment_intent.succeeded') {
      console.log("💰 Payment successful!");
      
      // Create booking now
      await this.createBooking(data.data.object);
      
      // Send email notification
      await this.sendConfirmationEmail(data.data.object);
    }
    
    return { received: true };
  }
}

Stripe Dashboard Setup:

text
Webhook URL: https://your-website.com/webhook/stripe
Events: payment_intent.succeeded, checkout.session.completed

Webhooks vs Traditional API Calls 🆚

Traditional API Call (You make the call):

text
1
2
3
4
5
6
7
8
9
10
11
Your server → Stripe API → "What's the payment status?"
           ← Response   ← "Still processing..."

// 5 seconds later
Your server → Stripe API → "What about now?"
           ← Response   ← "Still processing..."

// Another 5 seconds later
Your server → Stripe API → "Now?"
           ← Response   ← "Completed!"

Webhook (They call you):

text
User makes payment → Stripe processing → Payment complete
                                      ↓
                   Stripe → Your server → "Payment successful!"

Real-world Applications 🌍

1. E-commerce Payments:

text
User checkout → Payment gateway → Webhook → Order confirmation

2. Social Media Notifications:

text
Someone likes your post → Platform → Webhook → Push notification

3. CI/CD Pipeline:

text
Code push → GitHub → Webhook → Automatic deployment

4. Email Marketing:

text
User subscribes → Form → Webhook → Welcome email series

Why Are Webhooks So Important? 🎯

1. Instant Response

text
❌ Manual checking: Found out 30 seconds later

✅ Webhook: Found out in 0.5 seconds

2. Resource Efficient

text
❌ Polling: Repeated API calls = server load

✅ Webhook: Only calls when event happens = minimal load

3. Reliability

text
❌ Manual check: Possibility of missing events

✅ Webhook: Automatic retry mechanism

4. Better User Experience

text
❌ "Processing... please wait"

✅ Instant confirmations and updates

Common Webhook Providers 🏢

Payment Gateways:

⦿ Stripe: Most popular, excellent webhook support

⦿ PayPal: Comprehensive event system

⦿ Razorpay: Indian market leader

Development Tools:

⦿ GitHub: Code push, PR merge events

⦿ GitLab: CI/CD pipeline events

⦿ Vercel: Deployment notifications

Communication:

⦿ Twilio: SMS/Call events

⦿ SendGrid: Email delivery events

⦿ Slack: Message, reaction events

Best Practices 📋

1. Security First:

typescript
// Verify webhook signature
const signature = req.headers['stripe-signature'];
const event = stripe.webhooks.constructEvent(
  req.body,
  signature,
  process.env.STRIPE_WEBHOOK_SECRET
);

2. Handle Idempotency:

typescript
// Handle duplicate events
const existingRecord = await db.findUnique({
  where: { webhookEventId: event.id }
});

if (existingRecord) {
  return; // Already processed
}

3. Error Handling:

typescript
try {
  await processWebhook(event);
} catch (error) {
  console.error('Webhook processing failed:', error);
  // Log for debugging
  // Return appropriate status code
}

4. Timeout Management:

typescript
// Respond quickly
app.post('/webhook', async (req, res) => {
  res.status(200).json({ received: true });
  
  // Process in background
  processWebhookAsync(req.body);
});

Testing Webhooks in Development 🧪

Local Development:

bash
# Using Stripe CLI
stripe listen --forward-to localhost:3001/webhook/stripe

# Or using ngrok
ngrok http 3001

Testing Tools:

⦿ Stripe CLI: Built-in testing

⦿ Webhook.site: Generic webhook testing

⦿ Postman: Custom webhook simulation

Common Pitfalls and Solutions 🚨

1. Webhook Timeout:

text
❌ Long processing in webhook handler
✅ Quick acknowledgment + background processing

2. Missing Security:

text
❌ Raw body processing without verification
✅ Signature verification for authenticity check

3. No Retry Logic:

text
❌ One-time failure = permanent failure
✅ Implement retry mechanism

4. Duplicate Processing:

text
❌ Processing same event multiple times
✅ Duplicate prevention with idempotency keys

Complete Example: Trip Booking System 🎫

Frontend:

typescript
const handleBooking = async () => {
  const response = await fetch('/api/create-checkout', {
    method: 'POST',
    body: JSON.stringify({ tripId, userId, amount })
  });
  
  const { checkoutUrl } = await response.json();
  window.location.href = checkoutUrl;
};

Backend Webhook:

typescript
@Post('webhook/stripe')
async handleStripeWebhook(@Body() event: any) {
  if (event.type === 'checkout.session.completed') {
    const session = event.data.object;
    
    // Database transaction
    await this.prisma.$transaction(async (tx) => {
      // Payment record
      const payment = await tx.payment.create({
        data: {
          stripeSessionId: session.id,
          amount: session.amount_total / 100,
          status: 'completed'
        }
      });
      
      // Booking record
      await tx.booking.create({
        data: {
          userId: session.metadata.userId,
          tripId: session.metadata.tripId,
          paymentId: payment.id,
          status: 'confirmed'
        }
      });
    });
    
    // Send confirmation email
    await this.emailService.sendBookingConfirmation(session);
  }
}

Conclusion 🎯

Webhooks are an essential part of modern web development. They make your applications more responsive, efficient, and user-friendly.

As a developer, proper webhook implementation will significantly improve your application's quality and user experience.

Key Takeaways:

✅ Webhooks = Automatic notification system
✅ Enables real-time communication
✅ Saves server resources
✅ Provides better user experience
✅ Essential for modern applications


Happy Coding! 🚀

Learn Full Stack

with tapaScript

By clicking Sign Up you re confirming that you agree with our Terms and Conditions.

newsletter

Don't forget to connect with us on