Golem add webhook moonbit

From golemcloud/golem · View interactive version

Golem add webhook moonbit is an AI agent skill from the GitHub repository golemcloud/golem. Using webhooks in a MoonBit Golem agent. Use when the user asks to create webhooks, receive webhook callbacks, integrate with webhook-driven external APIs, or generate temporary callback URLs for external services.

How to use this skill

Skill file

Using Webhooks in a MoonBit Golem Agent

Overview

Golem webhooks let an agent generate a public URL that, when POSTed to by an external system, delivers the request body to the agent. A webhook is backed by a Golem promise. A durable agent can suspend after the configured idle delay while waiting for the callback; persisted state and host metadata still consume storage.

This is useful for:

Prerequisites

The agent type must be deployed via an HTTP API mount (#derive.mount("/...") on the agent struct and an httpApi deployment in golem.yaml). Without a mount, webhooks cannot be created.

Related Skills

| Skill | When to Load |

|---|---|

| golem-add-http-endpoint-moonbit | Setting up the HTTP mount and endpoint annotations required before using webhooks |

| golem-configure-api-domain | Configuring httpApi in golem.yaml, including subdomain versus domain |

| golem-wait-for-external-input-moonbit | Lower-level promise API if you need more control than webhooks provide |

API

All functions are in the @webhook package of the Golem MoonBit SDK:

| Function / Type | Description |

|---|---|

| @webhook.create() | Creates a webhook (promise + public URL) and returns a WebhookHandler |

| WebhookHandler::url(self) | Returns the public URL to share with external systems |

| WebhookHandler::wait(self) | Async: waits for the webhook payload and returns WebhookRequestPayload |

| WebhookRequestPayload::text(self) | Decodes the POST body as a UTF-8 string |

| WebhookRequestPayload::bytes(self) | Returns the raw POST body as Bytes |

Webhook URL Structure

Webhook URLs have the form:

https://<domain>/<prefix>/<suffix>/<id>

```yaml

httpApi:

deployments:

local:

webhookUrl: "/my-custom-webhooks/"

agents:

OrderAgent: {}

```

Webhook Suffix

You can configure a webhook suffix using #derive.mount_webhook("/path") on the agent struct to override the default kebab-case agent name in the webhook URL:

#derive.agent
#derive.mount("/api/orders/{id}")
#derive.mount_webhook("/workflow-hooks")
struct OrderAgent {
  id : String
}

Webhook suffixes accept literal segments and system variables such as {agent-type}, not constructor variables such as {name}:

#derive.agent
#derive.mount("/api/events/{name}")
#derive.mount_webhook("/{agent-type}/callbacks")
struct EventAgent {
  name : String
}

Usage Pattern

1. Create a Webhook, Share the URL, and Await the Callback

Run the waiting fragments below inside an async fn. Any helper or method that calls them must also be async; MoonBit has no await keyword.

let webhook = @webhook.create()
let url = webhook.url()

// Share `url` with an external service (e.g., register it as a callback URL)
// Waiting can suspend the durable agent after the suspension policy delay.

let payload = webhook.wait()

2. Decode the Payload as Text

let webhook = @webhook.create()
// ... share webhook.url() ...
let payload = webhook.wait()
let body = payload.text()

3. Use Raw Bytes

let webhook = @webhook.create()
// ... share webhook.url() ...
let payload = webhook.wait()
let raw : Bytes = payload.bytes()

Complete Example

///|
#derive.agent
#derive.mount("/integrations/{name}")
#derive.mount_auth(false)
struct IntegrationAgent {
  name : String
  mut last_event : String
  mut pending : @webhook.WebhookHandler?
}

///|
fn IntegrationAgent::new(name : String) -> IntegrationAgent {
  { name, last_event: "", pending: None }
}

///|
/// Creates a webhook and publishes its URL before waiting in a separate invocation
#derive.endpoint(post="/register")
pub fn IntegrationAgent::register(self : Self) -> String {
  let webhook = @webhook.create()
  self.pending = Some(webhook)
  webhook.url()
}

///|
#derive.endpoint(post="/wait")
pub async fn IntegrationAgent::wait(self : Self) -> String {
  let webhook = match self.pending {
    Some(webhook) => webhook
    None => return "Register a webhook first"
  }
  let payload = webhook.wait()
  let body = payload.text()
  self.pending = None
  self.last_event = body
  self.last_event
}

///|
/// Returns the last received webhook event
#derive.endpoint(get="/last-event")
pub fn IntegrationAgent::get_last_event(self : Self) -> String {
  self.last_event
}

Key Constraints

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.