Testing the Discord API with a bot token and webhooks

Discord has two ways in for automation. A channel webhook is a URL that posts into one channel, and it's all most deploy and alert notifications need. The bot API does everything else: read messages, edit them, manage channels. Both are easy to call and both rate-limit aggressively, so a good test collection covers the limits too.

Step 1: Environment

VariableValue
discordApihttps://discord.com/api/v10
discordBotTokenthe bot token from the Developer Portal, marked as secret
discordWebhookUrla channel webhook URL, marked as secret
channelIda test channel's id

A webhook URL is a credential: anyone with it can post as that webhook. Keep it in a secret variable like a token.

Step 2: Post through a webhook

HTTP
POST {{discordWebhookUrl}}?wait=true
Content-Type: application/json

{
  "username": "Deploy bot",
  "content": "orders-api v1.4.2 is live on staging",
  "embeds": [{"title": "Deploy finished", "color": 5763719, "fields": [{"name": "Version", "value": "v1.4.2", "inline": true}]}],
  "allowed_mentions": {"parse": []}
}
JavaScript
pm.test('message created', () => {
  pm.expect(pm.response.code).to.equal(200);
  const m = pm.response.json();
  pm.expect(m.embeds[0].title).to.equal('Deploy finished');
});

Without ?wait=true, Discord answers 204 with no body, and you can't tell what it stored. allowed_mentions with an empty parse list means an @everyone in a commit message won't ping the whole server.

Step 3: The bot API

Use API key auth with header name Authorization and value Bot {{discordBotToken}}.

HTTP
GET {{discordApi}}/users/@me
HTTP
POST {{discordApi}}/channels/{{channelId}}/messages
Content-Type: application/json

{"content": "Test from the API collection", "allowed_mentions": {"parse": []}}
JavaScript
const msg = pm.response.json();
pm.test('posted as the bot', () => pm.expect(msg.author.bot).to.equal(true));
pm.environment.set('messageId', msg.id);

The bot must be in the server and allowed to send messages in that channel; otherwise Discord answers 403 with "code": 50013 (missing permissions) or 50001 (missing access).

Step 4: Edit and delete

HTTP
PATCH {{discordApi}}/channels/{{channelId}}/messages/{{messageId}}
Content-Type: application/json

{"content": "Edited by the API collection"}
HTTP
DELETE {{discordApi}}/channels/{{channelId}}/messages/{{messageId}}

DELETE answers 204. Run it as a cleanup step.

Step 5: Rate limits

Discord limits each route separately and tells you in headers: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset-After, X-RateLimit-Bucket. Over the limit:

JSON
{"message": "You are being rate limited.", "retry_after": 0.64, "global": false}
JavaScript
if (pm.response.code === 429) {
  const body = pm.response.json();
  console.log(`rate limited for ${body.retry_after}s, global: ${body.global}`);
}
pm.test('headroom left', () => {
  const left = Number(pm.response.headers.get('X-RateLimit-Remaining'));
  pm.expect(left).to.be.at.least(0);
});

Discord temporarily bans IPs that keep hitting limits, so in a run that posts several messages, add a delay rather than relying on retries.