Arcjet helps developers protect their apps in just a few lines of code. Implement rate limiting, bot protection, email verification, and defense against common attacks.
This is the Arcjet SDK for SvelteKit.
Visit the quick start guide to get started.
Try an Arcjet protected app live at https://example.arcjet.com (source code).
npm install -S @arcjet/sveltekit
The Arcjet rate limit example below applies a token bucket rate limit rule to a route where we identify the user based on their ID e.g. if they are logged in. The bucket is configured with a maximum capacity of 10 tokens and refills by 5 tokens every 10 seconds. Each request consumes 5 tokens.
// In your `+page.server.ts` file
import arcjet, { tokenBucket } from "@arcjet/sveltekit";
import { error, type RequestEvent } from "@sveltejs/kit";
import { env } from "$env/dynamic/private";
const aj = arcjet({
key: env.ARCJET_KEY!, // Get your site key from https://app.arcjet.com
rules: [
// Create a token bucket rate limit. Other algorithms are supported.
tokenBucket({
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
characteristics: ["userId"], // track requests by a custom user ID
refillRate: 5, // refill 5 tokens per interval
interval: 10, // refill every 10 seconds
capacity: 10, // bucket maximum capacity of 10 tokens
}),
],
});
export async function load(event: RequestEvent) {
const userId = "user123"; // Replace with your authenticated user ID
const decision = await aj.protect(event, { userId, requested: 5 }); // Deduct 5 tokens from the bucket
console.log("Arcjet decision", decision);
if (decision.isDenied()) {
return error(429, "Too Many Requests");
}
return { message: "Hello world" };
}
Arcjet Shield protects your application against common attacks, including the OWASP Top 10. You can run Shield on every request with negligible performance impact.
// In your `hooks.server.ts` file
import arcjet from "@arcjet/sveltekit";
import { error, type RequestEvent } from "@sveltejs/kit";
import { env } from "$env/dynamic/private";
const aj = arcjet({
key: env.ARCJET_KEY!, // Get your site key from https://app.arcjet.com
rules: [
shield({
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
}),
],
});
export async function handle({
event,
resolve,
}: {
event: RequestEvent;
resolve(event: RequestEvent): Response | Promise<Response>;
}): Promise<Response> {
// Ignore routes that extend the Arcjet rules - they will call `.protect` themselves
const filteredRoutes = ["/api/rate-limited", "/rate-limited"];
if (filteredRoutes.includes(event.url.pathname)) {
// The route will handle calling `aj.protect()`
return resolve(event);
}
// Ensure every other route is protected with shield
const decision = await aj.protect(event);
if (decision.isDenied()) {
return error(403, "Forbidden");
}
// Continue with the route
return resolve(event);
}
Licensed under the Apache License, Version 2.0.