# http.respondWebhook

Sends a reply to a webhook request whose caller is waiting for one. The caller receives the reply in its HTTP response.

```javascript
await http.respondWebhook(event.correlationId, payload);
```

# Input

| Variable | Type | Description |
|----------|------|-------------|
| `correlationId` | string | The `correlationId` of the `playground_webhook` event you are replying to. See [http.requestWebhook](/doc/587ae5fe-54b2-4f87-bf0e-fac2b0998baa)  |
| `payload` | any  | The reply: any JSON value (object, array, string, number, boolean or `null`), up to 4096 characters once converted to JSON. |

# Output

This function returns a Promise that resolves with `true` once the reply has been sent.

If the reply is refused, the Promise rejects and nothing is sent:

| Error | Reason |
|-------|--------|
| Webhook response correlationId is required | `correlationId` is missing or empty. |
| Webhook response payload must be JSON-serializable | `payload` is `undefined`, a function, or another value with no JSON form. |
| Webhook response payload too large | `payload` is longer than 4096 characters as JSON. |

An object that refers to itself can't be converted to JSON either; it rejects with JavaScript's own error.

# Comments

The caller waits for a reply only if its JSON request body contains `"waitResponse": true`.


:::warning
Reply within **30 seconds** of the request. After that, the caller receives an HTTP 504 error and your reply is ignored. `true` means the reply was sent, not that the caller received it.

:::

Only the first reply to a request counts. Later replies with the same `correlationId` are ignored.

The reply goes only to the caller. Other scripts on the same bot never receive it.

# What the caller receives

When the script replies in time (HTTP 200):

```json
{ "success": true, "correlationId": "…", "payload": { "received": "hello" } }
```

When there is no reply within 30 seconds (HTTP 504):

```json
{ "success": false, "error": "Timed out waiting for script response", "correlationId": "…" }
```

# Examples

A script that answers every request that waits for a reply:

```javascript
// Event should be placed first, to catch any early requests.
Bot.on("playground_webhook", async (event) => {
  // Reply only when the caller is waiting for it
  if(event.payload.waitResponse !== true) {
    return;
  }

  try {
    await http.respondWebhook(event.correlationId, { received: event.payload.message });
  } catch(error) {
    console.log("Reply refused:", error.message);
  }
});

const webhook = await http.requestWebhook();
console.log("Webhook URL:", webhook.url);
```

The server's request and the reply it receives:

```bash
curl -X POST "$WEBHOOK_URL" \
  -H "Authorization: Bearer $WEBHOOK_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"waitResponse": true, "message": "hello"}'

# {"success":true,"correlationId":"…","payload":{"received":"hello"}}
```

For details on using the webhook, see [http.requestWebhook](/doc/587ae5fe-54b2-4f87-bf0e-fac2b0998baa) and the [playground_webhook](/doc/73d8b4d0-2d66-4c87-af1c-c6d8bbd989e5) event documentation.