@arcjet/sveltekit
v1.2.0
Published
Arcjet SDK for SvelteKit
Readme
@arcjet/sveltekit
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.
Getting started
Visit the quick start guide to get started.
Example
Try an Arcjet protected Next.js app live at
example.arcjet.com
(source code).
See arcjet/example-sveltekit for a SvelteKit
example.
What is this?
This is our adapter to integrate Arcjet into SvelteKit. Arcjet helps you secure your SvelteKit web application. This package exists so that we can provide the best possible experience to SvelteKit users.
When should I use this?
You can use this if you are using SvelteKit. See our Get started guide for other supported frameworks.
Install
This package is ESM only. Install with npm in Node.js:
npm install @arcjet/sveltekitUse
Configure Arcjet in hooks.server.ts:
import { env } from "$env/dynamic/private";
import arcjet, { shield } from "@arcjet/sveltekit";
import { type RequestEvent, error } from "@sveltejs/kit";
// Get your Arcjet key at <https://app.arcjet.com>.
// Set it as an environment variable instead of hard coding it.
const arcjetKey = env.ARCJET_KEY;
if (!arcjetKey) {
throw new Error("Cannot find `ARCJET_KEY` environment variable");
}
const aj = arcjet({
key: arcjetKey,
rules: [
// Shield protects your app from common attacks.
// Use `DRY_RUN` instead of `LIVE` to only log.
shield({ mode: "LIVE" }),
],
});
export async function handle({
event,
resolve,
}: {
event: RequestEvent;
resolve(event: RequestEvent): Response | Promise<Response>;
}): Promise<Response> {
const decision = await aj.protect(event);
if (decision.isDenied()) {
return error(403, "Forbidden");
}
// Continue with the route
return resolve(event);
}For more on how to configure Arcjet with SvelteKit and how to protect SvelteKit, see the Arcjet SvelteKit SDK reference on our website.
