Create and Edit Subreddit Wikis
Learn how to manage your subreddit wikis using JavaScript Reddit API Wrapper.
We'll cover the following
In this lesson, we'll learn two methods—creating a wiki and editing a wiki with a snoowrap.
Create a wiki
Wikis are pages that moderators can enable on their subreddits. Once enabled, others can contribute to them if the moderators allow it. Wikis can have all sorts of information, most of which revolves around the subreddit's topic. We can use the edit()
method of the getWikiPage
method to create one.
Request parameters
Here are some important parameters we'll use to call the method:
Parameter | Type | Category | Description |
| string | required | This is the text of the wiki page we are going to make. |
| string | optional | This is the reason for explaining the change to the wiki page. |
| string | optional | This parameter provides the exact wiki version that needs to be updated. |
Click "Run" to create a wiki.
import snoowrap from "snoowrap";const reddit = new snoowrap({client_id: "{{CLIENT_ID}}",client_secret: "{{CLIENT_SECRET}}",refresh_token: "{{REFRESH_TOKEN}}",user_agent: "testscript by u/{{USERNAME}}",});async function createWiki() {try {const response = await reddit.getSubreddit(`{{SUBREDDIT_NAME}}`).getWikiPage("Wiki Example").edit({ text: "Welcome ", reason: "Added a greeting message" });printResponse(response);} catch (error) {printError(error);}}createWiki();
Let's look at the explanation for the code given above:
Lines 3–8: We initialize the
snoowrap
requester. The parameters are explained in the Parameters for Making a Snoowrap Requester lesson in the AppendixLines 14–15: We use a
getWikiPage()
method, where we pass the wiki name, and then we use theedit()
method in which we declare two parameters.
Response fields
Some important response fields for this method are detailed in the Response fields for Reddit API lesson in the Appendix.
Edit a wiki
If our wiki needs modification, we can update it using the same method. We just have to update the values of the parameters.
Request parameters
Here are some important parameters we'll use to call the method:
Parameter | Type | Category | Description |
| string | required | This is the new or updated text for the wiki page. |
| string | optional | This is the reason for explaining the change to the wiki page. |
| string | optional | This parameter is used to provide the exact wiki version that needs to be updated. |
Click "Run" to edit the wiki page we made in the method above.
import snoowrap from "snoowrap";const reddit = new snoowrap({client_id: "{{CLIENT_ID}}",client_secret: "{{CLIENT_SECRET}}",refresh_token: "{{REFRESH_TOKEN}}",user_agent: "testscript by u/{{USERNAME}}",});async function editWiki() {try {const response = await reddit.getSubreddit(`{{SUBREDDIT_NAME}}`).getWikiPage("Wiki Example").edit({text: "Welcome example updated",reason: "Added a welcome message",});printResponse(response);} catch (error) {printError(error);}}editWiki();
Let's understand the code given in the widget above:
Lines 14–15: We use the
getWikiPage()
method, in which we pass the wiki name, and then we use theedit()
method, in which we update the two parameters.
Response fields
Some important response fields for this method are detailed in the Response fields for Reddit API lesson in the Appendix.