Authenticating with the Twitch IRC Server

Before your bot can join a channel, it needs to authenticate with the Twitch IRC server. To authenticate with the server, your bot sends the PASS and NICK messages. The order that you send the messages matters — you must send PASS before sending NICK; otherwise, the server replies with: NOTICE * :Improperly formatted auth

Registering your bot

To get an access token, you must first register your bot. For information about registering your bot, see Registering an app.

Getting an access token

After registering your bot and getting a client ID, you can generate a user access token. For information about user access tokens, see Authentication. There are a number of ways to get an access token, including:

If you just need a token for testing chat functionality, you can try this option:

The scopes that you specify when requesting the token depends on your bot’s functionality. If the bot just reads chat messages, it only needs the chat:read, but if it also sends messages, it’ll need the chat:edit scope, too. For a list of scopes that you can specify, see Chat scopes. If you plan to send messages that include Twitch chat commands, see Twitch chat commands for a list of scopes.

Whose user token to use?

The user name that Twitch uses in the chat room for your bot comes from the user access token. If you want Twitch to use your bot’s name in the Twitch chat room, you need to:

If your bot needs to send moderator chat commands, you’ll also need to get it added as a moderator in the channel.

Sending the PASS and NICK messages

Set the PASS message’s text string to the access token. The string’s format is oauth:<token> where token is the access token.

PASS oauth:yfvzjqb705z12hrhy1zkwa9xt7v662

Set the NICK message’s text string to your bot’s nickname. The nickname should be the lowercase login name of the Twitch account used to get your access token.

NICK <user>

Here’s what the snippet of code might look like if you used this websocket package for Node.js.

client.on('connect', function(connection) {
    console.log('WebSocket Client Connected');
    connection.sendUTF('CAP REQ');
    connection.sendUTF('PASS oauth:yfvzjqb705z12hrhy1zkwa9xt7v662');
    connection.sendUTF('NICK myusername');

The PASS/NICK reply

If the Twitch IRC server successfully authenticates your bot, it replies with the following numeric messages: 001 <user> :Welcome, GLHF! 002 <user> :Your host is 003 <user> :This server is rather new 004 <user> :- 375 <user> :- 372 <user> :You are in a maze of twisty passages. 376 <user> :>
@badge-info=;badges=;color=;display-name=<user>;emote-sets=0,300374282;user-id=12345678;user-type= GLOBALUSERSTATE

Note that the above reply was formatted for readability, but the message is actually a single string that contains multiple messages. Each message is delimited by CRLF (\r\n). For example: 001 <user> :Welcome, GLHF!\r\ 002 <user> :Your host is\r\ 003 <user> :This server is rather new\r\ 004 <user> :-\r\ 375 <user> :-\r\ 372 <user> :You are in a maze of twisty passages, all alike.\r\ 376 <user> :>\r\n@badge-info=;badges=;color=;display-name=<user>;emote-sets=0,300374282;user-id=12345678;user-type= GLOBALUSERSTATE\r\n

If the server fails to authenticate your bot, it replies with the following message and closes the connection. NOTICE * :Login authentication failed

Also, as stated above, if you don’t send the PASS and NICK messages in the correct order, the authentication fails and the server replies with: NOTICE * :Improperly formatted auth

Next steps

Join the chat room where your bot will send and receive messages. See Joining a chat room.