Read Posts and Comments

Learn about the API calls that you need to read subreddit posts and comments.

In this lesson, we’ll explore the endpoints for reading posts on a particular subreddit. We’ll also cover the endpoint for reading comments.

Read subreddit posts

This endpoint is used to get frequently updated posts from the subreddit page usually. This endpoint is considered a listing endpoint because its response contains a list of data. In our case, it contains a list of posts.

In our case, we can read the list of the posts on a subreddit using the API by using the following endpoint:

Press + to interact
https://oauth.reddit.com/r/<subreddit>/new

Request parameters

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

Parameter

Type

Category

Description

after

string

required

This is the fullname of a thing.

before

string

required

This is the fullname of a thing.

limit

integer

required

Its default value is 25, with a maximum value of 100.

show

string

optional

This is assigned the string all.

Click "Run" to get the posts from the subreddit.

Press + to interact
// Defining import libraries here
import fetch from "node-fetch";
const endpointUrl = new URL(
"https://oauth.reddit.com/r/{{SUBREDDIT_NAME}}/new"
);
const headerParameters = {
UserAgent: "testscript by u/{{USERNAME}}",
authorization: `Bearer {{ACCESS_TOKEN}}`,
contentType: "application/json",
};
const options = {
method: "GET",
headers: headerParameters,
};
// Function to make API call
async function readSubreddit() {
try {
const response = await fetch(endpointUrl, options);
// Printing response
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
// Calling function to make API call
readSubreddit();

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

  • Line 4–6: We add the URL for the endpoint.

  • Lines 8–12: We add the parameters to read posts from the subreddit.

  • Lines 14–17: We have defined the request type and also passed the defined parameters to the options variable.

  • Lines 20–30: We have defined the readSubreddit function, which uses all the specified parameters to request the endpoint.

Response fields

The API call to this endpoint returns an array of objects in which each object has properties that store details about a particular post in it. Some important properties of the objects are explained in the Response fields for Reddit API lesson in the Appendix.

Read comments

Now that we can read posts from the subreddit, let's read comments on those posts.

To read comments from the post, we use the following endpoint:

Press + to interact
https://oauth.reddit.com/r/<subreddit>/comments/<post ID>

Request parameters

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

Parameter

Type

Category

Description

article


string

required

ID36 of a link, a string of alphanumeric characters, is used to identify an object on Reddit like a1c1b3.

showedits

boolean

required

We set it to True to see the edits.

showtitle

boolean

optional

We set it to True to see the title.

sort

string

optional

This is one of the following: confidencetopnewcontroversialoldrandomqa, and live.

Click "Run" to read comments from the post made earlier.

Press + to interact
// Defining import libraries here
import fetch from 'node-fetch';
const endpointUrl = new URL('https://oauth.reddit.com/r/{{SUBREDDIT_NAME}}/comments/{{POST_ID}}');
const headerParameters = {
UserAgent : 'testscript by u/{{USERNAME}}',
authorization: `Bearer {{ACCESS_TOKEN}}`,
contentType: 'application/json',
}
const queryParameters = new URLSearchParams({
limit:3,
article:'{{POST_ID}}',
showedits: false,
showtitle: true,
sort:'top'
});
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function readComment() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Printing response
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
// Calling function to make API call
readComment();

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

  • Line 3: We specify the URL to the endpoint discussed above.

  • Line 28: We then use the specified parameters and send a GET request to the endpoint.

Response fields

The result of an API call to this endpoint is an array of JSON objects where each object contains detail about a single comment. The object properties are briefly explained in the Response Fields for Reddit API lesson in the Appendix.