Skip to content
Back

How do I pair Polar.sh + Hono + Appwrite functions?

  • 0
  • Functions
Abhishek
14 Jul, 2025, 19:42

This is what all it required by polar to see the webhook data. Managing with webhook data is not an issue, but how do I pair this all with appwrite functions structure so I will be able to verify the signature and get the webhook data in the first place?

TypeScript
import { Hono } from "hono";
import { Webhooks } from "@polar-sh/hono";

const app = new Hono();

app.post(
    "/polar/webhooks",
    Webhooks({
        webhookSecret: process.env.POLAR_WEBHOOK_SECRET!,
        onPayload: async (payload) => {
            console.log("Received webhook payload:", payload);
        },
    })
);
TL;DR
Developers are seeking help on pairing Polar.sh, Hono, and Appwrite functions. They shared code snippets for validating events and handling webhooks but are unsure how to integrate them. They need guidance on converting Appwrite context into a Hono context. Solution: To integrate the functionalities, developers need to use the provided code snippet that sets up webhooks and handles payloads, ensuring proper verification.
Abhishek
14 Jul, 2025, 19:56
TypeScript
var Webhooks = ({
  webhookSecret,
  onPayload,
  entitlements,
  ...eventHandlers
}) => {
  return async (c) => {
    const requestBody = await c.req.text();
    const webhookHeaders = {
      "webhook-id": c.req.header("webhook-id") ?? "",
      "webhook-timestamp": c.req.header("webhook-timestamp") ?? "",
      "webhook-signature": c.req.header("webhook-signature") ?? ""
    };
    let webhookPayload;
    try {
      webhookPayload = validateEvent(
        requestBody,
        webhookHeaders,
        webhookSecret
      );
    } catch (error) {
      if (error instanceof WebhookVerificationError) {
        return c.json({ received: false }, { status: 403 });
      }
      throw error;
    }
    await handleWebhookPayload(webhookPayload, {
      webhookSecret,
      entitlements,
      onPayload,
      ...eventHandlers
    });
    return c.json({ received: true });
  };
};
Abhishek
14 Jul, 2025, 19:56
TypeScript
const validateEvent = (body, headers, secret) => {
    const base64Secret = Buffer.from(secret, "utf-8").toString("base64");
    const webhook = new Webhook(base64Secret);
    try {
        const parsed = webhook.verify(body, headers);
        return parseEvent(parsed);
    }
    catch (error) {
        if (error instanceof _WebhookVerificationError) {
            throw new WebhookVerificationError(error.message);
        }
        throw error;
    }
};
Kenny
14 Jul, 2025, 19:58

what does your main entry point to the function look like

Abhishek
14 Jul, 2025, 20:00

What do you mean?

Kenny
14 Jul, 2025, 20:00

How is this being served by appwrites function?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more