Skip to content

Honour X-Appwrite-Timestamp on webhook delete and update #14354

Open

Description

@ChiragAgg5k

Summary

DELETE /v1/webhooks/:webhookId deletes unconditionally. A client that decides to delete a webhook based on what it read earlier cannot make the delete depend on that read, so a concurrent update between the read and the delete is lost.

Appwrite already has a precondition for this on documents, rows and account writes: the X-Appwrite-Timestamp header. Webhook delete and update should honour it too.

Use case

The Appwrite MCP server (appwrite/mcp#127) implements MCP Events by managing one Appwrite webhook per subscription in the user's project. The server is stateless: the webhook is the only record of the subscription, and its name carries the lease expiry.

On each events/subscribe, the server cleans up the caller's webhooks whose lease expired more than 10 minutes ago, so stale subscriptions don't hold Free-plan webhook slots. Cleanup lists the webhooks, re-reads each candidate, and deletes it if its name and $updatedAt are unchanged.

The window between that re-read and the DELETE can't be closed from the client:

cleanup (request A)                 refresh (request B, same subscription)
GET /v1/webhooks/{id}   unchanged
                                    PUT /v1/webhooks/{id}   new lease, 200
                                    subscribe returns success to the client
DELETE /v1/webhooks/{id}  -> deleted, refresh lost

Client B was told its subscription is live, but its webhook is gone, so events are dropped until its next refresh. It's rare, but every client doing read-then-delete on webhooks has this gap. The mcp-event-subscriptions Appwrite profile asks for "conditional deletion or equivalent coordination" for this reason.

Proposal

Honour the existing X-Appwrite-Timestamp header on webhook delete and update, with the same semantics it has on documents and rows:

DELETE /v1/webhooks/{webhookId}
X-Appwrite-Timestamp: 2026-10-12T06:36:34.123+00:00
  • Header absent: behaves as today.
  • Stored $updatedAt is not later than the header: deletes and returns 204.
  • Stored $updatedAt is later than the header: returns 409 with a new webhook_update_conflict error, matching document_update_conflict and row_update_conflict, and nothing is deleted.
  • Webhook missing: returns 404 webhook_not_found as today.

The client sends the $updatedAt it read, so the check doesn't depend on clocks: any write after the read sets a later $updatedAt. That includes the delivery worker's writes (attempts, logs, auto-pause on failures), so a 409 can also mean a delivery failed since the read; read-then-delete clients treat it as "changed, re-read". The same header on PUT /v1/webhooks/{webhookId} (and the secret update) prevents lost updates between concurrent writes.

Implementation notes

Database::deleteDocument and updateDocument already do the check atomically: they read the row with forUpdate: true inside a transaction and throw ConflictException when $updatedAt is later than the request timestamp. The endpoint only needs to set it, as Databases/Collections/Documents/Delete.php does:

try {
    $dbForPlatform->withRequestTimestamp($requestTimestamp, fn () => $authorization->skip(
        fn () => $dbForPlatform->deleteDocument('webhooks', $webhook->getId())
    ));
} catch (ConflictException) {
    throw new Exception(Exception::WEBHOOK_UPDATE_CONFLICT);
}

Comparing $updatedAt in PHP between findOne and deleteDocument would leave the race in place server-side, so the check has to stay inside the database call.

Cover it by extending the webhook e2e tests in tests/e2e/Services/Webhooks/WebhooksBase.php: delete with the current $updatedAt succeeds; delete after an intervening update returns 409 and the webhook still exists.

Why not a new header, If-Match or an SDK param

  • A new X-Appwrite-If-Updated-At header with 412 would duplicate X-Appwrite-Timestamp with a different status code on one endpoint.
  • If-Match needs ETags, which Appwrite doesn't return for platform resources. If-Unmodified-Since only has second precision; $updatedAt has milliseconds.
  • X-Appwrite-Timestamp isn't a parameter in the SDKs on any endpoint today (SDK users set it as a client header). Exposing preconditions as SDK parameters should be one decision for every endpoint that honours the header, not a webhook-only param.

Related

Activity

  1. added
    enhancement
    product / platform
    on Oct 12, 2026
  2. added
    api / realtime
    product / auth
    product / databases
    product / functions
    product / sites
    product / storage
    product / vcs
    sdk / cli
    on Oct 12, 2026
  3. SahilArate commented on Oct 25, 2026

    @SahilArate
    Contributor

    Hi! I’d like to implement this. Before I start, one design question: should the precondition be the X-Appwrite-If-Updated-At header only, or also an optional ifUpdatedAt param so it appears in the SDKs and specs? My plan is to do the check inside withTransaction with a forUpdate read in Webhooks/Delete.php, return a new 412 error, and add e2e tests for the match, mismatch, missing and invalid-date cases. @ChiragAgg5k, if you are planning to build this yourself, I’m happy to leave it with you. Could this be assigned to me?

  4. removed
    product / storage
    product / databases
    product / auth
    product / functions
    api / realtime
    product / vcs
    product / sites
    sdk / cli
    on Nov 5, 2026
  5. Jishnu09-siuu commented on Nov 28, 2026

    @Jishnu09-siuu

    Hi! I'd like to work on this issue and can implement the conditional delete inside withTransaction using forUpdate: true in Webhooks/Delete.php along with the 412 error and E2E tests.
    Could you please assign this to me and confirm whether you prefer the X-Appwrite-If-Updated-At header only, or also an optional ifUpdatedAt parameter so it is exposed in the SDKs?

  6. changed the title [-]Conditional delete for webhooks (delete only if unchanged since read)[/-] [+]Honour X-Appwrite-Timestamp on webhook delete and update[/+] on Nov 28, 2026
  7. ChiragAgg5k commented on Nov 28, 2026

    @ChiragAgg5k
    MemberAuthor

    @SahilArate @Jishnu09-siuu thanks for offering. On the design question: neither a new header nor an ifUpdatedAt param. Appwrite already supports X-Appwrite-Timestamp as a precondition on documents and rows, so I've updated the proposal to reuse it on webhook delete and update, returning 409 webhook_update_conflict.

    #15450 is already open for this, so I've left review notes there to move it to that approach.

  8. sunztar commented on Nov 29, 2026

    @sunztar

    @SahilArate @Jishnu09-siuu thanks for offering. On the design question: neither a new header nor an ifUpdatedAt param. Appwrite already supports X-Appwrite-Timestamp as a precondition on documents and rows, so I've updated the proposal to reuse it on webhook delete and update, returning 409 webhook_update_conflict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementproduct / platform

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions