A public API for Twitch chat commands via StreamElements
This repo contains code for a REST API that is intended for use with the Twitch streaming platform by way of StreamElements.
When a viewer/user in your Twitch channel uses one of the included chat commands, a request is made to the corresponding command's endpoint to return the response.
The API contains responses for the following commands:
-
!strain - returns an autogenerated name of a fictional weed "strain" to the chaet
-
!urban (search term) - returns the searched term's results from the Urban Dictionary API
-
!askgpt (prompt) - returns a response to the given prompt from the OpenAI/ChatGPT API
To use the API's responses in your Twitch channel's chat, you'll need to create a custom command in StreamElements for each of the commands included in this repo.
Once done, you'll need to enter the string for each command in it's corresponding "response type" in the StreamElements custom chat command UI.
- For the !strain command, use:
${user} is on that ${urlfetch https://chat-command-api-f314fc32259e.herokuapp.com/strain} strain tonight!
- For the !urban command, use:
$(urlfetch https://chat-command-api-f314fc32259e.herokuapp.com/urban?term=$(queryescape ${1:}))
- For the !askgpt command, use:
$(urlfetch https://chat-command-api-f314fc32259e.herokuapp.com/askgpt?query=$(queryescape ${1:}))
To use this code to create and run your own Twitch chat command API for use with StreamElements, simply clone the repo, add in the necessary environment values (listed below) and deploy the code to a cloud provider (Heroku, etc) to host the chat command API for your channel during live-streams.
The following env values are used in this project
TWITCH_OAUTH_TOKENTWITCH_CHANNELSTWITCH_USERNAMEURBAN_DICTIONARY_API_KEYURBAN_DICTIONARY_API_HOSTOPENAI_API_KEY
The Twitch OAuth token is required in order to connect the script to Twitch's chat element, and the Twitch Username value is the account used when generating the token.
The Twitch Channels value is used to determine which channel(s) you'd like your API to respond to.
More information on creating a Twitch OAuth access token can be found here. When prompted, selected "bot chat token".
More information on creating and using an OpenAI developer key can be found here.
More information on creating and using an Urban Dictionary developer key can be found here.
Marcus McBride, 2025