Create a Subreddit and Submit a Post

Learn how to use the Reddit API to create a subreddit and submit a post to it.

In this lesson, we'll explore three endpoints—creating a subreddit, submitting posts to the subreddit, and approving posts.

Define thing and fullname

Let's discuss some terms before we start making API calls. In Reddit API, if a request or response object is one of the following mentioned in the table below, it is called a thing.

thing

Type prefix

Comment

t1_

Account

t2_

Link

t3_

Message

t4_

Subreddit

t5_

Award

t6_

Each thing is given an ID of 6 characters with a base 36. Reddit uses fullname to refer to the unique ID assigned to a thing, including its prefix. For example, t3_8dmv8z.

Create a subreddit

Subreddits are communities made around a specific topic. Individual users can make posts to any subreddit as long as the rules of that subreddit are followed and the moderators allow the post to be posted.

Any Reddit user can create a subreddit. Once a subreddit is made, a moderator can never delete it. The subreddit will always show the user who first created it. We can only remove the information of the creator of the subreddit, from that subreddit if that account is deleted. Although, instead of deleting a subreddit, we can always make it private.

Use the following endpoint to create a subreddit:

Press + to interact
https://oauth.reddit.com/api/site_admin

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

Request Parameters

Parameter

Type

Category

Description

api_type

string

optional

A string of json is passed to receive the response in JSON format.

name

string

required

This specifies the name of the subreddit.

public_description

string

required

This is a description of our subreddit. It should be in the markdown.

title

string

required

This is the fullname of a thing. It should only be given if you are editing the settings of an already-created subreddit. Otherwise, it should be set as undefined.

type

string

required

To make the subreddit public, you can set the type as public.

Please add the subreddit's name to be created in the widget below. A subreddit's name should be unique, without spaces, and not longer than 21 characters.

Click "Run" to create a subreddit.

Press + to interact
import fetch from "node-fetch";
const Name = "{{SUBREDDIT_NAME}}";
const endpointUrl = new URL("https://oauth.reddit.com/api/site_admin");
const bodyParameters = new URLSearchParams({
api_type: "json",
name: Name,
title: "A sample subreddit",
public_description: "Some description for the sample subreddit",
type: "public",
wikimode: "anyone",
link_type: "self",
});
const headerParameters = {
UserAgent: "testscript by u/{{USERNAME}}",
authorization: "Bearer {{ACCESS_TOKEN}}",
accept: "application/json",
};
// Setting API call options
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
// Function to make API call
async function createSubreddit() {
try {
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
createSubreddit();

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

  • Line 5: We specify the endpoint URL to create the subreddit.

  • Lines 7–15: We add the body parameters using the bodyParameters variable for making the subreddit.

  • Lines 17–21: We add the header parameters by using the headerParameters variable for making the subreddit.

  • Lines 31–41: We created an async function with the name, createSubreddit, in which we use all the defined parameters to create the subreddit.

Response fields

The expected response should be a JSON with a list of errors (none in the case of a successful call).

Submit a post

To eliminate the need to get the approval of a moderator, we'll post to our subreddit. You won't immediately see our post on the main page of the subreddit. To view it, you can go to the "Queues" section.

The is the base URL in focus:

Press + to interact
https://oauth.reddit.com/api/submit

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

Request Parameters

Parameter

Type

Category

Description

sr

string

required

This is the fullname of a subreddit. It should only be given if we are editing the settings of a subreddit that has already been created. Otherwise, it can be set as undefined.

title

string

required

This is the title of the post.

spoiler

boolean

optional

This is used to flag the post as a spoiler.

nsfw

boolean

optional

This is used to flag the post as NSFW.

kind

string

required

This will be one of the following: linkselfimagevideovideogif.

text

string

required

This is the body of the post in markdown format.

Click "Run" to submit a post to the subreddit.

Press + to interact
// Defining import libraries here
import fetch from "node-fetch";
const endpointUrl = new URL("https://oauth.reddit.com/api/submit");
const bodyParameters = new URLSearchParams({
api_type: "json",
sr: "{{SUBREDDIT_NAME}}",
title: "A sample subreddit post",
kind: "self",
text: "Some description for the sample post",
link_type: "self",
});
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 postSubmission() {
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
postSubmission();

Let's understand the code given in the widget above:

  • Lines 6–13: We add the parameters using the bodyParameters variable in which we pass the name of the subreddit and all the parameters required to create a post on the subreddit.

  • Lines 15–19: We add the header parameters by using the headerParameters variable for making the subreddit post.

  • Lines 28–36: We created an async function with the name postSubmission() in which we use all the defined parameters to create the subreddit post.

Response fields

The expected output should be JSON, with details about the post made. You can read more about these important properties in the Response fields for Reddit API lesson in the Appendix.

Approve a post

All posts need to be approved by the moderators to be publicly visible on the subreddit.

To approve the post we made above, use the following endpoint:

Press + to interact
https://oauth.reddit.com/api/approve

Here is an important parameter we'll use to call the endpoint:

Request Parameters

Parameter

Type

Category

Description

id

string

required

This is the fullname of a thing. This should only be given if you are editing the settings of an already-made subreddit. Otherwise, it should be set as undefined.

Press + to interact
import fetch from "node-fetch";
const endpointUrl = new URL("https://oauth.reddit.com/api/approve");
const bodyParameters = new URLSearchParams({
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 postApproval() {
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
postApproval();

Here is the explanation for the code given above:

  • Line 3: We add the URL for the endpoint.

  • Lines 5–13: We add the parameters for approving the post.

Response fields

The expected output should be an empty JSON if it gets processed successfully.