Setting up your environment
Install the packages
npm install uploadthing @stratus-dev/sdk
Add env variable
STRATUS_TOKEN=... # A secret key for your app (UUID)
⚠️
If you don't already have a Stratus secret key, sign up (opens in a new tab) and create one from the dashboard ! (opens in a new tab)
Create a Next.js API route
Works with any route method. (opens in a new tab)
- page.tsx
- layout.tsx
- route.ts
Example
💡
If you have better examples...submit a pull request (opens in a new tab).
app/api/route.ts
import { NextRequest, NextResponse } from "next/server";
import { Client, StratusError } from "@stratus-dev/sdk";
const client = new Client({
// Pretty soon we will clean this all up...
apiKey: process.env.STRATUS_TOKEN!,
apiURL: "https://gr8-limit-docker.onrender.com/api/v1/ratelimit",
});
export async function POST(req: NextRequest){
// Inside a route method
try {
const rateLimited = await client.rateLimit();
if (rateLimited) {
return NextResponse.json(
{ error: "Rate limited." },
{ status: 429 }
);
}
// Further logic if not rate limited
// ...
return NextResponse.json(
{ message: "Success" },
{ status: 200 }
);
}catch(error){
if (error instanceof StratusError) {
return NextResponse.json({ error: error.cause }, { status: error.code });
}
return NextResponse.json(
{ message: error.message || "Error" },
{ status: 500 }
);
}
}
🦤
The default rate limit, is 5 requests per second