Read Posts and Comments
Learn about the API calls that you need to read subreddit posts and comments.
We'll cover the following
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:
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 |
| string | required | This is the |
| string | required | This is the |
| integer | required | Its default value is |
| string | optional | This is assigned the string |
Click "Run" to get the posts from the subreddit.
// Defining import libraries hereimport 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 callasync function readSubreddit() {try {const response = await fetch(endpointUrl, options);// Printing responseprintResponse(response);} catch (error) {// Printing error messageprintError(error);}}// Calling function to make API callreadSubreddit();
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:
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 |
| string | required | ID36 of a link, a string of alphanumeric characters, is used to identify an object on Reddit like |
| boolean | required | We set it to |
| boolean | optional | We set it to |
| string | optional | This is one of the following: |
Click "Run" to read comments from the post made earlier.
// Defining import libraries hereimport 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 callasync function readComment() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Printing responseprintResponse(response);} catch (error) {// Printing error messageprintError(error);}}// Calling function to make API callreadComment();
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.