Golem add webhook ts

From golemcloud/golem · View interactive version

Golem add webhook ts is an AI agent skill from the GitHub repository golemcloud/golem. Using webhooks in a TypeScript 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 TypeScript 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 (http.mount(...) on defineAgent(...) and an httpApi deployment in golem.yaml). Without a mount, webhooks cannot be created.

Related Skills

| Skill | When to Load |

|---|---|

| golem-add-http-endpoint-ts | Setting up the http.mount(...) and endpoint declarations required before using webhooks |

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

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

API

All functions/classes are in @golemcloud/golem-ts-sdk:

| Function / Type | Description |

|---|---|

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

| WebhookHandler.getUrl() | Returns the public URL to share with external systems |

| WebhookHandler (await) | Implements PromiseLike — use await to get the WebhookRequestPayload |

| WebhookRequestPayload.json<T>() | Decodes the POST body as JSON |

| WebhookRequestPayload.bytes() | Returns the raw POST body as Uint8Array |

Imports

import { createWebhook } from '@golemcloud/golem-ts-sdk';

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 in the mount options of http.mount(...) to override the default kebab-case agent name in the webhook URL:

export const OrderAgent = defineAgent({
  name: 'OrderAgent',
  id: { id: z.string() },
  http: http.mount('/api/orders/{id}', { webhookSuffix: '/workflow-hooks' }),
  methods: { /* ... */ },
});

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

http: http.mount('/api/events/{name}', { webhookSuffix: '/{agent-type}/callbacks/{agent-version}' }),

Usage Pattern

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

const webhook = createWebhook();
const url = webhook.getUrl();

// 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

const payload = await webhook;

2. Decode the Payload as JSON

type PaymentEvent = { status: string; amount: number };

const webhook = createWebhook();
// ... share webhook.getUrl() ...
const payload = await webhook;
const event = payload.json<PaymentEvent>();

3. Use Raw Bytes

const webhook = createWebhook();
// ... share webhook.getUrl() ...
const payload = await webhook;
const raw: Uint8Array = payload.bytes();

Complete Example

import { z } from 'zod';
import { defineAgent, method, http, createWebhook } from '@golemcloud/golem-ts-sdk';

type WebhookEvent = { eventType: string; data: string };

export const IntegrationAgent = defineAgent({
  name: 'IntegrationAgent',
  id: { name: z.string() },
  http: http.mount('/integrations/{name}'),
  methods: {
    registerAndWait: method({ input: {}, returns: z.string(), http: http.post('/register') }),
    getLastEvent: method({ input: {}, returns: z.string(), http: http.get('/last-event') }),
  },
});

export const IntegrationAgentImpl = IntegrationAgent.implement({
  init: () => ({ lastEvent: '' }),
  methods: {
    async registerAndWait() {
      // 1. Create a webhook
      const webhook = createWebhook();
      const url = webhook.getUrl();

      // 2. In a real scenario, you would register `url` with an external service here.
      //    For this example, the URL is created so the caller can POST to it.
      //    Awaiting can suspend the durable agent after the suspension policy delay.

      // 3. Wait for the external POST
      const payload = await webhook;
      const event = payload.json<WebhookEvent>();

      this.lastEvent = `${event.eventType}: ${event.data}`;
      return this.lastEvent;
    },
    getLastEvent() {
      return this.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.