|
| 1 | +# Wiki Pages |
| 2 | + |
| 3 | +Use the Reddit API to read, create, and update a subreddit's wiki pages. Devvit supports both the original wiki (`v1`) and the newer wiki experience on Reddit web and mobile (`v2`). |
| 4 | + |
| 5 | +## Enable the Reddit API |
| 6 | + |
| 7 | +Add the Reddit permission to `devvit.json`: |
| 8 | + |
| 9 | +```json title="devvit.json" |
| 10 | +{ |
| 11 | + "permissions": { |
| 12 | + "reddit": true |
| 13 | + } |
| 14 | +} |
| 15 | +``` |
| 16 | + |
| 17 | +Run wiki edits from an authorized server workflow, such as a moderator menu action. |
| 18 | + |
| 19 | +## Choose a wiki version |
| 20 | + |
| 21 | +Wiki operations default to `v1`. Pass `wikiVersion: "v2"` to use the newer wiki, and check enrollment with `reddit.isWikiV2Enabled(subredditName)` first. Not every subreddit supports v2. |
| 22 | + |
| 23 | +The two wikis have independent content. Pages with the same name can have different content, and updating one version does not update the other. The `config/` pages are available only in v1. |
| 24 | + |
| 25 | +## Read and update a page |
| 26 | + |
| 27 | +This example checks v2 support, reads an existing page, and updates its content: |
| 28 | + |
| 29 | +```ts title="server/index.ts" |
| 30 | +import { context, reddit } from "@devvit/web/server"; |
| 31 | + |
| 32 | +export async function updateCommunityGuide() { |
| 33 | + const { subredditName } = context; |
| 34 | + if (!subredditName) { |
| 35 | + throw new Error("Subreddit context required."); |
| 36 | + } |
| 37 | + |
| 38 | + if (!(await reddit.isWikiV2Enabled(subredditName))) { |
| 39 | + throw new Error("Wiki v2 is not enabled for this subreddit."); |
| 40 | + } |
| 41 | + |
| 42 | + const page = await reddit.getWikiPage(subredditName, "community-guide", { |
| 43 | + wikiVersion: "v2", |
| 44 | + }); |
| 45 | + |
| 46 | + return page.update( |
| 47 | + page.content + "\n\nPlease review the community rules.", |
| 48 | + "Update community guidance", |
| 49 | + ); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Page methods retain the wiki version used to fetch the page. For example, calling `page.update()` on a v2 page updates v2 content. The client also provides [`reddit.updateWikiPage()`](../../api/redditapi/RedditClient/classes/RedditClient.md#updatewikipage) when you already have the subreddit name, page name, and replacement content. |
| 54 | + |
| 55 | +A successful enrollment check returning `false` means v2 is unavailable. A failed request can indicate an authorization or service error. Handle that failure rather than silently switching to v1, which may contain different content. |
| 56 | + |
| 57 | +## Create a page |
| 58 | + |
| 59 | +After checking v2 support, create a page with [`reddit.createWikiPage()`](../../api/redditapi/RedditClient/classes/RedditClient.md#createwikipage): |
| 60 | + |
| 61 | +```ts |
| 62 | +import { context, reddit } from "@devvit/web/server"; |
| 63 | + |
| 64 | +export async function createCommunityGuide() { |
| 65 | + const { subredditName } = context; |
| 66 | + if (!subredditName) { |
| 67 | + throw new Error("Subreddit context required."); |
| 68 | + } |
| 69 | + |
| 70 | + if (!(await reddit.isWikiV2Enabled(subredditName))) { |
| 71 | + throw new Error("Wiki v2 is not enabled for this subreddit."); |
| 72 | + } |
| 73 | + |
| 74 | + return reddit.createWikiPage({ |
| 75 | + subredditName, |
| 76 | + page: "community-guide", |
| 77 | + content: "# Community guide\n\nWelcome!", |
| 78 | + reason: "Initialize community guide", |
| 79 | + wikiVersion: "v2", |
| 80 | + }); |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## Read revision authors and history |
| 85 | + |
| 86 | +Use `revisionAuthorId` to look up the author of a page's current revision. For a revision from `page.getRevisions()`, use `authorId`. Both IDs can be absent, and a user lookup can return `undefined`: |
| 87 | + |
| 88 | +```ts |
| 89 | +import type { WikiPage } from "@devvit/web/server"; |
| 90 | +import { reddit } from "@devvit/web/server"; |
| 91 | + |
| 92 | +export async function logRevisionAuthors(page: WikiPage) { |
| 93 | + if (page.revisionAuthorId) { |
| 94 | + const author = await reddit.getUserById(page.revisionAuthorId); |
| 95 | + console.log(author?.username); |
| 96 | + } |
| 97 | + |
| 98 | + const revisions = await page.getRevisions({ limit: 5 }); |
| 99 | + for await (const revision of revisions) { |
| 100 | + if (revision.authorId) { |
| 101 | + const author = await reddit.getUserById(revision.authorId); |
| 102 | + console.log(author?.username); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +See [WikiPage](../../api/redditapi/models/classes/WikiPage.md) for page settings, editor management, history, and revert operations, and [WikiVersion](../../api/redditapi/models/type-aliases/WikiVersion.md) for version behavior. |
| 109 | + |
| 110 | +## Migrate existing wiki calls |
| 111 | + |
| 112 | +The two-argument call `reddit.getWikiPage(subredditName, pageName)` remains supported and reads v1. Only the overload that takes a revision ID as its third argument is deprecated. Replace that argument with an options object: |
| 113 | + |
| 114 | +```ts |
| 115 | +await reddit.getWikiPage(subredditName, pageName, { |
| 116 | + revisionId, |
| 117 | + wikiVersion: "v1", |
| 118 | +}); |
| 119 | +``` |
| 120 | + |
| 121 | +Replace the deprecated `WikiPage.revisionAuthor` and `WikiPageRevision.author` properties with `revisionAuthorId` and `authorId`, respectively. Fetch the user with `reddit.getUserById()` when you need the full user object. |
0 commit comments