Add Comments and Vote on a Post

Learn how to make comments and votes on a post using the Reddit API.

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:

Press + to interact
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

thing_id

string

required

This is the fullname of a thing.

text

string

required

This is the body of the comment that will be posted.

api_type

string

required

This is set to the json string. It is a flag that ensures that the response is in JSON format.

Click “Run” to comment on our earlier post.

Press + to interact
// Defining import libraries here
import 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 call
async function commentOnPost() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
commentOnPost();

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:

Press + to interact
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

dir

integer

required

Vote direction, one of (1 for upvote, 0 no vote, -1 downvote).


This is the direction of the vote. This is 1 for an upvote, 0 for no vote, and -1 for a downvote.

id

string

required

This is the body of the comment which will be posted.

Click “Run” to cast a vote on the post made earlier.

Press + to interact
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 call
async function votePost() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
votePost();

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.