Get started
Kotlin SDK
Fetch data

Receive webhooks

Receive webhooks

Mountaya delivers each change your webhook endpoints subscribe to as a signed POST. On a JVM server, constructWebhookEvent checks the signature and reads the event.

Verify a delivery

Pass the body exactly as it arrived, before any JSON parser reads it, with its Mountaya-Signature header:

import com.mountaya.admin.WebhookSignatureException
import com.mountaya.admin.constructWebhookEvent

val secret = System.getenv("MOUNTAYA_WEBHOOK_SECRET").orEmpty()

fun receive(body: ByteArray, signature: String?): Int {
    val event =
        try {
            constructWebhookEvent(body, signature, secret)
        } catch (refusal: WebhookSignatureException) {
            println(refusal.reason)
            return 400
        }
    // Queue the event and answer at once. Act on each event.id once.
    println("${event.id} ${event.type}")
    return 200
}

Answer a refused delivery with 400, and every other one with a 2xx within 10 seconds. Signatures explains the header.

Handle an event

Each event type has a class of WebhookEvent, so a when reads what each carries:

import com.mountaya.admin.generated.WebhookEvent

fun handle(event: WebhookEvent) {
    when (event) {
        is WebhookEvent.OrganizationMemberUpdated -> {
            // The membership as it is now, and the role it held before the change.
            println("${event.data.`object`.role}, was ${event.data.previous_attributes?.role}")
        }

        is WebhookEvent.CollectionDeleted -> println("collection ${event.data.`object`.id} deleted")
        else -> println("${event.type} at ${event.created_at}")
    }
}

Ignore unknown events

A type newer than your build arrives as WebhookEvent.Unknown. Answer it 2xx and move on:

import com.mountaya.admin.generated.WebhookEvent

fun isKnown(event: WebhookEvent): Boolean =
    when (event) {
        is WebhookEvent.Unknown -> {
            // A type added after this build: keep the id, act on nothing.
            println("unknown ${event.type}, id ${event.id}")
            false
        }

        else -> true
    }

Rotate the secret

Rotate the secret in the Mountaya app, under Organization → Webhooks. The previous secret keeps signing for up to 7 days, and the header carries a v1 for each. Deploy the new secret to MOUNTAYA_WEBHOOK_SECRET before the overlap ends: either secret verifies meanwhile.

Test your receiver

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

import com.mountaya.admin.WebhookSignatureException
import com.mountaya.admin.constructWebhookEvent
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.boolean
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.long
import java.io.File
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.time.Instant

class SignatureVectorsTest {
    @Test
    fun everyPublishedVectorVerifiesAsExpected() {
        // Downloaded from /schemas/v0.9.0/openapi/api/webhooks/signature-vectors.json.
        val vectors = Json.parseToJsonElement(File("signature-vectors.json").readText()).jsonObject
        for (case in vectors.getValue("cases").jsonArray.map { it.jsonObject }) {
            fun text(name: String) = case.getValue(name).jsonPrimitive.content
            val verified =
                try {
                    constructWebhookEvent(
                        text("body"),
                        text("header"),
                        text("secret"),
                        now = Instant.fromEpochSeconds(case.getValue("now").jsonPrimitive.long),
                    )
                    true
                } catch (refusal: WebhookSignatureException) {
                    false
                }
            val expected = case.getValue("expect").jsonObject.getValue("valid").jsonPrimitive.boolean
            assertEquals(expected, verified, text("name"))
        }
    }
}

Next steps

Last updated