Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add ratelimit

Marc Seitza876980738d2
diff --git a/lib/redis.ts b/lib/redis.ts
new file mode 100644
index 00000000..3a1dd024
--- /dev/null
+++ b/lib/redis.ts
@@ -0,0 +1,6 @@
+import { Redis } from "@upstash/redis";
+
+export const redis = new Redis({
+ url: process.env.UPSTASH_REDIS_REST_TOKEN as string,
+ token: process.env.UPSTASH_REDIS_REST_TOKEN as string,
+});
diff --git a/package-lock.json b/package-lock.json
index a2ecd5ac..48d9f0a8 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -38,6 +38,8 @@
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.15",
"@upstash/qstash": "^2.2.0",
+ "@upstash/ratelimit": "^1.0.0",
+ "@upstash/redis": "^1.25.1",
"@vercel/analytics": "^1.1.1",
"@vercel/blob": "^0.15.1",
"ai": "^2.2.25",
@@ -3082,6 +3084,17 @@
"resolved": "https://registry.npmjs.org/@ungap/structured-clone/-/structured-clone-1.2.0.tgz",
"integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ=="
},
+ "node_modules/@upstash/core-analytics": {
+ "version": "0.0.6",
+ "resolved": "https://registry.npmjs.org/@upstash/core-analytics/-/core-analytics-0.0.6.tgz",
+ "integrity": "sha512-cpPSR0XJAJs4Ddz9nq3tINlPS5aLfWVCqhhtHnXt4p7qr5+/Znlt1Es736poB/9rnl1hAHrOsOvVj46NEXcVqA==",
+ "dependencies": {
+ "@upstash/redis": "^1.19.3"
+ },
+ "engines": {
+ "node": ">=16.0.0"
+ }
+ },
"node_modules/@upstash/qstash": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/@upstash/qstash/-/qstash-2.2.0.tgz",
@@ -3091,6 +3104,22 @@
"jose": "^4.14.4"
}
},
+ "node_modules/@upstash/ratelimit": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/@upstash/ratelimit/-/ratelimit-1.0.0.tgz",
+ "integrity": "sha512-IUlc+UKuHJQqXGQi7E3UKV0CKfRZ0g6qeNnbSve940k3xLoM+X3H45MrSwDaH7meMWemztLs0Q4yjkd3ItddCw==",
+ "dependencies": {
+ "@upstash/core-analytics": "^0.0.6"
+ }
+ },
+ "node_modules/@upstash/redis": {
+ "version": "1.25.1",
+ "resolved": "https://registry.npmjs.org/@upstash/redis/-/redis-1.25.1.tgz",
+ "integrity": "sha512-ACj0GhJ4qrQyBshwFgPod6XufVEfKX2wcaihsEvSdLYnY+m+pa13kGt1RXm/yTHKf4TQi/Dy2A8z/y6WUEOmlg==",
+ "dependencies": {
+ "crypto-js": "^4.2.0"
+ }
+ },
"node_modules/@vercel/analytics": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/@vercel/analytics/-/analytics-1.1.1.tgz",
diff --git a/package.json b/package.json
index f831d32e..b560333b 100644
--- a/package.json
+++ b/package.json
@@ -45,6 +45,8 @@
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.15",
"@upstash/qstash": "^2.2.0",
+ "@upstash/ratelimit": "^1.0.0",
+ "@upstash/redis": "^1.25.1",
"@vercel/analytics": "^1.1.1",
"@vercel/blob": "^0.15.1",
"ai": "^2.2.25",
@@ -69,9 +71,9 @@
"next-auth": "^4.24.5",
"next-plausible": "^3.11.3",
"next-themes": "^0.2.1",
- "openai": "^4.20.0",
"notion-client": "^6.16.0",
"notion-utils": "^6.16.0",
+ "openai": "^4.20.0",
"postcss": "^8.4.31",
"react": "^18.2.0",
"react-dom": "^18.2.0",
diff --git a/pages/api/assistants/chat.ts b/pages/api/assistants/chat.ts
index c21906a3..a9bbdf4f 100644
--- a/pages/api/assistants/chat.ts
+++ b/pages/api/assistants/chat.ts
@@ -1,12 +1,35 @@
import { experimental_AssistantResponse } from "ai";
import OpenAI from "openai";
import { type MessageContentText } from "openai/resources/beta/threads/messages/messages";
+import { Ratelimit } from "@upstash/ratelimit";
+import { redis } from "@/lib/redis";
// Create an OpenAI API client (that's edge friendly!)
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY || "",
});
+const ratelimit = {
+ public: new Ratelimit({
+ redis,
+ analytics: true,
+ prefix: "ratelimit:public",
+ limiter: Ratelimit.slidingWindow(1, "3600s"),
+ }),
+ free: new Ratelimit({
+ redis,
+ analytics: true,
+ prefix: "ratelimit:free",
+ limiter: Ratelimit.slidingWindow(60, "10s"),
+ }),
+ paid: new Ratelimit({
+ redis,
+ analytics: true,
+ prefix: "ratelimit:paid",
+ limiter: Ratelimit.slidingWindow(60, "10s"),
+ }),
+};
+
// IMPORTANT! Set the runtime to edge
export const config = {
runtime: "edge",
@@ -20,6 +43,32 @@ export default async function POST(req: Request) {
isPublic: boolean | null;
} = await req.json();
+ if (input.isPublic) {
+ const ip = req.headers.get("x-forwarded-for");
+ const ratelimit = new Ratelimit({
+ redis: redis,
+ // rate limit to 5 requests per hour
+ limiter: Ratelimit.slidingWindow(5, "3600s"),
+ });
+
+ const { success, limit, reset, remaining } = await ratelimit.limit(
+ `ratelimit_${ip}`,
+ );
+
+ console.log("ratelimit", { success, limit, reset, remaining });
+
+ if (!success) {
+ return new Response("You have reached your request limit for the day.", {
+ status: 429,
+ headers: {
+ "X-RateLimit-Limit": limit.toString(),
+ "X-RateLimit-Remaining": remaining.toString(),
+ "X-RateLimit-Reset": reset.toString(),
+ },
+ });
+ }
+ }
+
// create a threadId if one wasn't provided
const threadId = input.threadId ?? (await openai.beta.threads.create()).id;