Skip to content

Verify X (Twitter) webhooks

Verify that a webhook really came from X (Twitter) before acting on it.

Identifier 'twitter', 'x'
Import import { verifyTwitter } from 'verihook/twitter'
Headers x-twitter-webhooks-signature
Signature HMAC-SHA256 of the raw body, keyed with the consumer secret, sent as sha256=<base64>.
Replay window None (the provider sends no timestamp)
result.eventType the payload’s *_events key, e.g. tweet_create_events

Your app’s API Key Secret (consumer secret): in the X Developer Portal, open the app’s Keys and tokens.

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

import { verifyTwitter } from 'verihook/twitter';
export async function POST(request: Request) {
const result = await verifyTwitter(request, process.env.TWITTER_CONSUMER_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('twitter', request, secret) from verihook does the same. Read why the raw body matters if verification fails behind a body parser.

app/api/webhooks/twitter/route.ts
import { createWebhookHandler } from 'verihook/next';
export const POST = createWebhookHandler('twitter', process.env.TWITTER_CONSUMER_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.

X sends a CRC check as a GET request with a crc_token, when you register the webhook and then periodically. Answer it with verifyTwitterCrc:

import { verifyTwitterCrc } from 'verihook/twitter';
// GET /webhooks/twitter
export async function GET(request: Request) {
const crcToken = new URL(request.url).searchParams.get('crc_token') ?? '';
return Response.json(await verifyTwitterCrc(crcToken, process.env.TWITTER_CONSUMER_SECRET!));
}
  • verifyX and verifyXCrc are aliases of the Twitter functions.

signWebhook from verihook/testing builds a correctly signed X (Twitter) request for your tests:

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

From the command line, npx verihook simulate twitter --url http://localhost:3000/webhooks/twitter 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.

X (Twitter)’s webhook documentation