TwitchRB
TwitchRB is the easiest and most complete Ruby library for the Twitch Helix API.
Installation
Add this line to your application's Gemfile:
gem "twitchrb"
Usage
Set Client Details
Firstly you'll need to set a Client ID and an Access Token.
An access token is required because the Helix API requires authentication.
@client = Twitch::Client.new(client_id: "abc123", access_token: "xyz123")
Resources
The gem maps as closely as we can to the Twitch API so you can easily convert API examples to gem code.
Responses are created as objects like Twitch::Channel
. Having types like Twitch::User
is handy for understanding what
type of object you're working with. They're built using OpenStruct so you can easily access data in a Ruby-ish way.
Some of the endpoints return pages of results. The result object will have a data
key to access the results, as well as metadata like cursor
for retrieving the next and previous pages. This can be used by using before
and after
parameters, on API endpoints that support it.
An example of using collections, including pagination:
results = @client.clips.list(broadcaster_id: 123)
results.total
results.data
results.each do |result|
puts result.id
end
results.first
results.last
results.cursor
@client.clips.list(broadcaster_id: 123, after: results.cursor)
OAuth
This library includes the ability to create, refresh and revoke OAuth tokens.
@oauth = Twitch::OAuth.new(client_id: "", client_secret: "")
@oauth.create(grant_type: "", scope: "")
@oauth.refresh(refresh_token: "")
@oauth.device(scopes: "bits:read channel:read:subscriptions")
@oauth.validate(token: "")
@oauth.revoke(token: "")
Users
@client.users.retrieve(id: 141981764)
@client.users.retrieve(ids: [141981764, 72938118])
@client.users.retrieve(username: "twitchdev")
@client.users.retrieve(usernames: ["twitchdev", "deanpcmad"])
@client.users.update(description: "New Description")
@client.users.blocks(broadcaster_id: 141981764)
@client.users.block_user(target_user_id: 141981764)
@client.users.unblock_user(target_user_id: 141981764)
@client.users.get_color(user_id: 123)
@client.users.get_color(user_ids: "123,321")
@client.users.update_color(user_id: 123, color: "blue")
@client.users.emotes(user_id: 123)
@client.users.emotes(user_id: 123, broadcaster_id: 321)
@client.users.emotes(user_id: 123, after: "abc123")
Channels
@client.channels.retrieve(id: 141981764)
@client.channels.followed user_id: 123123
@client.channels.followers broadcaster_id: 123123
@client.channels.follows_count(broadcaster_id: 141981764)
@client.channels.subscribers_count(broadcaster_id: 141981764)
attributes = {title: "My new title"}
@client.channels.update(broadcaster_id: 141981764, attributes)
@client.channels.editors(broadcaster_id: 141981764)
Videos
@client.videos.list(user_id: 12345)
@client.videos.list(game_id: 12345)
@client.videos.retrieve(id: 12345)
Clips
@client.clips.list(user_id: 12345)
@client.clips.list(game_id: 12345)
@client.clips.retrieve(id: "AwkwardHelplessSalamanderSwiftRage")
@client.clips.create(broadcaster_id: 1234)
Emotes
@client.emotes.global
@client.emotes.channel(broadcaster_id: 141981764)
@client.emotes.sets(emote_set_id: 301590448)
Badges
@client.badges.global
@client.badges.channel(broadcaster_id: 141981764)
Games
@client.games.retrieve(id: 514974)
@client.games.retrieve(ids: [66402, 514974])
@client.games.retrieve(name: "Battlefield 4")
@client.games.retrieve(names: ["Battlefield 4", "Battlefield 2042"])
EventSub Subscriptions
These require an application OAuth access token.
@client.eventsub_subscriptions.list
@client.eventsub_subscriptions.list(status: "enabled")
@client.eventsub_subscriptions.list(type: "channel.follow")
@client.eventsub_subscriptions.create(
type: "channel.follow",
version: 1,
condition: {broadcaster_user_id: 123},
transport: {method: "webhook", callback: "webhook_url", secret: "secret"}
)
@client.eventsub_subscriptions.delete(id: "abc12-abc12-abc12")
Banned Events
@client.banned_events.list(broadcaster_id: 123)
Banned Users
@client.banned_users.list(broadcaster_id: 123)
@client.banned_users.create broadcaster_id: 123, moderator_id: 321, user_id: 112233, reason: "testing", duration: 60
@client.banned_users.delete broadcaster_id: 123, moderator_id: 321, user_id: 112233
Send Chat Announcement
@client.announcements.create broadcaster_id: 123, moderator_id: 123, message: "test message", color: "purple"
Create a Shoutout
@client.shoutouts.create from: 123, to: 321, moderator_id: 123
Moderators
@client.moderators.channels user_id: 123
@client.moderators.list broadcaster_id: 123
@client.moderators.create broadcaster_id: 123, user_id: 321
@client.moderators.delete broadcaster_id: 123, user_id: 321
VIPs
@client.vips.list broadcaster_id: 123
@client.vips.create broadcaster_id: 123, user_id: 321
@client.vips.delete broadcaster_id: 123, user_id: 321
Raids
@client.raids.create from_broadcaster_id: 123, to_broadcaster_id: 321
@client.raids.delete broadcaster_id: 123
Chat Messages
@client.chat_messages.create broadcaster_id: 123, sender_id: 321, message: "A test message", reply_to: "aabbcc"
@client.chat_messages.delete broadcaster_id: 123, moderator_id: 123, message_id: "abc123-abc123"
Whispers
@client.whispers.create from_user_id: 123, to_user_id: 321, message: "this is a test"
AutoMod
@client.automod.check_status_multiple broadcaster_id: 123, id: "abc123", text: "Is this message allowed?"
messages = [{msg_id: "abc1", msg_text: "is this allowed?"}, {msg_id: "abc2", msg_text: "What about this?"}]
@client.automod.check_status_multiple broadcaster_id: 123, messages: messages
@client.automod.settings broadcaster_id: 123, moderator_id: 321
@client.automod.update_settings broadcaster_id: 123, moderator_id: 321, swearing: 1
Creator Goals
@client.goals.list broadcaster_id: 123
Blocked Terms
@client.blocked_terms.list broadcaster_id: 123, moderator_id: 321
@client.blocked_terms.create broadcaster_id: 123, moderator_id: 321, text: "term to block"
@client.blocked_terms.delete broadcaster_id: 123, moderator_id: 321, id: "abc12-12abc"
Charity Campaigns
@client.charity_campaigns.list broadcaster_id: 123
Chatters
@client.chatters.list broadcaster_id: 123, moderator_id: 123
Channel Points Custom Rewards
@client.custom_rewards.list broadcaster_id: 123
@client.custom_rewards.create broadcaster_id: 123, title: "New Reward", cost: 1000
@client.custom_rewards.update broadcaster_id: 123, reward_id: 321, title: "Updated Reward"
@client.custom_rewards.delete broadcaster_id: 123, reward_id: 321
Channel Points Custom Reward Redemptions
@client.custom_reward_redemptions.list broadcaster_id: 123, reward_id: 321, status: "UNFULFILLED"
@client.custom_reward_redemptions.update broadcaster_id: 123, reward_id: 321, redemption_id: 123, status: "FULFILLED"
Unban Requests
@client.unban_requests.list broadcaster_id: 123, moderator_id: 123, status: "pending"
@client.unban_requests.resolve broadcaster_id: 123, moderator_id: 123, id: "abc123", status: "approved"
Warnings
@client.warnings.create broadcaster_id: 123, moderator_id: 123, user_id: 321, reason: "dont do that"
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/deanpcmad/twitchrb.
License
The gem is available as open source under the terms of the MIT License.