How to call the reddit REST API using Node.js – Part I

One common programming task is to call a REST API. So it’s a good thing to know how to do this! In this demo, I’ll write a short Node.js program which “consumes” the reddit REST API. I’ll be doing this using a Linux environment (Ubuntu 16.04).

First, I had to sign up for a developer account at reddit. I was already a member of reddit, so I just used my own information to create the “app”. The reddit API access page is the place to go for signing up. There’s a link at the bottom which has the text “Read the full API terms and sign up for usage” – click that, and follow the instructions.

Next, go to your “Developed applications” page. Your “id” is found at the top left, a random string under the words “personal use script”. Click the “edit” link, below, and you’ll find your “secret”, another long string which should not be shared!

Using the reddit API requires authentication. Reddit needs to be able to know who is using their API, and to place limits on it, in case of abuse. So I need to figure out how to do this first.

Now that I’ve got an “id” and a “secret”, I can follow the instructions for authentication using OAuth2.

Let’s test the authentication process using curl, a command line tool which lets you “get” a web page from the command line.

curl -X POST -d 'grant_type=password&username=my_user_name&password=MyExcellentPassword' --user 'my_reddit_id:my_reddit_secret' https://www.reddit.com/api/v1/access_token

Copying and pasting that won’t work for you! Make sure to replace the strings my_user_name and MyExcellentPassword shown above with your own reddit sign-in information, and use your own id and secret as referenced above, when using curl.

After I did this, I saw the following output:

{"message": "Too Many Requests", "error": 429}

Oops. It turns out you need to set a user agent header to avoid this. Curl has a specific option, --user-agent, to let you do this. I just used a fake user-agent for my demo purposes: “my test bot 1.0”.

curl -X POST -d 'grant_type=password&username=my_user_name&password=MyExcellentPassword' --user 'my_reddit_id:my_reddit_secret' --user-agent 'my test bot 1.0' https://www.reddit.com/api/v1/access_token

Success! My output from curl looks like this:

{"access_token": "261295382351-FBXDPTpUam35NR_UTJSXnjl5Pmd", "token_type": "bearer", "expires_in": 3600, "scope": "*"}

This token will expire shortly… but that doesn’t matter. Now that I’ve got the authentication part working, I can work on the application, itself. I’ll cover that in my next blog post.

If you found this interesting, click the subscribe button below! I write a new post about once a week.

4 thoughts on “How to call the reddit REST API using Node.js – Part I

  1. Pingback: How to call the reddit REST API using Node.js – Part II | Full Stack Oasis

  2. Pingback: How to call the reddit REST API using Node.js – Part III | Full Stack Oasis

  3. Pingback: How to call the reddit REST API using Node.js – Part IV | Full Stack Oasis

Leave a Reply

Your email address will not be published. Required fields are marked *