Get started
Swift SDK
Fetch data

Receive webhooks

Receive webhooks

Mountaya sends each event your endpoint subscribes to as a signed POST. constructWebhookEvent checks the signature and gives you a typed event. Webhooks explains deliveries, and signatures the header.

Verify a delivery

Hand it the body exactly as it arrived, the Mountaya-Signature header and your endpoint's secret:

import Foundation
import MountayaAdmin
import MountayaClient

let secret = ProcessInfo.processInfo.environment["MOUNTAYA_WEBHOOK_SECRET"] ?? ""

// On a server: the body exactly as it arrived and its Mountaya-Signature header in, the status out.
func receive(body: Data, signature: String?) -> Int {
  let event: WebhookEvent
  do {
    event = try constructWebhookEvent(rawBody: body, signatureHeader: signature, secret: secret)
  } catch MountayaError.webhookSignature(let refusal) {
    print(refusal.reason)
    return 400
  } catch {
    return 500
  }

  // Queue the event and answer at once. Deduplicate on event.id later.
  print(event.id, event.type.rawValue)
  return 200
}

Read the body before any JSON parser touches it. A body decoded and encoded again is other bytes, which do not verify.

Handle an event

WebhookEvent has a case for each event type, so a switch reads what each one carries:

import MountayaAdmin
import MountayaClient

func handle(_ event: WebhookEvent) {
  switch event {
  case .organizationMemberUpdated(let updated):
    // The membership as it is now, and the role it held before the change.
    let before = updated.data.previous_attributes?.role?.rawValue ?? ""
    print("role", before, "->", updated.data.object.role.rawValue)
  case .collectionDeleted(let deleted):
    print("collection \(deleted.data.object.id) deleted")
  default:
    break
  }
}

An event can arrive more than once and in any order. Act on each id once.

Ignore unknown events

An event type newer than your build arrives as .unknown, with the raw envelope:

import MountayaAdmin

func handleAny(_ event: WebhookEvent) {
  if case .unknown(let envelope) = event {
    print("not handled yet:", envelope.type.rawValue)
    return
  }
  print("handled", event.id)
}

Answer it 200 like any other, so Mountaya does not send it again.

Rotate the secret

Rotate a secret in the Mountaya app, under Organization → Webhooks. For up to 7 days both secrets sign, and either one verifies, so deploy the new secret to your server before the overlap ends.

Test your receiver

Check your receiver against the published test vectors, with now: pinned to each case's clock:

import Foundation
import MountayaAdmin
import MountayaClient
import Testing

// signature-vectors.json, downloaded from the signatures page.
struct SignatureVectors: Decodable {
  struct Case: Decodable {
    struct Expect: Decodable { let valid: Bool }
    let name: String
    let secret: String
    let now: TimeInterval
    let header: String
    let body: String
    let expect: Expect
  }
  let cases: [Case]
}

@Test func verifiesThePublishedVectors() throws {
  let file = URL(fileURLWithPath: "signature-vectors.json")
  let vectors = try JSONDecoder().decode(SignatureVectors.self, from: Data(contentsOf: file))
  for vector in vectors.cases {
    var verifies = true
    do {
      _ = try constructWebhookEvent(
        rawBody: vector.body, signatureHeader: vector.header, secret: vector.secret,
        now: Date(timeIntervalSince1970: vector.now))
    } catch MountayaError.webhookSignature {
      verifies = false
    }
    #expect(verifies == vector.expect.valid, "\(vector.name)")
  }
}

To try a real delivery, send your endpoint a test event from Organization → Webhooks, or with postOrganizationWebhookDelivery.

Next steps

Last updated