Introduction to snoowrap
Learn how to use the JavaScript wrapper for the Reddit API.
We'll cover the following
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 |
| string | required | A |
| string | optional | A refresh token is a string of characters that can be used to obtain an authenticated instance of |
| string | optional | This is a string of random characters used to authenticate a user. |
| 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.
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:
Lines 3–9: We initialize
reddit
, in which we declare thesnoowrap
and its parameters are explained in the Parameters for Making a Snoowrap Requester lesson in the AppendixLine 11: We call the
getPreferences()
method.
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.