Get started

Receive webhooks

Mountaya sends each event your webhook endpoint subscribes to as a signed POST. Verify the signature, then act on the typed event.

Verify a delivery

constructWebhookEvent checks the delivery's Mountaya-Signature against your endpoint's secret, then returns the event. Hand it the body exactly as it arrived:

import 'dart:io';
import 'dart:typed_data';

import 'package:mountaya_admin/mountaya_admin.dart';

final secret = Platform.environment['MOUNTAYA_WEBHOOK_SECRET'] ?? '';

// Call it with the body exactly as it arrived and its Mountaya-Signature header.
int receive(Uint8List body, String? signature) {
  final WebhookEvent event;
  try {
    event = constructWebhookEvent(body, signature, secret);
  } on WebhookSignatureException catch (refusal) {
    print(refusal.reason);
    return 400;
  }

  // Queue the event and answer at once. Act on each event.id once.
  print('${event.id} ${event.type}');
  return 200;
}

Your framework's route calls it, and answers with the status it returns. Verify webhook signatures shows how the signature is computed.

Handle an event

Each event type is a class of its own, so a switch reads what each carries:

import 'package:mountaya_admin/mountaya_admin.dart';

// Called with each event constructWebhookEvent answered.
void handle(WebhookEvent event) {
  switch (event) {
    case OrganizationMemberUpdatedEvent(:final data):
      // The member's role now, and the one they held before.
      print('${data.object.role}, was ${data.previous_attributes?.role}');
    case CollectionDeletedEvent(:final data):
      print('Collection ${data.object.id} deleted');
    default:
      print('Not handled here: ${event.type}');
  }
}

Mountaya delivers an event at least once and in no set order. Keep what you act on keyed by event.id.

Ignore unknown events

An event this build cannot type still arrives, as UnknownWebhookEvent. Answer it with a 2xx like any other:

import 'package:mountaya_admin/mountaya_admin.dart';

// What a log line says of each delivery.
String describe(WebhookEvent event) => switch (event) {
  OrganizationMemberUpdatedEvent(:final data) => 'Member now ${data.object.role}',
  UnknownWebhookEvent() => 'A type this build does not know: ${event.type}',
  _ => 'Not handled here: ${event.type}',
};

Rotate the secret

Rotate a secret in the Mountaya app, under Organization → Webhooks. Both secrets sign every delivery for up to 7 days, and either verifies, so deploy the new one before the overlap ends.

Test your receiver

Check your code against the published test vectors. now: pins the clock each case is judged by:

import 'dart:convert';
import 'dart:io';

import 'package:mountaya_admin/mountaya_admin.dart';
import 'package:test/test.dart';

void main() {
  // Downloaded from the test vectors link on Verify webhook signatures.
  final vectors = jsonDecode(
    File('test/signature-vectors.json').readAsStringSync(),
  );
  final cases = switch (vectors) {
    {'cases': final List<Object?> cases} => cases,
    _ => const <Object?>[],
  };
  for (final entry in cases) {
    if (entry case {
      'name': final String name,
      'secret': final String secret,
      'now': final int now,
      'header': final String header,
      'body': final String body,
      'expect': {'valid': final bool valid},
    }) {
      test(name, () {
        var verified = true;
        try {
          constructWebhookEvent(
            body,
            header,
            secret,
            now: DateTime.fromMillisecondsSinceEpoch(now * 1000, isUtc: true),
          );
        } on WebhookSignatureException {
          verified = false;
        }
        expect(verified, valid);
      });
    }
  }
}

To send a real test event to your endpoint, call mountaya.admin.webhooks.postOrganizationWebhookDelivery with test:, or use Send test event in the Mountaya app.

Next steps

Last updated