Arcjet Shield WAF protects your application against common attacks, including the OWASP Top 10.
What is Arcjet?
Arcjet helps developers protect their apps in just a few lines of code. Bot detection. Rate limiting. Email validation. Attack protection. Data redaction. A developer-first approach to security.Quick start
This guide will show you how to protect your entire
1. Install Arcjet
In your project root, run the following command to install the SDK:
bun add @arcjet/bun
npm i @arcjet/nest
pnpm add @arcjet/nest
yarn add @arcjet/nest
npm i @arcjet/next
pnpm add @arcjet/next
yarn add @arcjet/next
npm i @arcjet/node
pnpm add @arcjet/node
yarn add @arcjet/node
npm i @arcjet/remix
pnpm add @arcjet/remix
yarn add @arcjet/remix
npm i @arcjet/sveltekit
pnpm add @arcjet/sveltekit
yarn add @arcjet/sveltekit
2. Set your key
Create a free Arcjet account then follow the
instructions to add a site and get a key. Add it to a .env.local
file in your
project root.
ARCJET_KEY=ajkey_yourkey
ARCJET_KEY=ajkey_yourkey
ARCJET_KEY=ajkey_yourkey
Since NODE_ENV
for you, you also
need to set ARCJET_ENV
in your environment file. This allows Arcjet to accept
a local IP address for development purposes.
# NODE_ENV is not set automatically, so tell Arcjet we're in dev# You can leave this unset in prodARCJET_ENV=development# Get your site key from https://app.arcjet.comARCJET_KEY=ajkey_yourkey
Since NODE_ENV
for you, you also
need to set ARCJET_ENV
in your environment file. This allows Arcjet to accept
a local IP address for development purposes.
# NODE_ENV is not set automatically, so tell Arcjet we're in dev# You can leave this unset in prodARCJET_ENV=development# Get your site key from https://app.arcjet.comARCJET_KEY=ajkey_yourkey
Since NODE_ENV
for you, you also
need to set ARCJET_ENV
in your environment file. This allows Arcjet to accept
a local IP address for development purposes.
# NODE_ENV is not set automatically, so tell Arcjet we're in dev# You can leave this unset in prodARCJET_ENV=development# Get your site key from https://app.arcjet.comARCJET_KEY=ajkey_yourkey
3. Protect a route
Create a file called hooks.server.js
in your project (inside src
):
import { env } from "$env/dynamic/private";import arcjet, { shield } from "@arcjet/sveltekit";import { error } from "@sveltejs/kit";
const aj = arcjet({ key: env.ARCJET_KEY, // Get your site key from https://app.arcjet.com rules: [ // Shield protects your app from common attacks e.g. SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "LIVE", }), ],});
export async function handle({ event, resolve }) { const decision = await aj.protect(event);
if (decision.isDenied()) { return error(403, "Forbidden"); }
return resolve(event);}
Create a file called hooks.server.ts
in your project (inside src
):
import { env } from "$env/dynamic/private";import arcjet, { shield } from "@arcjet/sveltekit";import { error, type RequestEvent } from "@sveltejs/kit";
const aj = arcjet({ key: env.ARCJET_KEY!, // Get your site key from https://app.arcjet.com rules: [ // Shield protects your app from common attacks e.g. SQL injection // DRY_RUN mode logs only. Use "LIVE" to block 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"); }
return resolve(event);}
This example shows how to protect your app with Arcjet Shield.
import arcjet, { shield } from "@arcjet/bun";import { env } from "bun";
const aj = arcjet({ key: env.ARCJET_KEY!, // Get your site key from https://app.arcjet.com rules: [ // Shield protects your app from common attacks like SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "LIVE" }), ],});
export default { port: 3000, fetch: aj.handler(async (req) => { const decision = await aj.protect(req); console.log("Arcjet decision", decision);
if (decision.isDenied()) { return new Response("Forbidden", { status: 403 }); }
return new Response("Hello world"); }),};
import arcjet, { shield } from "@arcjet/bun";import { env } from "bun";
const aj = arcjet({ key: env.ARCJET_KEY, // Get your site key from https://app.arcjet.com rules: [ // Shield protects your app from common attacks like SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "LIVE" }), ],});
export default { port: 3000, fetch: aj.handler(async (req) => { const decision = await aj.protect(req); console.log("Arcjet decision", decision);
if (decision.isDenied()) { return new Response("Forbidden", { status: 403 }); }
return new Response("Hello world"); }),};
This creates a global guard that will be applied to all routes. In a real application, implementing guards or per-route protections would give you more flexibility. See the reference guide and the NestJS example app for how to do this.
import { ArcjetGuard, ArcjetModule, shield } from "@arcjet/nest";import { Module } from "@nestjs/common";import { ConfigModule } from "@nestjs/config";import { APP_GUARD, NestFactory } from "@nestjs/core";
@Module({ imports: [ ConfigModule.forRoot({ isGlobal: true, }), ArcjetModule.forRoot({ isGlobal: true, key: process.env.ARCJET_KEY!, rules: [ // Shield protects your app from common attacks e.g. SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "DRY_RUN" }), ], }), ], controllers: [], providers: [ { provide: APP_GUARD, useClass: ArcjetGuard, }, ],})class AppModule {}
async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000);}bootstrap();
import { ArcjetGuard, ArcjetModule, shield } from "@arcjet/nest";import { Module } from "@nestjs/common";import { ConfigModule } from "@nestjs/config";import { APP_GUARD, NestFactory } from "@nestjs/core";
@Module({ imports: [ ConfigModule.forRoot({ isGlobal: true, }), ArcjetModule.forRoot({ isGlobal: true, key: process.env.ARCJET_KEY, rules: [ // Shield protects your app from common attacks e.g. SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "DRY_RUN" }), ], }), ], controllers: [], providers: [ { provide: APP_GUARD, useClass: ArcjetGuard, }, ],})class AppModule {}
async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000);}bootstrap();
This example shows how to protect your app with Arcjet Shield. You can also use it in middleware to protect every route, but we’ll start with a single route.
import arcjet, { shield } from "@arcjet/next";
const aj = arcjet({ key: process.env.ARCJET_KEY, rules: [ // Shield protects your app from common attacks e.g. SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "DRY_RUN", }), ],});
export default async function handler(req, res) { const decision = await aj.protect(req);
for (const result of decision.results) { console.log("Rule Result", result); }
console.log("Conclusion", decision.conclusion);
if (decision.isDenied() && decision.reason.isShield()) { return res.status(403).json({ error: "You are suspicious!" }); // Returning the reason is useful for debugging, but don't return it to the // client in production // .json({ error: "You are suspicious!", reason: decision.reason }); }
res.status(200).json({ name: "Hello world" });}
import arcjet, { shield } from "@arcjet/next";import type { NextApiRequest, NextApiResponse } from "next";
const aj = arcjet({ key: process.env.ARCJET_KEY!, rules: [ // Shield protects your app from common attacks e.g. SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "DRY_RUN", }), ],});
export default async function handler( req: NextApiRequest, res: NextApiResponse,) { const decision = await aj.protect(req);
for (const result of decision.results) { console.log("Rule Result", result); }
console.log("Conclusion", decision.conclusion);
if (decision.isDenied() && decision.reason.isShield()) { return res.status(403).json({ error: "You are suspicious!" }); // Returning the reason is useful for debugging, but don't return it to the // client in production // .json({ error: "You are suspicious!", reason: decision.reason }); }
res.status(200).json({ name: "Hello world" });}
import arcjet, { shield } from "@arcjet/next";import { NextResponse } from "next/server";
const aj = arcjet({ key: process.env.ARCJET_KEY!, rules: [ // Shield protects your app from common attacks e.g. SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "DRY_RUN", }), ],});
export async function GET(req: Request) { const decision = await aj.protect(req);
for (const result of decision.results) { console.log("Rule Result", result); }
console.log("Conclusion", decision.conclusion);
if (decision.isDenied() && decision.reason.isShield()) { return NextResponse.json( { error: "You are suspicious!", // Useful for debugging, but don't return it to the client in // production //reason: decision.reason, }, { status: 403 }, ); }
return NextResponse.json({ message: "Hello world", });}
import arcjet, { shield } from "@arcjet/next";import { NextResponse } from "next/server";
const aj = arcjet({ key: process.env.ARCJET_KEY, rules: [ // Shield protects your app from common attacks e.g. SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "DRY_RUN", }), ],});
export async function GET(req) { const decision = await aj.protect(req);
for (const result of decision.results) { console.log("Rule Result", result); }
console.log("Conclusion", decision.conclusion);
if (decision.isDenied() && decision.reason.isShield()) { return NextResponse.json( { error: "You are suspicious!", // Useful for debugging, but don't return it to the client in // production //reason: decision.reason, }, { status: 403 }, ); }
return NextResponse.json({ message: "Hello world", });}
This sets up a simple server with Arcjet configured in the handler:
import arcjet, { shield } from "@arcjet/node";import http from "node:http";
const aj = arcjet({ // Get your site key from https://app.arcjet.com // and set it as an environment variable rather than hard coding. key: process.env.ARCJET_KEY, rules: [ // Shield protects your app from common attacks e.g. SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "DRY_RUN", }), ],});
const server = http.createServer(async function (req, res) { const decision = await aj.protect(req);
for (const result of decision.results) { console.log("Rule Result", result); }
console.log("Conclusion", decision.conclusion);
if (decision.isDenied()) { res.writeHead(403, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: "Forbidden" })); } else { res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify({ message: "Hello world" })); }});
server.listen(8000);
import arcjet, { shield } from "@arcjet/node";import http from "node:http";
const aj = arcjet({ // Get your site key from https://app.arcjet.com // and set it as an environment variable rather than hard coding. key: process.env.ARCJET_KEY!, rules: [ // Shield protects your app from common attacks e.g. SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "DRY_RUN", }), ],});
const server = http.createServer(async function ( req: http.IncomingMessage, res: http.ServerResponse,) { const decision = await aj.protect(req);
for (const result of decision.results) { console.log("Rule Result", result); }
console.log("Conclusion", decision.conclusion);
if (decision.isDenied()) { res.writeHead(403, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: "Forbidden" })); } else { res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify({ message: "Hello world" })); }});
server.listen(8000);
Create a new route at app/routes/arcjet.tsx
with the contents:
import arcjet, { shield } from "@arcjet/remix";
const aj = arcjet({ // Get your site key from https://app.arcjet.com // and set it as an environment variable rather than hard coding. key: process.env.ARCJET_KEY, rules: [ // Shield protects your app from common attacks e.g. SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "DRY_RUN", }), ],});
// The loader function is called for every request to the app, but you could// also protect an actionexport async function loader(args) { const decision = await aj.protect(args);
if (decision.isDenied()) { throw new Response("Forbidden", { status: 503, statusText: "Forbidden" }); }
// We don't need to use the decision elsewhere, but you could return it to // the component return null;}
export default function Index() { return ( <> <h1>Hello world</h1> </> );}
import arcjet, { shield } from "@arcjet/remix";import type { LoaderFunctionArgs } from "@remix-run/node";
const aj = arcjet({ // Get your site key from https://app.arcjet.com // and set it as an environment variable rather than hard coding. key: process.env.ARCJET_KEY!, rules: [ // Shield protects your app from common attacks e.g. SQL injection // DRY_RUN mode logs only. Use "LIVE" to block shield({ mode: "DRY_RUN", }), ],});
// The loader function is called for every request to the app, but you could// also protect an actionexport async function loader(args: LoaderFunctionArgs) { const decision = await aj.protect(args);
for (const result of decision.results) { console.log("Rule Result", result); }
console.log("Conclusion", decision.conclusion);
if (decision.isDenied()) { throw new Response("Forbidden", { status: 403, statusText: "Forbidden" }); }
// We don't need to use the decision elsewhere, but you could return it to // the component return null;}
export default function Index() { return ( <> <h1>Hello world</h1> </> );}
4. Start App
Start your app and load http://localhost:3000
. Refresh the page and you will
see the requests showing up in the Arcjet dashboard.
Your entire application is protected.
4. Start App
Start your app and load http://localhost:3000
. Refresh the page and you will
see the requests showing up in the Arcjet dashboard.
Your entire application is protected.
4. Start app
Start your app and load http://localhost:5173
. Refresh the page and you will
see the requests showing up in the Arcjet dashboard.
Your entire application is protected.
4. Start app
npm run start
pnpm run start
yarn run start
4. Start server
npx tsx --env-file .env.local index.ts
node --env-file .env.local index.js
4. Start app
npm run dev
pnpm run dev
yarn run dev
5. Simulate a suspicious request
To see Arcjet Shield WAF in action, try making a request with the special header
x-arcjet-suspicious
set to true
. After 5 requests, Arcjet Shield will be
triggered and will block the request. This simulates the threshold being reached
and is a constant, so you can use it as part of your tests.
curl -v -H "x-arcjet-suspicious: true" http://localhost:3000
After the 5th request, you will see this in your logs:
Rule Result ArcjetRuleResult { ttl: 0, state: 'DRY_RUN', conclusion: 'DENY', reason: ArcjetShieldReason { type: 'SHIELD', shieldTriggered: true }}Conclusion ALLOW
The final conclusion is ALLOW
even though the rule result conclusion is
DENY
. This is because the rule is in dry run mode. Switch it to LIVE
mode to
actually block the request.
curl -v -H "x-arcjet-suspicious: true" http://localhost:3000
After the 5th request, you will see this in your logs:
Rule Result ArcjetRuleResult { ttl: 0, state: 'DRY_RUN', conclusion: 'DENY', reason: ArcjetShieldReason { type: 'SHIELD', shieldTriggered: true }}Conclusion ALLOW
The final conclusion is ALLOW
even though the rule result conclusion is
DENY
. This is because the rule is in dry run mode. Switch it to LIVE
mode to
actually block the request.
curl -v -H "x-arcjet-suspicious: true" http://localhost:8000
After the 5th request, you will see this in your logs:
Rule Result ArcjetRuleResult { ttl: 0, state: 'DRY_RUN', conclusion: 'DENY', reason: ArcjetShieldReason { type: 'SHIELD', shieldTriggered: true }}Conclusion ALLOW
The final conclusion is ALLOW
even though the rule result conclusion is
DENY
. This is because the rule is in dry run mode. Switch it to LIVE
mode to
actually block the request.
curl -v -H "x-arcjet-suspicious: true" http://localhost:5173/arcjet
After the 5th request, you will see this in your logs:
Rule Result ArcjetRuleResult { ttl: 0, state: 'DRY_RUN', conclusion: 'DENY', reason: ArcjetShieldReason { type: 'SHIELD', shieldTriggered: true }}Conclusion ALLOW
The final conclusion is ALLOW
even though the rule result conclusion is
DENY
. This is because the rule is in dry run mode. Switch it to LIVE
mode to
actually block the request.
curl -v -H "x-arcjet-suspicious: true" http://localhost:3000
The default response for a blocked request is a 403 Forbidden which you will see when you make the 6th request:
# ...5 requests made previouslycurl -v -H "x-arcjet-suspicious: true" http://localhost:3000...> GET / HTTP/2> Host: localhost:3000> User-Agent: curl/8.4.0> Accept: */*> x-arcjet-suspicious: true>< HTTP/2 403< content-type: application/json; charset=utf-8< date: Tue, 09 Jan 2024 13:43:04 GMT< etag: "7a3v9j8es220"< vary: Accept-Encoding< content-length: 72<* Connection #0 to host localhost left intact{"message":"Forbidden"}%
curl -v -H "x-arcjet-suspicious: true" http://localhost:5173
The default response for a blocked request is a 403 Forbidden which you will see when you make the 6th request:
# ...5 requests made previouslycurl -v -H "x-arcjet-suspicious: true" http://localhost:5173...> GET / HTTP/2> Host: localhost:5173> User-Agent: curl/8.4.0> Accept: */*> x-arcjet-suspicious: true>< HTTP/2 403< content-type: application/json; charset=utf-8< date: Tue, 09 Jan 2024 13:43:04 GMT< etag: "7a3v9j8es220"< vary: Accept-Encoding< content-length: 72<* Connection #0 to host localhost left intact{"message":"Forbidden"}%
The requests will also show in the Arcjet dashboard.
FAQs
Do I need to run any infrastructure e.g. Redis?
No, Arcjet handles all the infrastructure for you so you don't need to worry about deploying global Redis clusters, designing data structures to track rate limits, or keeping security detection rules up to date.
What is the performance overhead?
Arcjet SDK tries to do as much as possible asynchronously and locally to minimize latency for each request. Where decisions can be made locally or previous decisions are cached in-memory, latency is usually <1ms.
When a call to the Arcjet API is required, such as when tracking a rate limit in a serverless environment, there is some additional latency before a decision is made. The Arcjet API has been designed for high performance and low latency, and is deployed to multiple regions around the world. The SDK will automatically use the closest region which means the total overhead is typically no more than 20-30ms, often significantly less.
What happens if Arcjet is unavailable?
Where a decision has been cached locally e.g. blocking a client, Arcjet will continue to function even if the service is unavailable.
If a call to the Arcjet API is needed and there is a network problem or Arcjet is unavailable, the default behavior is to fail open and allow the request. You have control over how to handle errors, including choosing to fail close if you prefer. See the reference docs for details.
How does Arcjet protect me against DDoS attacks?
Network layer attacks tend to be generic and high volume, so these are best handled by your hosting platform. Most cloud providers include network DDoS protection by default.
Arcjet sits closer to your application so it can understand the context. This is important because some types of traffic may not look like a DDoS attack, but can still have the same effect. For example, a customer making too many API requests and affecting other customers, or large numbers of signups from disposable email addresses.
Network-level DDoS protection tools find it difficult to protect against this type of traffic because they don't understand the structure of your application. Arcjet can help you to identify and block this traffic by integrating with your codebase and understanding the context of the request e.g. the customer ID or sensitivity of the API route.
Volumetric network attacks are best handled by your hosting provider. Application level attacks need to be handled by the application. That's where Arcjet helps.
What next?
Explore
Arcjet can be used with specific rules on individual routes or as general protection on your entire application. You can setup bot protection, rate limiting for your API, minimize fraudulent registrations with the signup form protection and more.
Get help
Need help with anything? Email us or join our Discord to get support from our engineering team.