Use snoowrap to Interact with Posts and Comments
Learn how to get and make posts and comments using JavaScript Reddit API Wrapper (snoowrap).
We'll cover the following
In this lesson, we'll learn the two snoowrap methods: creating posts and commenting on those posts.
Make a post
We can make a post using the submitSelfpost()
method. We'll need to provide the title and the body to submit the post.
Here are some important parameters we'll use to call the method:
Request Parameters
Parameter | Type | Category | Description |
| string | required | The name of the subreddit used for the post |
| string | required | The title we want in our new post. |
| string | required | The body of the post. |
Press "Run" to submit a new post to a subreddit.
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 submitPost() {try {const subreddit = await reddit.submitSelfpost({subredditName: `{{SUBREDDIT_NAME}}`,title: "This is a selfpost",text: "Heyyy from SNOOWRAP!",});printResponse(subreddit.name);} catch (error) {printError(error);}}submitPost();
Let's take a look at the code given in the widget above:
Lines 3–9: We initialize the
snoowrap
requester. The parameters are explained in the Parameters for Making a Snoowrap Requester lesson in the Appendix.Line 10: We define values of all the required parameters where we used the variables
title
andtext
to make a post title and text. The response is the ID of the post created. We'll approve this post in the coming lessons. We can find the post in one of the moderation inboxes.
Response fields
This request returns a JSON response, which contains the post ID.
Make a comment
We can comment on a post using the reply()
method. We'll need to provide the body of the comment.
The following is an important parameter we'll use to call the method:
Request Parameters
Parameter | Type | Category | Description |
| string | required | The text of the reply, |
Press the "Run" to comment on a post.
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 makeComment() {try {const response = await reddit.getSubmission(`{{POST_ID}}`).reply("This is my comment. Thanks.");printResponse(response.id);} catch (error) {printError(error);}}makeComment();
Let's understand the code given in the widget above:
Lines 3–9: We initialize the
reddit
in which we initialize thesnoowrap
requester and pass the values in the parameters. The parameters are explained in the Parameters for Making a Snoowrap Requester lesson in the Appendix.Line 12–14: We make an instance of a submission using its ID. Then we use the
reply()
method to post a comment with the text from thebody
parameter. The expected response should be an ID for the comment we made.
Response fields
This request returns a JSON object that contains the details about the comment made on a post. Some important properties are discussed in the Response fields for Reddit API lesson in the Appendix.
Read posts
There are multiple methods in the snoowrap module used to read posts from subreddits. Here is the list of methods that we can use to read posts.
getNew()
getHot()
getTop()
getControversial()
The following is an important parameter we'll use to call the method:
Request Parameters
Parameter | Type | Category | Description |
| object | optional | A parameter that is used to provide options for the resultant listing. |
Click "Run" to read a new post from a subreddit on Reddit.
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 readPost() {try {const response = await reddit.getSubreddit("{{SUBREDDIT_NAME}}").getNew({ limit: 5 });// const response = await reddit.getSubreddit('{{SUBREDDIT_NAME}}').getHot({limit:5})// const response = await reddit.getSubreddit('{{SUBREDDIT_NAME}}').getTop({limit:5})// const response = await reddit.getSubreddit('{{SUBREDDIT_NAME}}').getControversial({limit:5})printResponse(response);} catch (error) {printError(error);}}readPost();
Let's take a look at the code given in the widget above:
Lines 3–9: We initialize the
snoowrap
requester, and its parameters are explained in the Parameters for Making a Snoowrap Requester lesson in the Appendix
Response fields
The request will return an array of JSON objects. Each object contains details about a particular post. Some important properties of the response are explained in the Response fields for Reddit API lesson in the Appendix.
Read comments from a post
We read comments on submission using the getSubmission()
method. After the getSubmission()
method, we use the expandReplies()
method to get all the comments on the post. After that, we access the comments attribute of a submission, and we'll get a comment object. We'll use an id
to print the individual comments.
Here are some important parameters we'll use to call the method:
Request Parameters
Parameter | Type | Category | Description |
| string | required | A string of characters used to identify a post |
| number | optional | A parameter that is used to provide a limit for the resultant listing. |
| number | optional | A parameter that is used to provide depth for the resultant listing. |
Click "Run" to read the comments of a post.
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 readComment() {try {await reddit.getSubmission("{{POST_ID}}").expandReplies().comments.then((result) => {result.forEach((comment) => {console.log(comment.id, ":", comment.body);});});} catch (error) {printError(error);}}readComment();
Let's look at the explanation for the code given above:
Lines 3–9: We initialize
reddit
and define thesnoowrap
requester in it. The parameters are explained in the Parameters for Making a Snoowrap Requester lesson in the AppendixLine 11: We created an async function,
readComment
, to fetch comments for a post using thePost_ID
of that particular post.
Response fields
The request will return an array of JSON objects. Each object contains details about each comment on a particular post. Some important properties of the response are explained in the Response fields for Reddit API lesson in the Appendix.