Golem add webhook scala

From golemcloud/golem · View interactive version

Golem add webhook scala is an AI agent skill from the GitHub repository golemcloud/golem. Using webhooks in a Scala 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 Scala 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. Under the hood, a webhook is backed by a Golem promise. Creation does not suspend the agent. For durable agents awaiting a callback, suspension can occur after the suspension policy delay when all active work permits it. Suspension frees guest execution resources; persisted state and other resources remain.

This is useful for:

Prerequisites

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

Related Skills

| Skill | When to Load |

|---|---|

| golem-add-http-endpoint-scala | 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-scala | Lower-level promise API if you need more control than webhooks provide |

API

All functions are on the golem.HostApi object:

| Function / Type | Description |

|---|---|

| HostApi.createWebhook() | Creates a webhook (promise + public URL) and returns a WebhookHandler |

| WebhookHandler.url | The public URL to share with external systems |

| WebhookHandler.await() | Awaits the webhook POST payload asynchronously (Future[WebhookRequestPayload]) |

| WebhookRequestPayload.json[A]() | Decodes the POST body as JSON (requires implicit Schema[A]) |

| WebhookRequestPayload.bytes | Returns the raw POST body as Array[Byte] |

Imports

import golem.HostApi
import golem.HostApi.WebhookRequestPayload
import scala.concurrent.Future
import golem.concurrent.Implicits.global

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 webhookSuffix on the @agentDefinition annotation to override the default kebab-case agent name in the webhook URL:

@agentDefinition(mount = "/api/orders/{id}", webhookSuffix = "/workflow-hooks")
trait OrderAgent extends BaseAgent {
  class Id(val id: String)
  // ...
}

Only system variables such as {agent-type} and {agent-version} are supported in webhookSuffix, not constructor parameters such as {name}:

@agentDefinition(mount = "/api/events/{name}", webhookSuffix = "/{agent-type}/callbacks/{agent-version}")

Usage Pattern

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

val webhook = HostApi.createWebhook()
val url = webhook.url

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

val payload: Future[WebhookRequestPayload] = webhook.await()

2. Create a Webhook and Await Asynchronously

import scala.concurrent.Future
import golem.concurrent.Implicits.global

val webhook = HostApi.createWebhook()
val url = webhook.url
// ... share url ...

val result: Future[WebhookRequestPayload] = webhook.await()
val bytes: Future[Array[Byte]] = result.map(_.bytes)

3. Decode the Payload as JSON

import zio.blocks.schema.Schema

case class PaymentEvent(status: String, amount: Long) derives Schema

val webhook = HostApi.createWebhook()
// ... share webhook.url ...
val event: Future[PaymentEvent] = webhook.await().map(_.json[PaymentEvent]())

4. Use Raw Bytes

val webhook = HostApi.createWebhook()
// ... share webhook.url ...
val raw: Future[Array[Byte]] = webhook.await().map(_.bytes)

Complete Example

import golem.*
import golem.runtime.annotations.{agentDefinition, agentImplementation, endpoint}
import zio.blocks.schema.Schema

import scala.concurrent.Future
import golem.concurrent.Implicits.global

case class WebhookEvent(eventType: String, data: String) derives Schema

@agentDefinition(mount = "/integrations/{name}")
trait IntegrationAgent extends BaseAgent {
  class Id(val name: String)

  @endpoint(method = "POST", path = "/register")
  def registerAndWait(): Future[String]

  @endpoint(method = "GET", path = "/last-event")
  def getLastEvent(): String
}

@agentImplementation()
class IntegrationAgentImpl(private val name: String) extends IntegrationAgent {
  private var lastEvent: String = ""

  override def registerAndWait(): Future[String] = {
    // 1. Create a webhook
    val webhook = HostApi.createWebhook()
    val url = webhook.url

    // Publish the URL before waiting (replace with your callback registration).
    println(s"Webhook for $name: $url")

    // 3. Wait for the external POST
    webhook.await().map { payload =>
      val event = payload.json[WebhookEvent]()
      lastEvent = s"${event.eventType}: ${event.data}"
      lastEvent
    }
  }

  override def getLastEvent(): String = lastEvent
}

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.