Skip to content

Verify Slack webhooks

Verify that a webhook really came from Slack before acting on it.

Identifier 'slack'
Import import { verifySlack } from 'verihook/slack'
Headers x-slack-signature
x-slack-request-timestamp
Signature HMAC-SHA256 of v0:<timestamp>:<raw body>, sent as v0=<hex>.
Replay window 300 seconds (options.tolerance)
result.eventType the body’s type (Events API), the slash command, or the interaction payload.type

The app’s Signing Secret: open your app at api.slack.com/apps, then Basic Information → App Credentials. It is not the bot token or the deprecated verification token.

Store it in an environment variable (SLACK_SIGNING_SECRET below) and never commit it.

import { verifySlack } from 'verihook/slack';
export async function POST(request: Request) {
const result = await verifySlack(request, process.env.SLACK_SIGNING_SECRET!);
if (!result.valid) {
console.warn(result.code, result.reason, result.hint);
return new Response('Invalid signature', { status: 401 });
}
console.log(result.eventType, result.event);
return Response.json({ received: true });
}

request can be a Fetch Request or { headers, body, url? } with the raw body. verifyWebhook('slack', request, secret) from verihook does the same. Read why the raw body matters if verification fails behind a body parser.

app/api/webhooks/slack/route.ts
import { createWebhookHandler } from 'verihook/next';
export const POST = createWebhookHandler('slack', process.env.SLACK_SIGNING_SECRET!, async (payload, result) => {
// Runs only for a valid signature
console.log(result.eventType, result.event);
});

Each adapter responds 401 to an invalid signature, 413 to a body over maxBodySize (2 MB by default) and 200 to a duplicate when you pass a dedupeStore.

  • Slash commands and interactivity are form posts (application/x-www-form-urlencoded). result.event holds the form fields; interactivity puts JSON in the payload field.
  • The Events API sends a url_verification request when you set the URL. After verification, respond with the challenge value from the body.

signWebhook from verihook/testing builds a correctly signed Slack request for your tests:

import { signWebhook } from 'verihook/testing';
import { verifySlack } from 'verihook/slack';
const hook = await signWebhook('slack', {
secret: 'slack_test_secret',
payload: { id: 'evt_test' },
});
const result = await verifySlack(
{ headers: hook.headers, body: hook.body, url: hook.url },
hook.secret,
);
// result.valid === true

From the command line, npx verihook simulate slack --url http://localhost:3000/webhooks/slack sends one to your local server.

See Testing your handlers for supertest and Fetch Request examples.

Failed results carry a code and often a hint with the likely cause. See Troubleshooting for each error code.

Slack’s webhook documentation