DEV Community

Cover image for How Do You Build Event-Driven APIs with Webhooks and Message Queues?
Wanda
Wanda

Posted on • Originally published at apidog.com

How Do You Build Event-Driven APIs with Webhooks and Message Queues?

TL;DR

Event-driven APIs use webhooks for notifying external clients and message queues for handling internal processing. Publish events to a queue (like RabbitMQ or Kafka), process them asynchronously using background workers, and send notifications to clients via webhooks. Modern PetstoreAPI follows this pattern for handling orders, inventory updates, and payment notifications.

Introduction

A customer places an order. Your API needs to charge payment, update inventory, send an email, notify the warehouse, and trigger webhooks. Should you handle all this synchronously and make the customer wait, or respond immediately and process in the background?

Try Apidog today

Event-driven APIs respond quickly and process work asynchronously. For example, the order endpoint returns 201 Created right away, then triggers background processing. Once complete, webhooks notify interested clients.

Modern PetstoreAPI uses this event-driven architecture for orders, payments, and inventory.

Apidog helps you test webhooks, validate event flows, and simulate async processing.

Event-Driven Architecture

Event-driven APIs emit events when actions occur. Other services subscribe and react to these events.

Components

  1. Event Producer – API endpoint that publishes events
  2. Event Bus/Queue – Routes events (RabbitMQ, Kafka, SQS)
  3. Event Consumer – Background workers that process events
  4. Webhooks – Notify external clients

Flow

Client → POST /orders → API
API → Publish "order.created" → Queue
API → Return 201 Created → Client
Worker → Consume event → Process order
Worker → Publish "order.completed" → Queue
Webhook Worker → Send webhook → Client
Enter fullscreen mode Exit fullscreen mode

Webhooks for External Events

Webhooks notify external systems when significant events happen.

Example: Notify clients when an order is completed

// Publish event when order is created
app.post('/v1/orders', async (req, res) => {
  const order = await createOrder(req.body);

  await eventBus.publish('order.created', {
    orderId: order.id,
    userId: order.userId,
    total: order.total
  });

  res.status(201).json(order);
});

// Background worker sends webhooks
eventBus.subscribe('order.completed', async (event) => {
  const webhooks = await getWebhooks(event.userId, 'order.completed');

  for (const webhook of webhooks) {
    await sendWebhook(webhook.url, {
      event: 'order.completed',
      data: event
    });
  }
});
Enter fullscreen mode Exit fullscreen mode

Message Queues for Internal Events

Message queues decouple internal event processing, improving scalability and reliability.

RabbitMQ Example

Publisher:

await publishEvent('order.created', { orderId: '019b4132' });
Enter fullscreen mode Exit fullscreen mode

Consumer:

await consumeEvents('order.*', async (event) => {
  await processOrder(event);
});
Enter fullscreen mode Exit fullscreen mode

How Modern PetstoreAPI Implements Event-Driven APIs

PetstoreAPI uses event-driven patterns for order processing:

  1. POST /orders returns 201 Created immediately
  2. Publishes order.created event
  3. Payment worker processes payment
  4. Inventory worker updates stock
  5. Email worker sends confirmation
  6. Webhook worker notifies the client

See Modern PetstoreAPI event architecture.

Testing with Apidog

Apidog is useful for validating event-driven APIs:

  • Test webhook delivery
  • Validate event payloads
  • Simulate async processing
  • Test retry logic

Conclusion

Event-driven APIs enhance performance and scalability. Use webhooks for external client notifications, and message queues for internal processing. Modern PetstoreAPI is a practical example of robust, production-ready event-driven design.

FAQ

What’s the difference between webhooks and message queues?

Webhooks notify external clients over HTTP. Message queues handle internal service communication.

Which message queue should I use?

RabbitMQ for simplicity, Kafka for high throughput, AWS SQS for managed service.

How do you handle webhook failures?

Implement retry logic with exponential backoff (see our webhook reliability guide).

Can you use events without message queues?

Yes, but queues provide durability, retry, and decoupling.

How do you test event-driven APIs?

Use Apidog to test webhooks and validate event flows.

Top comments (0)