Automating Twitch Notifications with n8n Webhooks

Automating Twitch Notifications with n8n Webhooks

Have you ever wanted to automatically trigger actions whenever you go live on Twitch? With n8n and Twitch EventSub, you can capture Twitch webhooks and connect them to Twitter, Discord, or any other service. In this guide, I’ll show you how to set up a webhook from Twitch to n8n and use it to automate your workflow.

Prerequisites

  • A Twitch Developer account
  • An n8n instance with a valid HTTPS domain (e.g. https://n8n.hellbz.de)
  • Basic knowledge of webhooks

Create a Twitch App

Go to the Twitch Developer Console, register your application, and note down the Client ID and Client Secret.

Create an n8n Webhook

In n8n, create a new workflow with a Webhook Trigger node. Set it to POST and copy the Production URL as your Twitch callback.

Subscribe to Twitch EventSub

POST https://api.twitch.tv/helix/eventsub/subscriptions
Authorization: Bearer <YOUR_APP_TOKEN>
Client-Id: <YOUR_CLIENT_ID>
Content-Type: application/json

{
  "type": "stream.online",
  "version": "1",
  "condition": { "broadcaster_user_id": "<YOUR_TWITCH_USER_ID>" },
  "transport": {
    "method": "webhook",
    "callback": "https://n8n.hellbz.de/webhook/twitch",
    "secret": "<YOUR_SECRET>"
  }
}

Handle the Verification Challenge

Twitch sends a challenge string on subscription. You must respond with that string to verify the webhook. In n8n, add a Respond to Webhook node with:

{{$json["challenge"]}}

Verify Twitch Signatures (Recommended)

For security, validate Twitch’s HMAC signatures by computing sha256 = HMAC(secret, message_id + message_timestamp + raw_body).

// Verify Twitch EventSub HMAC signatures
const crypto = require('crypto');

// Read Twitch headers
const sigHeader = $json.headers['twitch-eventsub-message-signature'];
const msgId     = $json.headers['twitch-eventsub-message-id'];
const timestamp = $json.headers['twitch-eventsub-message-timestamp'];

// Raw body (if Webhook node has "Raw Body" enabled)
const raw = Buffer.from($binary.data.data, 'base64');

// Compute HMAC
const secret = $vars.twitchSecret; // store your secret securely
const hmac = crypto.createHmac('sha256', secret)
                   .update(msgId + timestamp)
                   .update(raw)
                   .digest('hex');

const expected = `sha256=${hmac}`;
if (sigHeader !== expected) {
  throw new Error('Invalid Twitch signature');
}

return [{ json: { verified: true, event: $json.body } }];

Connect to Twitter (or Other Actions)

  • Twitter: Post an “I’m live now!” tweet.
  • Discord: Send a message to your server.
  • Email: Notify your subscribers.

Add the desired node in n8n and map fields (e.g., stream title + link).

Final Workflow Outline

  1. Webhook Trigger (receives Twitch events)
  2. Respond to Webhook (echo challenge)
  3. Code (validate HMAC)
  4. Action (Twitter/Discord/etc.)

References

Source: https://harshil.dev/writings/automated-tweets-for-your-twitch-live-stream/

Leave a Reply