> ## Documentation Index
> Fetch the complete documentation index at: https://www.chapterjs.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Webhook

> Post messages in a channel under any name and avatar, and manage the webhooks of a server

A `Webhook` is a way to post messages in a channel under any name. You get one from `channel.createWebhook()`, `channel.fetchWebhooks()` or `guild.fetchWebhooks()`.

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
import type { TextChannel } from 'chapterjs';

async function relay(channel: TextChannel, author: string, text: string) {
  const webhook = await channel.createWebhook({ name: 'Relay' });
  await webhook.send({ content: text, username: author });
}
```

> [!NOTE]
>
> What a message can hold is in [Messages](https://www.chapterjs.org/messages.md); what `send()` takes here is [WebhookMessageOptions](https://www.chapterjs.org/reference/message-options.md#webhookmessageoptions).

> [!WARNING]
>
> The token of a webhook lets anyone post with it. It never appears when you log a webhook or call `toJSON()`, and the bot never gives it to your code.

## Properties

**id** `Snowflake`

The unique id Discord gave it.

**createdAt** `Date`

When it was created: a Discord id contains its creation date.

**name** `string | null`

The default name the webhook posts with.

**channelId** `Snowflake | null`

The id of the channel the webhook posts in.

**guildId** `Snowflake | null`

The id of the server of the webhook.

**canSend** `boolean`

Whether the bot can post with it: only webhooks it created.

## Helpers

These read what the bot remembers: they never send a request.

### toJSON()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
toJSON(): object
```

The data of the webhook, without its token: a token is a secret.

## Actions

### send()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
send(message: string | WebhookMessageOptions): Promise<Message>
```

Posts a message with the webhook. A text alone is its content; see [WebhookMessageOptions](https://www.chapterjs.org/reference/message-options.md#webhookmessageoptions) for the rest.

Only the bot that created a webhook can post with it (`canSend`). Otherwise `send()` throws:

```text theme={"theme":{"light":"github-light","dark":"vesper"}}
This webhook was not created by the bot: Discord only lets its creator post with it.
```

[Discord documentation](https://docs.discord.com/developers/resources/webhook#execute-webhook)

### edit()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
edit(
  options: { name?: string; avatar?: string | null; channelId?: Snowflake },
  reason?: string
): Promise<Webhook>
```

Changes the name, the avatar or the channel of the webhook. See [webhook edit options](#webhook-edit-options).

[Discord documentation](https://docs.discord.com/developers/resources/webhook#modify-webhook)

### delete()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
delete(reason?: string): Promise<void>
```

Deletes the webhook.

[Discord documentation](https://docs.discord.com/developers/resources/webhook#delete-webhook)

## Types

### Webhook edit options

What `edit()` takes. Every field can be left out: only what you pass changes.

**name** `string`

The default name of the webhook.

**avatar** `string | null`

The image of the default avatar of the webhook, as a data URI (`data:image/png;base64,...`); `null` removes it.

**channelId** `Snowflake`

The id of the channel to move the webhook to.

[Discord documentation](https://docs.discord.com/developers/resources/webhook#modify-webhook-json-params)

`WebhookMessageOptions` is in [Message options](https://www.chapterjs.org/reference/message-options.md#webhookmessageoptions).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.