You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: versioned_docs/version-0.13/guides/migrate/public-api.md
+64-59Lines changed: 64 additions & 59 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,35 +1,37 @@
1
-
# Migrating your PRAW App to Devvit Web
1
+
# Migrating Your PRAW App to Devvit Web
2
2
3
-
If you have built Reddit bots or moderation tools using PRAW (Python Reddit API Wrapper) and the standard Reddit API, you can port them directly into Reddit using Devvit Web. Devvit Web represents Reddit's modern client/server architecture for applications, allowing you to build rich moderation tools and automated bots utilizing familiar web frameworks (like Hono and Vite).
4
-
This guide shows you how to transition your Python/PRAW app to a Devvit Web app, utilizing concepts and logic structures you are already familiar with.
3
+
If you have built Reddit bots or moderation tools using PRAW (Python Reddit API Wrapper) and the standard Reddit API, you can port them directly into Reddit using Devvit Web. Devvit Web is Reddit's modern client/server architecture for applications, allowing you to build rich moderation tools and automated bots using familiar web frameworks (like Hono and Vite).
5
4
6
-
## **1\. Creating a Devvit App**
5
+
This guide shows you how to transition your Python/PRAW app to a Devvit Web app with concepts and logic structures you're already familiar with.
7
6
8
-
Unlike standard Python scripts, a Devvit Web application is structurally split into a front-end client and a back-end server, tied together by a configuration file. To jumpstart your migration, you can utilize official Reddit templates.
7
+
## Creating a Devvit app
9
8
10
-
### **Using the Mod Tool Template**
9
+
Unlike standard Python scripts, a Devvit Web app is structurally split into a front-end client and a back-end server and tied together by a configuration file. To jumpstart your migration, you can use official Devvit templates.
11
10
12
-
A highly recommended starting point for migrating PRAW moderation tools is the **Mod Tool Template**. Simply navigate to [developers.reddit.com/new](http://developers.reddit.com/new), select the Mod Tool Template and follow the instructions. The project created for you provides a complete foundation with a lightweight web framework (Hono) for backend logic, Vite for web components, and TypeScript for type safety.
11
+
### Mod tool template
13
12
14
-
### **The Architecture**
13
+
A highly recommended starting point for migrating PRAW moderation tools is the **Mod Tool Template**. Go to [developers.reddit.com/new](http://developers.reddit.com/new), select the Mod Tool Template, and follow the instructions. The project created for you provides a complete foundation with a lightweight web framework (Hono) for backend logic, Vite for web components, and TypeScript for type safety.
14
+
15
+
### Architecture
15
16
16
17
A typical Devvit Web template will generate the following file structure:
17
18
18
19
-**devvit.json**: This is your app's configuration file (replacing the old devvit.yaml paradigm). It defines your app's name, permissions, triggers, and scheduled jobs.
19
20
-**src/client/**: This directory holds your webview code (HTML/CSS/JS or React components built with Vite). For Mod Tools it's common to not use the client folder
20
21
-**src/server/**: This directory contains your backend API logic. Here, a Node server framework (like Hono) processes requests, interacts with the Reddit API, and handles triggers. All server endpoints typically start with /internal/ or /api/.
21
22
22
-
## **2\.Python to TypeScript: Server Concepts**
23
+
## Python to TypeScript: Server Concepts
23
24
24
25
In PRAW, you managed state in a continuous Python loop. In Devvit Web, your application acts as an API server responding to specific incoming webhook requests (handled seamlessly by Hono). Here are the key analogies:
25
26
26
27
-**dict vs. Object/Record:** Python dictionaries serve the same structural purpose as TypeScript objects.
27
28
-**pip install vs. npm install:** Instead of managing a requirements.txt file, Devvit uses a package.json file to track dependencies.
28
29
-**Continuous Polling vs. Webhooks:** Instead of polling Reddit in a while True: loop, Devvit automatically sends a POST request to your Hono server whenever an event occurs.
In Devvit Web, triggers are configured in your devvit.json. When an event happens (like a new comment), Devvit sends a payload to the designated endpoint on your server.
31
34
32
-
In Devvit Web, you configure Triggers in your devvit.json. When an event happens (like a new comment), Devvit sends a payload to the designated endpoint on your server.
33
35
**Step 1: Configuration (devvit.json)**
34
36
35
37
```json
@@ -45,45 +47,48 @@ In Devvit Web, you configure Triggers in your devvit.json. When an event happens
45
47
46
48
```ts
47
49
// Hono is a small web framework used to define HTTP routes.
48
-
import { Hono } from'hono';
50
+
import { Hono } from"hono";
49
51
// TriggerResponse is the expected JSON response shape for trigger endpoints.
To moderate content in Devvit Web, you use the Reddit API client accessible within your server logic. This behaves similarly to comment.mod.remove() in PRAW but relies on asynchronous function calls.
79
+
To moderate content in Devvit Web, use the Reddit API client accessible within your server logic. This behaves similarly to `comment.mod.remove()` in PRAW but relies on asynchronous function calls.
75
80
76
81
```ts
77
82
// Hono handles incoming HTTP requests from Devvit.
78
-
import { Hono } from'hono';
83
+
import { Hono } from"hono";
79
84
// reddit is the Devvit Reddit API client for moderation/content actions.
80
-
import { reddit } from'@devvit/web/server';
85
+
import { reddit } from"@devvit/web/server";
81
86
// TriggerResponse is the response type expected by trigger handlers.
## **5\.Using Redis for Storage (Replacing SQLite/JSON)**
126
+
## Using Redis for storage (replacing SQLite/JSON)
122
127
123
128
Instead of maintaining a local SQLite database for tracking user warnings or config states, Devvit Web gives you direct access to a managed Redis instance.
## **6\. Using Schedulers (Replacing cron jobs or time.sleep)**
161
+
## Using schedulers (replacing cron jobs or time.sleep)
162
+
163
+
PRAW bots frequently rely on time.sleep() for delayed tasks. In Devvit Web, you define Scheduled Tasks in devvit.json and map them to internal Hono endpoints. You can schedule recurring jobs (like cron) or one-off tasks.
157
164
158
-
PRAW bots frequently rely on time.sleep() for delayed tasks. In Devvit Web, you define Scheduled Tasks in devvit.json and map them to internal Hono endpoints. You can schedule recurring jobs (like cron) or one-off tasks.
159
165
**Step 1: Configuration (devvit.json)**
160
166
161
167
```json
@@ -168,39 +174,38 @@ PRAW bots frequently rely on time.sleep() for delayed tasks. In Devvit Web, you
168
174
}
169
175
}
170
176
}
171
-
172
177
```
173
178
174
-
**Step 2: Scheduling and Handling (src/server/index.ts)**
179
+
**Step 2: Scheduling and handling (src/server/index.ts)**
0 commit comments