Skip to content

Get started with Astro

Arcjet is the runtime security platform that ships with your code. Enforce budgets, stop prompt injection, detect bots, and protect personal information with Arcjet’s AI security building blocks.

This guide shows you how to protect an application with Arcjet by blocking automated clients that inflate costs and enforcing per-user token budgets.

In your project root, run the following:

This automatically installs and configures the Arcjet Astro integration in your project. Learn more about how this works in the Astro docs . Alternatively, you can follow the manual installation instructions.

Manual installation instruction

In your project root, install the integration:

Update your Astro configuration file:

astro.config.mjs
import { defineConfig } from "astro/config";
import node from "@astrojs/node";
import arcjet from "@arcjet/astro";
// https://astro.build/config
export default defineConfig({
adapter: node({
mode: "standalone",
}),
env: {
// We recommend enabling secret validation
validateSecrets: true,
},
integrations: [
// Add the Arcjet Astro integration
arcjet(),
],
});

Create a free Arcjet account then follow the instructions to add a site and get a key.

The Arcjet Astro integration reads your Arcjet key from the ARCJET_KEY environment variable. During development, add your key to a .env.local file in your project root. Astro automatically loads the environment variables from this file. Learn more in the Astro docs.

.env.local
# Get your site key from https://console.arcjet.com
ARCJET_KEY=ajkey_yourkey
ARCJET_ENV=development

This configures Arcjet to protect your AI application: block automated clients that inflate costs, and enforce per-user token budgets.

Update your Astro configuration with the contents:

astro.config.mjs
import { defineConfig } from "astro/config";
import node from "@astrojs/node";
import arcjet, { shield, detectBot, tokenBucket } from "@arcjet/astro";
export default defineConfig({
adapter: node({
mode: "standalone",
}),
env: {
validateSecrets: true,
},
integrations: [
arcjet({
rules: [
// Shield protects your app from common attacks such as 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 such as Slack, Discord
],
}),
// Create a token bucket rate limit. Other algorithms are supported.
tokenBucket({
mode: "LIVE",
// Tracked by IP address by default, but this can be customized
// See https://docs.arcjet.com/fingerprints
//characteristics: ["ip.src"],
refillRate: 5, // Refill 5 tokens per interval
interval: 10, // Refill every 10 seconds
capacity: 10, // Bucket capacity of 10 tokens
}),
],
}),
],
});

This creates an Arcjet instance that can be used to protect your routes. It can be imported from the arcjet:client module in your project.

4. Start app

Visit http://localhost:4321/api.json in your browser and refresh a few times to hit the rate limit.

Wait 10 seconds, then run:

Terminal window
curl -v http://localhost:4321/api.json

The wait is necessary because the decision is cached for your IP based on the interval rate limit configuration.

You get a 403 response because curl is considered a bot by default (customizable).

The requests also appear in the Arcjet dashboard.

Do I need to run any infrastructure, such as 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 Cloud 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 Cloud 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, such as blocking a client, Arcjet will continue to function even if the service is unavailable.

If a call to the Cloud 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, such as the customer ID or the 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.

Need help with anything? Email us or join our Discord to get support from our engineering team.