Add Comments and Vote on a Post
Learn how to make comments and votes on a post using the Reddit API.
We'll cover the following
In this lesson, we’ll explore two endpoints—comment and vote.
Comment on a post
A user mainly interacts with a post through comments. We can automate this process using the API and comment on posts that meet specific criteria. However, it’s not suggested to write scripts that make unnecessary comments on posts.
To comment, we use the following endpoint:
https://oauth.reddit.com/api/comment
Request parameters
The following are some important parameters that we’ll use to call the endpoint:
Parameter | Type | Category | Description |
| string | required | This is the |
| string | required | This is the body of the comment that will be posted. |
| string | required | This is set to the |
Click “Run” to comment on our earlier post.
// Defining import libraries hereimport fetch from "node-fetch";const endpointUrl = new URL("https://oauth.reddit.com/api/comment");const bodyParameters = new URLSearchParams({text: "Sample comment",thing_id: "t3_{{POST_ID}}",api_type: "json",});const headerParameters = {UserAgent: "testscript by u/{{USERNAME}}",authorization: `Bearer {{ACCESS_TOKEN}}`,contentType: "application/json",};const options = {method: "POST",headers: headerParameters,body: bodyParameters,};// Function to make API callasync function commentOnPost() {try {const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API callcommentOnPost();
Let’s understand how this program works.
Line 4: We specify the endpoint URL for the endpoint.
Lines 6–16: We add the parameters for commenting.
Lines 25–33: We created an
async
function with the name,commentOnPost
, in which we use the defined parameters to comment on a post.
Response fields
This endpoint returns a JSON response containing the data about the comment we made. You can read more about these important properties in the Response fields for Reddit API lesson in the Appendix.
Vote on a post
According to the rules of the Reddit API, developers must not automate a script that decides when to cast a vote on its own. However, an API can be used as a proxy to vote. A user can either have no vote, an upvote, or a downvote.
To vote for a post, we use the following endpoint:
https://oauth.reddit.com/api/vote
Request parameters
Here are some important parameters that we’ll use to call the endpoint:
Parameter | Type | Category | Description |
| integer | required | Vote direction, one of ( This is the direction of the vote. This is |
| string | required | This is the body of the comment which will be posted. |
Click “Run” to cast a vote on the post made earlier.
import fetch from "node-fetch";const endpointUrl = new URL("https://oauth.reddit.com/api/vote");const bodyParameters = new URLSearchParams({dir: 1,id: "t3_{{POST_ID}}",});const headerParameters = {UserAgent: "testscript by u/{{USERNAME}}",authorization: "Bearer {{ACCESS_TOKEN}}",contentType: "application/json",};const options = {method: "POST",headers: headerParameters,body: bodyParameters,};// Function to make API callasync function votePost() {try {const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API callvotePost();
The post_id
variable has the ID of the same post used in the previous widget. No changes have been made besides changing the URL
and the parameters
.
Response fields
In case of successful execution, the response returned by this endpoint is an empty JSON object. An appropriate error message is shown otherwise.