Get started with Node.js + Hono
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.
1. Install Arcjet
Section titled “1. Install Arcjet”In your project root, run the following:
npm i @arcjet/node @arcjet/inspectpnpm add @arcjet/node @arcjet/inspectyarn add @arcjet/node @arcjet/inspectRequirements
Section titled “Requirements”- Node.js 22.21.0 or later
- Hono 4.3 or later
- CommonJS is not supported. Arcjet is ESM only.
2. Set your key
Section titled “2. Set your key”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.
You can also set
ARCJET_ENV
and other values in .env files.
# Run Arcjet in development <https://docs.arcjet.com/environment#arcjet-env>.ARCJET_ENV=development# Arcjet key for your site (from <https://console.arcjet.com>).# More info: <https://docs.arcjet.com/environment#arcjet-key>.ARCJET_KEY=ajkey_yourkeyNext you need to update the dev command in your package.json to use the
.env.local file.
{ "scripts": { "dev": "tsx watch --env-file .env.local src/index.ts" } ...}3. Configure
Section titled “3. Configure”This configures Arcjet to protect your AI application: block automated clients that inflate costs, and enforce per-user token budgets.
Update your index.ts file with the contents:
import arcjet, { detectBot, shield, tokenBucket } from "@arcjet/node";import { isSpoofedBot } from "@arcjet/inspect";import { serve, type HttpBindings } from "@hono/node-server";import { Hono } from "hono";
const aj = arcjet({ key: process.env.ARCJET_KEY!, 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", // 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 }), ],});
const app = new Hono<{ Bindings: HttpBindings }>();
app.get("/", async (c) => { const decision = await aj.protect(c.env.incoming, { requested: 5 }); // Deduct 5 tokens from the bucket console.log("Arcjet decision", decision);
if (decision.isDenied()) { if (decision.reason.isRateLimit()) { return c.json({ error: "Too Many Requests" }, 429); } else if (decision.reason.isBot()) { return c.json({ error: "No Bots Allowed" }, 403); } else { return c.json({ error: "Forbidden" }, 403); } }
// Requests from hosting IPs are likely from bots, so they can usually be // blocked. However, consider your use case - if this is an API endpoint // then hosting IPs might be legitimate. // https://docs.arcjet.com/blueprints/vpn-proxy-detection if (decision.ip.isHosting()) { return c.json({ error: "Forbidden" }, 403); }
// Paid Arcjet accounts include additional verification checks 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(isSpoofedBot)) { return c.json({ error: "Forbidden" }, 403); }
return c.json({ message: "Hello Hono!" });});
const port = 3000;console.log(`Server is running on port ${port}`);
serve({ fetch: app.fetch, port,});4. Start app
npm run devpnpm run devyarn run devVisit http://localhost:3000 in your browser and
refresh a few times to hit the rate limit.
Wait 10 seconds, then run:
curl -v http://localhost:3000The 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 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 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 e.g. 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 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?
Section titled “What next?”Get help
Section titled “Get help”Need help with anything? Email us or join our Discord to get support from our engineering team.