Introduction to snoowrap

Learn how to use the JavaScript wrapper for the Reddit API.

In this lesson, we'll look at how to set up snoowrap and make our first API call with it.

Set up Snoowrap

Snoowrap is the JavaScript Reddit API Wrapper that makes using the Reddit API much more manageable. We'll use the snoowrap() requester to fetch content from Reddit. We'll use the refresh_token instead of the access_token to create a request to Reddit.

Request parameters

Here are some important parameters we'll use to call the method:

Parameter

Type

Category

Description

user_agent

string

required

A user_agent is used to identify the source of a network request. We need a unique and descriptive user_agent.

refresh_token

string

optional

A refresh token is a string of characters that can be used to obtain an authenticated instance of Reddit.

client_id

string

optional

This is a string of random characters used to authenticate a user.

secret_id

string

optional

This is a string of random characters used to authenticate a user.

We'll call the getPreferences()method to get our current User preferences for Reddit. You can call this method without any parameters.

Click "Run" to get your user preferences using Snoowrap.

Press + to interact
import snoowrap from "snoowrap";
const reddit = new snoowrap({
client_id: "{{CLIENT_ID}}",
client_secret: "{{CLIENT_SECRET}}",
refresh_token: "{{REFRESH_TOKEN}}",
username: "{{USERNAME}}",
user_agent: "testscript by u/{{USERNAME}}",
});
async function userPreferences() {
try {
const response = await reddit.getPreferences();
printResponse(response);
} catch (error) {
printError(error);
}
}
userPreferences();

Let's take a look at the code given in the widget above:

Response fields

The expected output of this snoowrap function is a JSON that contains user preferences. Some important response properties are explained in the Response fields for Reddit API lesson in the Appendix.