KlangDocs

    Open Klang

    Bot API reference

    View as Markdown

    Webhooks

    Creating, listing, changing, moving and deleting a server’s incoming webhooks with a bot token, and renaming or deleting one with its own token. What each route needs, the webhook object, the limits, the errors and the events.

    Before you read

    A bot with Manage webhooks can do with a server’s webhooks what people do in Server settings › Integrations › Webhooks, with the same rules:

    • Manage webhooks (MANAGE_WEBHOOKS) in the webhook’s channel, for every route here.
    • Webhooks live in text channels: up to 15 per channel and 50 per server.
    • Every change goes to the server’s audit log, with the reason in X-Audit-Log-Reason, and the bots with GUILD_WEBHOOKS that see the channel get a WEBHOOKS_UPDATE.
    • Answers carry the webhook object, with its token and its url: whoever manages a webhook can read them again, as in Discord.

    Posting with a webhook needs no bot: see Execute a webhook. What a webhook’s messages look like, and how to keep its URL safe, is in Webhooks.

    With discord.js and discord.py:

    // Create one in a channel, with an avatar and a reason for the audit log.
    const webhook = await channel.createWebhook({
      name: 'Harbor log',
      avatar: './harbor-log.png',
      reason: 'Tide alerts for the harbor',
    });
    await webhook.send('High tide at 18:42.');
    
    // The channel's webhooks, or all of the server's.
    const inChannel = await channel.fetchWebhooks();
    const inServer = await channel.guild.fetchWebhooks();
    
    // Rename it and move it to another channel.
    await webhook.edit({ name: 'Tide alerts', channel: alerts });
    
    // Delete it. What it posted stays.
    await webhook.delete('Moved to a bot command');
    # Create one in a channel, with an avatar and a reason for the audit log.
    with open("harbor-log.png", "rb") as image:
        webhook = await channel.create_webhook(
            name="Harbor log", avatar=image.read(), reason="Tide alerts for the harbor"
        )
    await webhook.send("High tide at 18:42.")
    
    # The channel's webhooks, or all of the server's.
    in_channel = await channel.webhooks()
    in_server = await channel.guild.webhooks()
    
    # Rename it and move it to another channel.
    webhook = await webhook.edit(name="Tide alerts", channel=alerts)
    
    # Delete it. What it posted stays.
    await webhook.delete(reason="Moved to a bot command")

    discord.js’s webhook.edit() without a channel and webhook.delete() use the webhook’s own token when the library has it, which it does for webhooks it created or fetched: see With the webhook’s token. discord.py builds Webhook.url with Discord’s domain; Klang’s address is in the object’s url.

    Create a webhook

    HTTP
    POST /channels/{channel.id}/webhooks
    Field Type Notes
    name string Required. 1 to 80 characters, without “Klang”, and not everyone or here.
    avatar image data or null data:image/png;base64,…: a PNG, JPEG, WebP or GIF image, up to 1 MB, not animated. Klang reads the file itself: what the data URI says doesn’t count.

    Answers 200 with the webhook. The request body can be up to 2 MB.

    JSON
    {
      "id": "1575719344436019200",
      "type": 1,
      "guild_id": "1575719326438260736",
      "channel_id": "1575719326580867072",
      "name": "Harbor log",
      "avatar": null,
      "application_id": null,
      "user": {
        "id": "1575719327113543680",
        "username": "lighthouse_8739",
        "discriminator": "0",
        "global_name": "Lighthouse",
        "avatar": null,
        "banner": null,
        "accent_color": null,
        "public_flags": 0,
        "bot": true
      },
      "token": "KS3s8JItkdnEvVhUFUxBwiqyoejPE4sDaN4iz1iCa3i1hv2BFDh6JzTSwTqKkxTh",
      "url": "https://api.klang.gg/api/webhooks/1575719344436019200/KS3s8JItkdnEvVhUFUxBwiqyoejPE4sDaN4iz1iCa3i1hv2BFDh6JzTSwTqKkxTh"
    }
    • Needs: MANAGE_WEBHOOKS in the channel.
    • Rate limit: webhook-manage.
    Status Code When
    400 30007 The channel has 15 webhooks, or the server 50.
    400 50035 A name or avatar that doesn’t fit, or a channel that isn’t a text channel (voice, category or thread).
    403 50013 Without MANAGE_WEBHOOKS in the channel.
    404 10003 A channel the bot can’t see.

    A name Klang doesn’t allow:

    JSON
    {
      "code": 50035,
      "message": "The request body isn’t valid.",
      "errors": {
        "name": {
          "_errors": [{ "code": "INVALID", "message": "That name can’t be used for a webhook." }]
        }
      }
    }

    List webhooks

    HTTP
    GET /channels/{channel.id}/webhooks
    GET /guilds/{guild.id}/webhooks

    A channel’s webhooks, or a server’s, as a list of webhook objects like the one above. Klang has no app webhooks, so these are all the webhooks there are.

    • Needs: MANAGE_WEBHOOKS in the channel. For a server’s, MANAGE_WEBHOOKS in the server, and the list has only the webhooks of the channels where the bot has it: an overwrite can take it away in one channel.
    • Rate limit: read-webhooks.
    Status Code When
    403 50013 Without MANAGE_WEBHOOKS where needed.
    404 10003 A channel the bot can’t see.
    404 10004 A server the bot isn’t in.

    Get a webhook

    HTTP
    GET /webhooks/{webhook.id}

    One webhook, with its token.

    • Needs: MANAGE_WEBHOOKS in its channel.
    • Rate limit: read-webhooks.

    A webhook that doesn’t exist, or that’s in a channel the bot can’t see, answers the same:

    JSON
    { "code": 10015, "message": "Unknown Webhook" }

    Change a webhook

    HTTP
    PATCH /webhooks/{webhook.id}
    Field Type Notes
    name string As when creating it.
    avatar image data or null As when creating it. null removes it.
    channel_id ID Moves it to another text channel of the same server, where the bot needs MANAGE_WEBHOOKS too.

    What you don’t send stays as it was, and an empty body answers the webhook as it is. Answers 200 with the webhook. Moving one sends WEBHOOKS_UPDATE for both channels.

    • Needs: MANAGE_WEBHOOKS in its channel.
    • Rate limit: webhook-manage.
    Status Code When
    400 50035 A name or avatar that doesn’t fit, or a channel_id that isn’t a text channel.
    403 50013 Without MANAGE_WEBHOOKS in its channel or in the one it moves to.
    404 10003 A channel_id of another server, or one the bot can’t see.
    404 10015 A webhook that doesn’t exist or is in a channel the bot can’t see.

    Delete a webhook

    HTTP
    DELETE /webhooks/{webhook.id}

    Answers 204. Its URL stops working at once; the messages it posted stay.

    • Needs: MANAGE_WEBHOOKS in its channel.
    • Rate limit: webhook-manage.

    With the webhook’s token

    HTTP
    GET /webhooks/{webhook.id}/{webhook.token}
    PATCH /webhooks/{webhook.id}/{webhook.token}
    DELETE /webhooks/{webhook.id}/{webhook.token}

    The webhook’s URL is enough to read it, change its name and avatar (channel_id is ignored here), or delete it, as in Discord: these routes need no bot token. Their answers have no user, and GET leaves out the token and the url too. PATCH answers 200 with the webhook, and DELETE answers 204:

    JSON
    {
      "id": "1575719344436019200",
      "type": 1,
      "guild_id": "1575719326438260736",
      "channel_id": "1575719326438260738",
      "name": "Harbor log",
      "avatar": null,
      "application_id": null,
      "token": "KS3s8JItkdnEvVhUFUxBwiqyoejPE4sDaN4iz1iCa3i1hv2BFDh6JzTSwTqKkxTh",
      "url": "https://api.klang.gg/api/webhooks/1575719344436019200/KS3s8JItkdnEvVhUFUxBwiqyoejPE4sDaN4iz1iCa3i1hv2BFDh6JzTSwTqKkxTh"
    }
    • The audit log records these changes without an author, with the reason in X-Audit-Log-Reason if there is one.
    • Errors use Discord’s codes, as in the rest of the API: a wrong ID or token answers 404 with 10015, like posting with it, and counts against the IP address’s limit of URLs that fail. A name or avatar that isn’t valid answers 400 with 50035.
    • Rate limit: webhook-manage, per webhook.

    The same routes answer under /api/v9, with the same codes, and under https://api.klang.gg/api/webhooks/…, the path of the URL people paste, which answers with Klang’s own codes (see Execute a webhook).

    Events

    • WEBHOOKS_UPDATE, with GUILD_WEBHOOKS: a webhook of that channel was created, changed or deleted, by a bot or by a person. It says where, not what: list the channel’s webhooks to see them.
    • GUILD_AUDIT_LOG_ENTRY_CREATE, with GUILD_MODERATION and View audit log: the entry, with action 50 (created), 51 (changed) or 52 (deleted).