Using Reddit API for Ruby

Blainelove
4 min readApr 21, 2021

This is a quick guide to getting started with the Reddit API on Ruby. I’ve broken down the steps I took to get up and running with Reddit’s API, including my suggestion for the easiest Ruby Gem to use (after trying many), and the step by step process of how to accomplish simple tasks with the API. I’ve also included helpful links and docs at the end of the post.

The first step is to register for API usage on Reddit’s site. This can be done by logging into Reddit, going to Reddit’s app page, then click “are you a developer? create an app…”. You must input specific details about the app where you intend to use the API before they will grant you access. I selected script as I just wanted to test out the API, but it is important to put the correct type for your app. For my bare minimum personal use script, I put my name, selected script, and entered http://localhost:8080/ as the redirect uri. After clicking “create app” you will immediately be given your client ID and secret. These keys will be necessary to use the API.

It is necessary to register your app prior to using the API otherwise you will not receive a client secret key. This key is how Reddit will identify you as a developer and allow your app access to the API. This key is confidential and should be treated like a password. If publishing code to GitHub it is best practice to put all API keys in a separate file. You can use git ignore so this file containing your confidential keys does not get published to GitHub.

Next, let’s talk about authentication. Reddit uses an authentication called OAuth2 to authenticate users across their API. Similarly to how one would log into their Reddit account online or on the mobile app, OAuth is the process which grants the API access to perform actions on behalf of a Reddit user. Authentication occurs by acquiring an access token which is then used when making requests through the API. Detailed instructions on setup and authentication, including how to retrieve an authentication token, can be found here.

As you can see, there is a lot of setup work involved to use the Reddit API. The above steps are common for many API services as the creators need to verify that a developer’s usage aligns with their API rules, and that all parties can be authenticated.

Luckily, in Ruby there are already many Gems created that help make the Reddit API quick and easy to get started with. After trying several different Gems (and having many dependency issues), I stumbled upon Redd. This Gem is an API wrapper around the Reddit API that has full OAuth support. It also has capabilities to access most all of Reddit’s API endpoints.

Installation

$ gem install redd

Connecting and creating a session

require 'redd'session = Redd.it(
user_agent: 'personal use script',
client_id: CLIENT_ID,
secret: SECRET,
username: USERNAME,
password: PASSWORD
)

To successfully connect to the Reddit API, you need a client id, secret, username, and password. The above are all variables which I’ve stored in a separate file. Breaking this down:

  • user_agent is a way to tell the server who you are. There are standards to follow depending on the app you’re creating, but for my purposes I just gave it a generic title.
  • client_id and secret are given to you when you register your app on Reddit.
  • username and password is the account credentials of the Reddit account you’re using the API with. I used my own personal account login to test out the API, but if I were to use in a real app I would want to create a separate account specifically for API usage.

Example Usage

Now that we have successfully connected and created a session, we can start requesting some data through Reddit’s available endpoints. Building from session above we can do things like the following:

Get the current top post of r/ruby

r_ruby = session.subreddit('ruby')
post = r_ruby.hot.first
puts post.title

Get the number of subscribers of r/ruby

r_ruby = session.subreddit('ruby')
subs = r_ruby.subscribers
puts subs

Output

Adding some printing statements and running the script gives the following:

You can see how easy it is to get up and running with Reddit’s API using the Gem Redd. There are many more endpoints available and endless possibilities. From here it’s possible to start exploring the rest of the Reddit API and building out your own Reddit bot or gathering data from Reddit.

Helpful Links for Redd

Redd Github

Redd Documentation

Redd Official Subreddit and Quick Guide

--

--