import Tabs from "@theme/Tabs"; import TabItem from "@theme/TabItem";
Triggers allow your app to automatically respond to specific events or actions within a Reddit community. Use triggers to build automation, moderation, and engagement features that react to user or moderator activity.
A trigger is an action you can build into your app that will occur automatically when a specified condition is met. For example, you can set up a trigger to respond when a new post is submitted, a comment is created, or a moderator takes action.
Event triggers let your app automatically respond to a user's or moderator's action. The following trigger types are supported:
onPostSubmitonPostCreateonPostUpdateonPostReportonPostDeleteonPostFlairUpdateonCommentCreateonCommentDeleteonCommentReportonCommentSubmitonCommentUpdateonPostNsfwUpdateonPostSpoilerUpdateonAppInstallonAppUpgradeonModActiononModMailonAutomoderatorFilterPostonAutomoderatorFilterComment
A full list of events and their payloads can be found in the EventTypes documentation. In devvit.json, use the trigger key onModAction; the underlying trigger event type is documented as ModAction. For more details on mod-specific actions, see ModActions and ModMail.
Declare the triggers and their corresponding endpoints in your devvit.json:
"triggers": {
"onAppUpgrade": "/internal/on-app-upgrade",
"onCommentCreate": "/internal/on-comment-create",
"onPostSubmit": "/internal/on-post-submit"
}Listen for the events in your server and access the data passed into the request:
<Tabs variant="pill" groupId="http-server-framework" defaultValue="hono" values={[ { label: 'Hono', value: 'hono' }, { label: 'Express', value: 'express' }, ]}>
import type {
OnAppUpgradeRequest,
OnCommentCreateRequest,
OnPostSubmitRequest,
TriggerResponse,
} from '@devvit/web/shared';
app.post('/internal/on-app-upgrade', async (c) => {
console.log('Handle event for on-app-upgrade!');
const input = await c.req.json<OnAppUpgradeRequest>();
const installer = input.installer;
console.log('Installer:', JSON.stringify(installer, null, 2));
return c.json<TriggerResponse>({ status: 'ok' });
});
app.post('/internal/on-comment-create', async (c) => {
console.log('Handle event for on-comment-create!');
const input = await c.req.json<OnCommentCreateRequest>();
const comment = input.comment;
const author = input.author;
console.log('Comment:', JSON.stringify(comment, null, 2));
console.log('Author:', JSON.stringify(author, null, 2));
return c.json<TriggerResponse>({ status: 'ok' });
});
app.post('/internal/on-post-submit', async (c) => {
console.log('Handle event for on-post-submit!');
const input = await c.req.json<OnPostSubmitRequest>();
const post = input.post;
const author = input.author;
console.log('Post:', JSON.stringify(post, null, 2));
console.log('Author:', JSON.stringify(author, null, 2));
return c.json<TriggerResponse>({ status: 'ok' });
});import type {
OnAppUpgradeRequest,
OnCommentCreateRequest,
OnPostSubmitRequest,
TriggerResponse,
} from '@devvit/web/shared';
const router = express.Router();
// ..
router.post<string, never, TriggerResponse, OnAppUpgradeRequest>(
"/internal/on-app-upgrade",
async (req, res) => {
console.log(`Handle event for on-app-upgrade!`);
const installer = req.body.installer;
console.log("Installer:", JSON.stringify(installer, null, 2));
res.status(200).json({ status: "ok" });
});
router.post<string, never, TriggerResponse, OnCommentCreateRequest>(
"/internal/on-comment-create",
async (req, res) => {
console.log(`Handle event for on-comment-create!`);
const comment = req.body.comment;
const author = req.body.author;
console.log("Comment:", JSON.stringify(comment, null, 2));
console.log("Author:", JSON.stringify(author, null, 2));
res.status(200).json({ status: "ok" });
});
router.post<string, never, TriggerResponse, OnPostSubmitRequest>(
"/internal/on-post-submit",
async (req, res) => {
console.log(`Handle event for on-post-submit!`);
const post = req.body.post;
const author = req.body.author;
console.log("Post:", JSON.stringify(post, null, 2));
console.log("Author:", JSON.stringify(author, null, 2));
res.status(200).json({ status: "ok" });
});- Avoid creating recursive triggers that could cause infinite loops or crashes (for example, a comment trigger that creates a comment).
- Always check the event payload to ensure your app is not the source of the event before taking action.
- Review the EventTypes documentation for details on event payloads.