Reddit API Overview
The Reddit API allows you to read and write Reddit content such as posts / comments / upvotes, in order to integrate your app's behavior with the content of the community it's installed in.
Unlike traditional Reddit API usage, you don't need to create an app at reddit.com/prefs/apps or manage API keys. Devvit handles authentication automatically when you enable the reddit permission in your app.
Private user data
Devvit apps cannot access certain private user data. This data is private to the logged-in user and is not exposed through the Devvit platform:
- Subscribed subreddits - The list of subreddits a user is subscribed to
- Upvoted and downvoted content - Posts and comments the user has voted on
- Saved content - Posts and comments the user has saved
- Recently viewed posts - The user's browsing history
- Private profile information - Any profile data that is not publicly visible
- Follows and friends - The list of users someone follows (on reddit.com) or has friended (on Old Reddit)
The Reddit client
Devvit Web (recommended)
Enable the Reddit permission in devvit.json, then import the client in your server code:
{
"permissions": {
"reddit": true
}
}
import { reddit } from '@devvit/web/server';
Legacy public-api (deprecated)
In Devvit 0.15, blocks and @devvit/public-api remain supported but deprecated. We recommend migrating to @devvit/web where possible.
In an existing public-api handler, use the Reddit client passed through context:
const subreddit = await context.reddit.getCurrentSubreddit();
Keep public-api handlers in the file configured by blocks.entry. The examples below use Devvit Web. If your app uses devvit.yaml, migrate to devvit.json before upgrading to 0.15.
Reddit Thing IDs
Reddit uses prefixed IDs (called "things") to identify different types of content:
| Prefix | Type | Example | Description |
|---|---|---|---|
t1_ | Comment | t1_abc123 | A comment on a post or reply to another comment |
t2_ | User | t2_xyz789 | A Reddit user account |
t3_ | Post | t3_def456 | A post |
t4_ | Message | t4_ghi012 | A private message |
t5_ | Subreddit | t5_jkl345 | A subreddit community |
These IDs are returned by API methods and used when referencing specific content:
// Get a post by its full ID
const post = await reddit.getPostById('t3_abc123');
// Get a comment by its full ID
const comment = await reddit.getCommentById('t1_xyz789');
// A comment's parentId can be either a post (t3_) or another comment (t1_)
const parentId = comment.parentId; // 't3_abc123' or 't1_def456'
Search posts
Use reddit.searchPosts() to search across Reddit or restrict a query to a subreddit. Use subreddit.searchPosts() to search within an existing subreddit object.
Both methods return a lazy Listing<Post>. Iterate with for await, or use .get(count) or .all() to fetch results. Set a practical limit to bound the number of results your app requests.
import { reddit } from "@devvit/web/server";
export async function searchAnnouncements() {
const subreddit = await reddit.getCurrentSubreddit();
const posts = subreddit.searchPosts({
query: "announcement",
sort: "new",
timeframe: "month",
limit: 20,
});
for await (const post of posts) {
console.log(post.id, post.title);
}
}
To use the client with the same subreddit scope, pass subredditName explicitly:
const posts = reddit.searchPosts({
query: "announcement",
subredditName: subreddit.name,
sort: "new",
timeframe: "month",
limit: 20,
});
Omitting subredditName searches all, even when your code runs in an installed-subreddit context. The subreddit object's searchPosts() method always uses that subreddit and does not accept a subredditName option.
| Option | Description | Default |
|---|---|---|
query | Search query | Required |
sort | relevance, hot, top, new, or comments | relevance |
timeframe | hour, day, week, month, year, or all | all |
limit | Maximum number of results to fetch | No total limit set by the client |
pageSize | Number of results requested per page | 100 |
after, before | Listing cursors for pagination | Omitted |
The search methods return a listing immediately; the network requests happen as you consume it. Search ranking and availability of newly indexed content depend on Reddit's search service.
See reddit.searchPosts(), subreddit.searchPosts(), and SearchPostsOptions for the API references.
Example usage
Submitting a post
This Devvit Web example creates a custom post using the default entry configured in devvit.json:
import { context, reddit } from '@devvit/web/server';
export const createPost = async () => {
const { subredditName } = context;
if (!subredditName) {
throw new Error('subredditName is required');
}
return await reddit.submitCustomPost({
userGeneratedContent: {
text: 'Hello there! This is a post from a Devvit app',
},
subredditName: subredditName,
title: 'New Post',
entry: 'default',
});
};
Managing Community Highlights
Community Highlights display selected posts at the top of a subreddit. Use post.isHighlighted(), post.highlight(), and post.unhighlight() to inspect or change a post's highlight. Use the subreddit methods to retrieve the highlighted posts and change their display order.
These operations use the app's Reddit access. If you expose them through an app endpoint, check that the current user has permission to manage the subreddit's posts.
This example checks the caller's moderator permissions, highlights the current post for seven days, and moves it to the first position:
import { context, reddit } from "@devvit/web/server";
export async function highlightCurrentPost() {
if (!context.userId || !context.postId) {
throw new Error("Signed-in post interaction required.");
}
const subreddit = await reddit.getCurrentSubreddit();
const user = await reddit.getUserById(context.userId);
const permissions = await user?.getModPermissionsForSubreddit(subreddit.name);
if (
!permissions?.some(
(permission) => permission === "all" || permission === "posts",
)
) {
throw new Error("Moderator post permissions required.");
}
const post = await reddit.getPostById(context.postId);
if (post.subredditId !== subreddit.id) {
throw new Error("Post must belong to this subreddit.");
}
const wasHighlighted = await post.isHighlighted();
await post.highlight({
highlightLabelType: "ANNOUNCEMENT",
highlightUntil: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
});
const highlights = await subreddit.getHighlightedPosts();
await subreddit.reorderHighlightedPosts([
post.id,
...highlights
.filter((item) => item.postId !== post.id)
.map((item) => item.postId),
]);
return {
wasHighlighted,
highlights: await subreddit.getHighlightedPosts(),
};
}
post.highlight() updates the settings if the post is already highlighted. Omit highlightUntil to keep a highlight until it is removed. A subreddit can have six highlights; highlighting a seventh post removes the last highlight in display order.
To remove a highlight, call await post.unhighlight() from an authorized action. Removing a highlight from a post that is not highlighted has no effect. These methods do not accept runAs.
subreddit.getHighlightedPosts() returns an ordered array of HighlightedPostInfo, rather than Post objects. Each item has a postId and optional highlightedUntil and highlightLabel fields. Use reddit.getPostById(postId) when you need the post's content. Note the different names for the input options: highlightUntil and highlightLabelType.
Fetch the current highlights before reordering and pass the complete intended order. Another moderator can change the list between requests, so handle failures and refresh the list before trying again.
See the Post highlight methods, HighlightPostOptions, and Subreddit reorder method for the API references.
Submitting a comment
Auto-comments should be used to spark conversation in the post comments, but you should avoid lower-signal updates (e.g., level/progress pings).
import { reddit } from '@devvit/web/server';
export const createComment = async () => {
return await reddit.submitComment({
id: 't3_123456', // Replace with the actual post ID
text: 'This is a comment from a Devvit app',
runAs: 'USER' // Optional: specify the user to run as
});
};
Wiki pages
See Wiki Pages to read and update wiki content, choose between v1 and v2, and migrate existing wiki calls.
