Moonbase Documentation

Backend Hooks

Run logic when routes are ready, when purchases are fulfilled, or on shutdown.

The backend loader exposes three hook points:

Prop

Type

When to use hooks

Use onPurchaseFulfilled for post-purchase integrations like provisioning, syncing licenses, or notifying third-party services. Prefer it over wrapping payment-provider webhooks when you only need logic after Moonbase has already granted access.

Purchase fulfilled payload

The onPurchaseFulfilled callback receives a single payload object:

Prop

Type

Each item in payload.items includes:

Prop

Type

module.exports = {
  name: 'fulfillment-example',
  register(ctx) {
    ctx.hooks.onRoutesReady(() => {
      ctx.logger.info('routes ready');
    });

    ctx.hooks.onPurchaseFulfilled(async (payload) => {
      for (const item of payload.items) {
        ctx.logger.info('fulfilled purchase item', {
          eventId: payload.eventId,
          provider: payload.provider,
          userId: payload.userId,
          product: item.product.slug,
          plan: item.plan,
          quantity: item.quantity
        });
      }
    });

    ctx.hooks.onShutdown(() => {
      ctx.logger.info('api shutting down');
    });
  }
};

On this page