Nightbot API Example

Using the Nightbot API

I wanted to provide people an example on how to get stared with the Nightbot API to do more advanced things then just using the built in functions.


Step 1) Create an "App" on the Nightbot dashboard

To make calls to the Nightbot API, you will need to obtain a token from the API. To obtain a token, you will need a Nightbot app Client ID and Client Secret.

To get a Client ID and Client Secret, you need to create an "App" on the Nightbot dashboard. Keep these SECRET! An Id and Secret are essentially a username and password.


Step 2) Obtain a Token from the Nightbot API

To obtain a token from the API, make a REST API call to the token API endpoint including the Client ID and Client Secret. You will also want to include the scope. The scope determines what types of API calls the token is allowed to do.
For security, its very important you store the Client ID and Client Secret in a non-web accessable location on your server. In this example we are doing a PHP CURL POST:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => https://api.nightbot.tv/oauth2/token,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "grant_type=client_credentials&client_id={YourClientIdHere}&client_secret={YourClientSecretHere}&scope=channel channel_send commands commands_default regulars subscribers timers"
));
$nightbotApi = curl_exec($curl);
curl_close($curl);


The result of a successful API call to Nightbots token endpoint will contain:
access_token
token_type
expires_in
refresh_token
scope

Store the access_token value somewhere secure. You will need the token to make calls to other Nightbot endpoints.

Note the token expires after a few weeks, then you will need to get a new token. You should create an expiration date by adding the current date(epoch seconds) to the expires_in(epoch seconds) date and store it along side the access token.
Each time you make an API call you should first check that your access token has not expired.


Step 3) Make an API call to Nightbot

In the following example, we will ask Nightbot make a comment in the Twitch channel via a REST API call to the channel endpoint, using a PHP CURL POST that includes the token, and the message to the channel:
$message = "Hello from Nightbot!";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => https://api.nightbot.tv/1/channel/send,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "access_token={YourAccessTokenHere}&message=$message"
));
$nightbotApi = curl_exec($curl);
curl_close($curl);


Step 4) THE REAL MAGIC!
What can you do now that you can make API calls to Nightbot? The real magic happens by trigging the API calls from Twitch chat.

For example, in the Twitch channel, add a new command to Nightbot and point it to the webpage you created above. Substute the URL for your server. In this example, the command name will be "!sso". "customapi" is the syntax that lets you specify a URL you want Nightbot to call and include extra data.
!addcom !sso $(customapi https://l0r3.net/bots/nightbot/nightbot_example.php/?&query=$(querystring))

On the page you created in the above steps, before your API call, add some code to hold the querystring. For this PHP example you can use:
$query = $_GET['query'];

In the code you created in step 3, replace the "$message" line with:
$message = "Everyone please go checkout https://www.twitch.tv/$query";

Now that you can trigger Nightbot API calls from Twitch chat, theres no limit to the projects you can do!

You can find the Nightbot endpoints, scope definitions, and more in the Nightbot API Documentation