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
| Variable | Value |
|---|---|
discordApi | https://discord.com/api/v10 |
discordBotToken | the bot token from the Developer Portal, marked as secret |
discordWebhookUrl | a channel webhook URL, marked as secret |
channelId | a 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
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": []}
}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}}.
GET {{discordApi}}/users/@mePOST {{discordApi}}/channels/{{channelId}}/messages
Content-Type: application/json
{"content": "Test from the API collection", "allowed_mentions": {"parse": []}}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
PATCH {{discordApi}}/channels/{{channelId}}/messages/{{messageId}}
Content-Type: application/json
{"content": "Edited by the API collection"}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:
{"message": "You are being rate limited.", "retry_after": 0.64, "global": false}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.