Twitch API

This gem simplifies the Twitch-API for ruby users.


With Rails:

#add to your Gemfile
gem 'twitch', '~> 0.1.0'

Just irb or pry:

$ gem install twitch

irb > require 'twitch'
irb > @twitch =

Changes in 0.1.0 from 0.0.x

Listed below are some changes introduced in version 0.1.0 from 0.0.x. Some of the changes break backward compatibility with previous versions.

- Replaced camelCase method names with snake_case.
- Removed 'get_' prefix from method names.
- Made 'your_' prefix optional. (e.g. user() and your_user() are equal)


Step 1: Get url for your application - (@scope is an array of permissions, like ["user_read", "channel_read", "user_follows_edit"])

@twitch ={
  client_id: @client_id,
  secret_key: @secret_key,
  redirect_uri: @redirect_uri,
  scope: @scope

Step 2: Authenticate and get access_token (this is done on your @redirect_url)

@twitch ={
  client_id: @client_id,
  secret_key: @secret_key,
  redirect_uri: @redirect_uri,
  scope: @scope

@data = @twitch.auth(params[:code])
session[:access_token] = @data[:body]["access_token"]

Step 3: You can now use user token

@twitch = access_token: session["access_token"]
@yourself = @twitch.your_user()


Calls will return a Hash with :body for the content of the call and a :response

@twitch.user "day9tv"





#does not require any access_token 
# @twitch =
@twitch.user "day9tv"

#requires access_token, use 
# @twitch = access_token: session["access_token"]


@twitch.teams() "eg"

Channels "lethalfrag"
@twitch.channel_panels "lethalfrag"

#Requires access_token

# Requires access_token (and special scope for channel editing)
#   edit_channel(channelname, status, game)
#   arguments:
#    status (string)
#    game (string)

@twitch.edit_channel "ChannelName", "Ranked Solo Queue", "League of Legends"

#Requires access_token (and special scope for channel commercials)
#   run_commercial(channel, length = 30)
#   arguments:
#    channel (string)
#    length (int)
#  *this is untested*

@twitch.run_commercial "lethalfrag", 30


@twitch.following 'esl_csgo'

@twitch.followed 'esl_csgo'

Streams "lethalfrag"

# getStreams(options = {})
# see Twitch-API for options


# getFeaturedStreams(options = {})
# see Twitch-API for options


#Requires access_token


# search_streams(options = {})
# see Twitch-API for options


# search_games(options = {})
# see Twitch-API for options



# get_channel_videos(channel, options = {})
# see Twitch-API for options

@twitch.channel_videos "lethalfrag"

# get_video(video_id) 12345


To allow the gem to use different HTTP libraries, you can define an Adapter:

require 'open-uri' 

module Twitch
  module Adapters
    class OpenURIAdapter < BaseAdapter
      def self.request(method, url, options={})
        if (method == :get)
          ret = {}

          open(url) do |io|
            ret[:body] = JSON.parse(
            ret[:response] = io.status.first.to_i

    end # class OpenURIAdapter
  end # module Adapters
end # module Twitch

and then pass it into the Twitch class:

@twitch = adapter: Twitch::Adapters::OpenURIAdapter

# or

@twitch =
@twitch.adapter = Twitch::Adapters::OpenURIAdapter

Adapters must be defined inside the Twitch::Adapters module, otherwise they will be considered invalid. Any invalid adapter passed to the library will revert to the default adapter.

The default adapter is Twitch::Adapters::HTTPartyAdapter which uses the HTTParty library.

Feel free to contribute or add functionality!