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.
This is the Arcjet SDK for the Next.js framework. Find our other SDKs on GitHub.
Try an Arcjet protected app live at https://example.arcjet.com (source code).
Arcjet security features for protecting Next.js apps:
- 🤖 Bot protection - manage traffic by automated clients and bots.
- 🛑 Rate limiting - limit the number of requests a client can make.
- 🛡️ Shield WAF - protect your application against common attacks.
- 📧 Email validation - prevent users from signing up with fake email addresses.
- 📝 Signup form protection - combines rate limiting, bot protection, and email validation to protect your signup forms.
- 🕵️♂️ Sensitive information detection - block personally identifiable information (PII).
- 🚅 Nosecone - set security headers such as
Content-Security-Policy
(CSP).
This example will protect a Next.js API route with a rate limit, bot detection, and Shield WAF.
You can also find this quick start guide in the docs.
npm i @arcjet/next
Create a free Arcjet account then follow the instructions to add a site and get a key.
Add your key to a .env.local
file in your project root.
ARCJET_KEY=ajkey_yourkey
Create a new API route at /app/api/arcjet/route.ts
:
import arcjet, {
type ArcjetRuleResult,
detectBot,
shield,
tokenBucket,
} from "@arcjet/next";
import { NextResponse } from "next/server";
const aj = arcjet({
key: process.env.ARCJET_KEY!, // Get your site key from https://app.arcjet.com
rules: [
// Shield protects your app from common attacks e.g. SQL injection
shield({ mode: "LIVE" }),
// Create a bot detection rule
detectBot({
mode: "LIVE", // Blocks requests. Use "DRY_RUN" to log only
// Block all bots except the following
allow: [
"CATEGORY:SEARCH_ENGINE", // Google, Bing, etc
// Uncomment to allow these other common bot categories
// See the full list at https://arcjet.com/bot-list
//"CATEGORY:MONITOR", // Uptime monitoring services
//"CATEGORY:PREVIEW", // Link previews e.g. Slack, Discord
],
}),
// Create a token bucket rate limit. Other algorithms are supported.
tokenBucket({
mode: "LIVE",
refillRate: 5, // Refill 5 tokens per interval
interval: 10, // Refill every 10 seconds
capacity: 10, // Bucket capacity of 10 tokens
}),
],
});
function isSpoofed(result: ArcjetRuleResult) {
return (
// You probably don't want DRY_RUN rules resulting in a denial
// since they are generally used for evaluation purposes but you
// could log here.
result.state !== "DRY_RUN" &&
result.reason.isBot() &&
result.reason.isSpoofed()
);
}
export async function GET(req: Request) {
const decision = await aj.protect(req, { requested: 5 }); // Deduct 5 tokens from the bucket
console.log("Arcjet decision", decision);
if (decision.isDenied()) {
if (decision.reason.isRateLimit()) {
return NextResponse.json(
{ error: "Too Many Requests", reason: decision.reason },
{ status: 429 },
);
} else if (decision.reason.isBot()) {
return NextResponse.json(
{ error: "No bots allowed", reason: decision.reason },
{ status: 403 },
);
} else {
return NextResponse.json(
{ error: "Forbidden", reason: decision.reason },
{ status: 403 },
);
}
}
// Arcjet Pro plan verifies the authenticity of common bots using IP data.
// Verification isn't always possible, so we recommend checking the decision
// separately.
// https://docs.arcjet.com/bot-protection/reference#bot-verification
if (decision.results.some(isSpoofed)) {
return NextResponse.json(
{ error: "Forbidden", reason: decision.reason },
{ status: 403 },
);
}
return NextResponse.json({ message: "Hello world" });
}
npm run dev
Visit http://localhost:3000/api/arcjet
in your browser and refresh a few times
to hit the rate limit.
Wait 10 seconds, then run:
curl -v http://localhost:3000/api/arcjet
The wait is necessary because the decision is cached for your IP based on the
interval
rate limit configuration.
You should see a 403
response because curl
is considered a bot by default
(customizable).
The requests will also show in the Arcjet dashboard.
- Customize allowed bots
- Protect server actions
- Learn how Arcjet works.
- Review the Next.js SDK reference.
The protect function returns a Promise
that resolves to an
ArcjetDecision
object. This allows you to access the full decision
and results.
for (const result of decision.results) {
// Check the rate limit rule results
if (result.reason.isRateLimit()) {
if (result.isDenied()) {
console.log("Rate limit rule returned deny conclusion", result);
}
// Log how many tokens are remaining in the bucket
const remaining = decision.reason.remaining;
console.log("Remaining rate limit tokens", remaining);
}
// Log the name of any denied bots
if (result.reason.isBot()) {
if (result.isDenied()) {
console.log("Detected bot", reason.denied);
}
}
}
For each request, Arcjet returns additional data about the IP address. The available fields depends on the pricing plan you're on. The free plan includes country and IP type e.g. VPN or proxy.
if (decision.ip.hasCountry()) {
console.log("Country", decision.ip.countryName);
}
if (decision.ip.isVpn()) {
console.log("VPN detected");
}
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.
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.
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.
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.
Licensed under the Apache License, Version 2.0.