### Setup and Run React Router Middleware Example Source: https://github.com/arcjet/arcjet-js/blob/main/examples/react-router-middleware/README.md Commands to install dependencies, build the project, and start the development server. ```bash cd arcjet-js npm ci && npm run build cd examples/react-router-middleware/ npm ci ``` ```bash npm run dev ``` -------------------------------- ### Initialize and run the Nuxt example Source: https://github.com/arcjet/arcjet-js/blob/main/examples/nuxt/README.md Commands to install dependencies and start the development server for the Nuxt example. ```sh cd arcjet-js npm ci cd examples/nuxt/ npm ci ``` ```sh npm run dev ``` -------------------------------- ### Install example dependencies Source: https://github.com/arcjet/arcjet-js/blob/main/examples/astro/README.md Navigate to the example directory and install its specific dependencies. ```bash cd examples/astro npm ci ``` -------------------------------- ### Install Example Dependencies Source: https://github.com/arcjet/arcjet-js/blob/main/examples/bun-rate-limit/README.md Navigate to the example directory and install its specific dependencies. ```bash cd examples/bun-rate-limit bun install ``` -------------------------------- ### Install Example Dependencies Source: https://github.com/arcjet/arcjet-js/blob/main/examples/fastify/README.md Install the specific dependencies required for the Fastify example. ```sh cd examples/fastify/ npm ci ``` -------------------------------- ### Setup and Run Arcjet React Router Example Source: https://github.com/arcjet/arcjet-js/blob/main/examples/react-router/README.md Commands to prepare the environment and start the development server for the React Router example. ```sh cd arcjet-js npm ci && npm run build cd examples/react-router/ npm ci ``` ```sh npm run dev ``` -------------------------------- ### Install example dependencies Source: https://github.com/arcjet/arcjet-js/blob/main/examples/nextjs-server-actions/README.md Installs the specific dependencies required for the Next.js server actions example. ```bash cd examples/nextjs-server-actions npm ci ``` -------------------------------- ### Install the package Source: https://github.com/arcjet/arcjet-js/blob/main/sensitive-info-rampart/README.md Install the dependency via npm. ```shell npm install @arcjet/sensitive-info-rampart ``` -------------------------------- ### Install dependencies Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-guard/CONTRIBUTING.md Install project dependencies using npm. ```sh npm ci ``` -------------------------------- ### Install stable-hash Source: https://github.com/arcjet/arcjet-js/blob/main/stable-hash/README.md Install the package using npm in a Node.js environment. ```sh npm install @arcjet/stable-hash ``` -------------------------------- ### Install @nosecone/sveltekit Source: https://github.com/arcjet/arcjet-js/blob/main/nosecone-sveltekit/README.md Install the package using npm in a Node.js environment. ```sh npm install @nosecone/sveltekit ``` -------------------------------- ### Install @arcjet/decorate Source: https://github.com/arcjet/arcjet-js/blob/main/decorate/README.md Install the package using npm. ```sh npm install @arcjet/decorate ``` -------------------------------- ### Start the Deno server Source: https://github.com/arcjet/arcjet-js/blob/main/examples/deno-sensitive-info/README.md Execute the start task to launch the Deno server. ```bash deno task start ``` -------------------------------- ### Navigate to example directory Source: https://github.com/arcjet/arcjet-js/blob/main/examples/deno-sensitive-info/README.md Change the working directory to the Deno sensitive info example folder. ```bash cd examples/deno-sensitive-info ``` -------------------------------- ### Install @arcjet/sprintf Source: https://github.com/arcjet/arcjet-js/blob/main/sprintf/README.md Install the package using npm for Node.js environments. ```sh npm install @arcjet/sprintf ``` -------------------------------- ### Install SDK Dependencies Source: https://github.com/arcjet/arcjet-js/blob/main/examples/bun-rate-limit/README.md Run these commands from the project root to install dependencies and build the SDK. ```bash bun install && bun run build ``` -------------------------------- ### Install Nosecone Source: https://github.com/arcjet/arcjet-js/blob/main/nosecone/README.md Use npm to install the package in a Node.js environment. ```sh npm install nosecone ``` -------------------------------- ### Start the Server Source: https://github.com/arcjet/arcjet-js/blob/main/examples/express-newman/README.md Launch the Express server to begin testing. ```bash npm start ``` -------------------------------- ### Install and Build Project Dependencies Source: https://github.com/arcjet/arcjet-js/blob/main/examples/nextjs-sensitive-info/README.md Commands to install dependencies and build the project from the root directory. ```bash npm ci && npm run build ``` ```bash cd examples/nextjs-sensitive-info npm ci ``` -------------------------------- ### Install SDK dependencies Source: https://github.com/arcjet/arcjet-js/blob/main/examples/astro/README.md Run this command from the project root to install necessary SDK dependencies. ```bash npm ci && npm run build ``` -------------------------------- ### Start development server Source: https://github.com/arcjet/arcjet-js/blob/main/examples/astro/README.md Launch the Astro development server to test the integration. ```bash npm run dev ``` -------------------------------- ### Install Arcjet Framework SDKs Source: https://github.com/arcjet/arcjet-js/blob/main/README.md Install the specific Arcjet package for your framework. ```sh npm i @arcjet/next ``` ```sh npm i @arcjet/node ``` ```sh bun add @arcjet/bun ``` ```sh deno add npm:@arcjet/deno ``` ```sh npm i @arcjet/fastify ``` ```sh npm i @arcjet/nest ``` ```sh npm i @arcjet/nuxt ``` ```sh npm i @arcjet/remix ``` ```sh npm i @arcjet/react-router ``` ```sh npm i @arcjet/sveltekit ``` ```sh npm i @arcjet/astro ``` -------------------------------- ### Install @arcjet/protocol Source: https://github.com/arcjet/arcjet-js/blob/main/protocol/README.md Use npm to install the package in a Node.js environment. ```sh npm install @arcjet/protocol ``` -------------------------------- ### Start the Bun Server Source: https://github.com/arcjet/arcjet-js/blob/main/examples/bun-rate-limit/README.md Launch the server with hot reloading enabled. ```bash bun run --hot index.ts ``` -------------------------------- ### Start the development server Source: https://github.com/arcjet/arcjet-js/blob/main/examples/nestjs/README.md Launch the NestJS server in development mode. ```bash npm run start:dev ``` -------------------------------- ### Install @arcjet/logger Source: https://github.com/arcjet/arcjet-js/blob/main/logger/README.md Install the package using npm for Node.js environments. ```sh npm install @arcjet/logger ``` -------------------------------- ### Install Arcjet Guard Source: https://github.com/arcjet/arcjet-js/blob/main/README.md Install the guard package for guard protection. ```sh npm i @arcjet/guard ``` -------------------------------- ### Install @arcjet/duration Source: https://github.com/arcjet/arcjet-js/blob/main/duration/README.md Install the package using npm for Node.js environments. ```sh npm install @arcjet/duration ``` -------------------------------- ### Parse duration examples Source: https://github.com/arcjet/arcjet-js/blob/main/duration/README.md Examples showing how different duration strings are parsed into seconds. ```ts console.log(parse("1s")); // => 1 console.log(parse("1m")); // => 60 console.log(parse("1h")); // => 3600 console.log(parse("1d")); // => 86400 ``` -------------------------------- ### Install AI SDK dependencies Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-next/README.md Install the Vercel AI SDK and the OpenAI provider required for the chat route. ```sh npm install ai @ai-sdk/openai ``` -------------------------------- ### Install @arcjet/ip Source: https://github.com/arcjet/arcjet-js/blob/main/ip/README.md Install the package using npm for Node.js environments. ```sh npm install @arcjet/ip ``` -------------------------------- ### Install @arcjet/cache Source: https://github.com/arcjet/arcjet-js/blob/main/cache/README.md Install the package using npm for Node.js environments. ```sh npm install @arcjet/cache ``` -------------------------------- ### Install @arcjet/headers Source: https://github.com/arcjet/arcjet-js/blob/main/headers/README.md Install the package using npm for use in Node.js environments. ```sh npm install @arcjet/headers ``` -------------------------------- ### Install Arcjet Bun package Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-bun/README.md Add the Arcjet Bun SDK to your project dependencies. ```sh bun add @arcjet/bun ``` -------------------------------- ### Install @arcjet/body Source: https://github.com/arcjet/arcjet-js/blob/main/body/README.md Install the package using npm for Node.js environments. ```sh npm install @arcjet/body ``` -------------------------------- ### Install @arcjet/runtime Source: https://github.com/arcjet/arcjet-js/blob/main/runtime/README.md Install the package using npm for use in Node.js environments. ```sh npm install @arcjet/runtime ``` -------------------------------- ### Install Arcjet Node.js package Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-node/README.md Install the core Arcjet package for Node.js applications. ```sh npm install @arcjet/node ``` -------------------------------- ### Install @arcjet/analyze Source: https://github.com/arcjet/arcjet-js/blob/main/analyze/README.md Install the @arcjet/analyze package using npm. This package is ESM only. ```sh npm install @arcjet/analyze ``` -------------------------------- ### Install Arcjet Fastify Package Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-fastify/README.md Install the required npm package for Fastify integration. ```sh npm install @arcjet/fastify ``` -------------------------------- ### Quick Start with Arcjet Guard Source: https://github.com/arcjet/arcjet-js/blob/main/README.md Initialize the client and configure reusable rules for use within tool call handlers. ```ts import { launchArcjet, tokenBucket, detectPromptInjection, } from "@arcjet/guard"; // Create the Arcjet client once at module scope const arcjet = launchArcjet({ key: process.env.ARCJET_KEY! }); // Configure reusable rules const limitRule = tokenBucket({ refillRate: 10, intervalSeconds: 60, maxTokens: 100, }); const piRule = detectPromptInjection(); // Per request — create rule inputs each time async function handleToolCall( userId: string, userMessage: string, tokenCount: number, ) { const rl = limitRule({ key: userId, requested: tokenCount }); const decision = await arcjet.guard({ label: "tools.weather", rules: [rl, piRule(userMessage)], }); if (decision.conclusion === "DENY") { throw new Error(`Blocked: ${decision.reason}`); } // safe to proceed } ``` -------------------------------- ### Install Arcjet Next.js Package Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-next/README.md Install the required npm package for Next.js integration. ```sh npm install @arcjet/next ``` -------------------------------- ### Install Arcjet Remix Package Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-remix/README.md Install the necessary dependency for integrating Arcjet into a Remix project. ```sh npm install @arcjet/remix ``` -------------------------------- ### Install @arcjet/transport Source: https://github.com/arcjet/arcjet-js/blob/main/transport/README.md Use npm to install the package in a Node.js environment. ```sh npm install @arcjet/transport ``` -------------------------------- ### Install @arcjet/redact Source: https://github.com/arcjet/arcjet-js/blob/main/redact/README.md Install the package using npm for Node.js environments. ```sh npm install @arcjet/redact ``` -------------------------------- ### Install @arcjet/redact-wasm Source: https://github.com/arcjet/arcjet-js/blob/main/redact-wasm/README.md Install the package using npm for Node.js environments. ```sh npm install @arcjet/redact-wasm ``` -------------------------------- ### Build project Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-guard/CONTRIBUTING.md Bundle source code and generate type declarations. ```sh npm run build ``` -------------------------------- ### Run all checks Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-guard/CONTRIBUTING.md Execute build, lint, and unit tests. ```sh npm test ``` -------------------------------- ### Manage Client Instances Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-deno/README.md Create a single client instance for base rules and extend it per-route using withRule() to optimize performance. ```ts // lib/arcjet.ts — create once, import everywhere import arcjet, { shield } from "npm:@arcjet/deno"; export default arcjet({ key: Deno.env.get("ARCJET_KEY")!, rules: [ shield({ mode: "LIVE" }), // base rules applied to every request ], }); ``` ```ts // routes/chat.ts — extend per-route with withRule() import aj from "./lib/arcjet.ts"; import { detectBot, tokenBucket } from "npm:@arcjet/deno"; const routeAj = aj.withRule(detectBot({ mode: "LIVE", allow: [] })).withRule( tokenBucket({ mode: "LIVE", refillRate: 2_000, interval: "1h", capacity: 5_000, }), ); ``` -------------------------------- ### Install @arcjet/env Source: https://github.com/arcjet/arcjet-js/blob/main/env/README.md Install the package using npm for Node.js environments. ```sh npm install @arcjet/env ``` -------------------------------- ### Use MemoryCache Source: https://github.com/arcjet/arcjet-js/blob/main/cache/README.md Demonstrates initializing the cache, setting a value with a TTL, and retrieving it before and after expiration. ```ts import { setTimeout } from "node:timers/promises"; import { MemoryCache } from "@arcjet/cache"; const cache = new MemoryCache(); cache.set("namespace", "key", "value", 2); console.log(await cache.get("namespace", "key")); // => [ 'value', 2 ] await setTimeout(2001); console.log(await cache.get("namespace", "key")); // => [ undefined, 0 ] ``` -------------------------------- ### Implement a Custom Adapter Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet/README.md Demonstrates the minimal interface required for a custom integration, bypassing standard framework adapters. ```ts import http from "node:http"; import { readBody } from "@arcjet/body"; import arcjet, { ArcjetAllowDecision, ArcjetReason, shield } from "arcjet"; const arcjetKey = process.env.ARCJET_KEY; if (!arcjetKey) { throw new Error("Cannot find `ARCJET_KEY` environment variable"); } const aj = arcjet({ // Your adapter takes care of this: this is a naïve example. client: { async decide() { return new ArcjetAllowDecision({ reason: new ArcjetReason(), results: [], ttl: 0, }); }, report() {}, }, key: arcjetKey, log: console, rules: [shield({ mode: "LIVE" })], }); const server = http.createServer(async function ( request: http.IncomingMessage, response: http.ServerResponse, ) { const url = new URL(request.url || "", "http://" + request.headers.host); // Your adapter takes care of this: this is a naïve example. const context = { getBody() { return readBody(request, { limit: 1024 }); }, host: request.headers.host, ip: request.socket.remoteAddress, method: request.method, path: url.pathname, }; const decision = await aj.protect(context, {}); if (decision.isDenied()) { response.writeHead(403, { "Content-Type": "application/json" }); response.end(JSON.stringify({ message: "Forbidden" })); return; } response.writeHead(200, { "Content-Type": "application/json" }); response.end(JSON.stringify({ message: "Hello world" })); }); server.listen(8000); ``` -------------------------------- ### Initialize and use Logger Source: https://github.com/arcjet/arcjet-js/blob/main/logger/README.md Create a logger instance with a specified level and use it to output formatted messages. ```ts import { Logger } from "@arcjet/logger"; const logger = new Logger({ level: "debug" }); logger.debug("only printed in debug mode"); // Logs ✦Aj DEBUG only printed in debug mode logger.error("%d + %d = %d", 1, 2, 3); // Logs ✦Aj ERROR 1 + 2 = 3 ``` -------------------------------- ### Install Arcjet Nest Package Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-nest/README.md Install the required npm package for NestJS integration. ```sh npm install @arcjet/nest ``` -------------------------------- ### Configure bot protection in Next.js Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-next/README.md Sets up bot detection rules with an allow list and demonstrates how to handle denied requests and verify bot authenticity. ```ts import arcjet, { detectBot } from "@arcjet/next"; import { isSpoofedBot } from "@arcjet/inspect"; import { NextResponse } from "next/server"; const aj = arcjet({ key: process.env.ARCJET_KEY!, rules: [ detectBot({ mode: "LIVE", // Blocks requests. Use "DRY_RUN" to log only allow: [ "CATEGORY:SEARCH_ENGINE", // Google, Bing, etc // Uncomment to allow these other common bot categories: // "CATEGORY:MONITOR", // Uptime monitoring services // "CATEGORY:PREVIEW", // Link previews e.g. Slack, Discord // See the full list at https://arcjet.com/bot-list ], }), ], }); export async function GET(request: Request) { const decision = await aj.protect(request); if (decision.isDenied() && decision.reason.isBot()) { return NextResponse.json({ error: "No bots allowed" }, { status: 403 }); } // Verifies the authenticity of common bots using IP data. // Verification isn't always possible, so check the results separately. // https://docs.arcjet.com/bot-protection/reference#bot-verification if (decision.results.some(isSpoofedBot)) { return NextResponse.json({ error: "Forbidden" }, { status: 403 }); } return NextResponse.json({ message: "Hello world" }); } ``` -------------------------------- ### Install Arcjet Astro Package Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-astro/README.md Install the required npm package for Astro integration. ```sh npm install @arcjet/astro ``` -------------------------------- ### Run Deno tests Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-deno/README.md Executes the test suite for the package using Deno with required environment and network permissions. ```sh deno test test/index.test.ts --allow-env --allow-net --no-check ``` -------------------------------- ### Install @arcjet/inspect Source: https://github.com/arcjet/arcjet-js/blob/main/inspect/README.md Install the package using npm for Node.js environments. Note that this package is ESM only. ```sh npm install @arcjet/inspect ``` -------------------------------- ### Avoid Helper Functions for Client Initialization Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-guard/README.md Do not wrap launchArcjet in a helper function, as this creates a new client and connection on every call. ```ts // Bad — creates a new client (and connection) every call function getArcjet() { return launchArcjet({ key: process.env.ARCJET_KEY! }); } const decision = await getArcjet().guard({ ... }); // Good — reuses the client const arcjet = launchArcjet({ key: process.env.ARCJET_KEY! }); const decision = await arcjet.guard({ ... }); ``` -------------------------------- ### new Logger(options) Source: https://github.com/arcjet/arcjet-js/blob/main/logger/README.md Creates a new instance of the Logger with the specified configuration options. ```APIDOC ## new Logger(options) ### Description Creates a new logger instance configured with the provided options. ### Parameters - **options** (Options) - Required - Configuration object containing the log level. ### Returns A Logger instance. ``` -------------------------------- ### Initialize Arcjet Client and Rules Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-guard/README.md Create the client and rule configurations at the module scope to ensure persistent connection reuse and stable rule IDs. ```ts // Create the client once at module scope const arcjet = launchArcjet({ key: process.env.ARCJET_KEY! }); // Configure reusable rules (also at module scope) const limitRule = tokenBucket({ refillRate: 10, intervalSeconds: 60, maxTokens: 100 }); // Per request — created each time const decision = await arcjet.guard({ label: "tools.weather", rules: [limitRule({ key: userId })], }); ``` -------------------------------- ### Configure proxy and handle errors Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-astro/README.md Set up proxy IP resolution and implement explicit error handling for protect() results. ```js arcjet({ rules: [], proxies: ["100.100.100.100"] }); ``` ```ts if (decision.isErrored()) { console.error("Arcjet error", decision.reason.message); // allow the request to proceed } ``` -------------------------------- ### Install Arcjet React Router Package Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-react-router/README.md Install the required npm package for React Router integration. ```sh npm install @arcjet/react-router ``` -------------------------------- ### Install @arcjet/analyze-wasm Source: https://github.com/arcjet/arcjet-js/blob/main/analyze-wasm/README.md Install the package using npm. This package is ESM only and intended for use in Node.js environments. ```sh npm install @arcjet/analyze-wasm ``` -------------------------------- ### Run tests for the Bun package Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-bun/README.md Execute the test suite for the package within the Bun environment. ```sh bun test ``` -------------------------------- ### Implement server hooks Source: https://github.com/arcjet/arcjet-js/blob/main/nosecone-sveltekit/README.md Use createHook in your server hooks file to apply security headers. ```ts import { createHook } from "@nosecone/sveltekit"; import { sequence } from "@sveltejs/kit/hooks"; export const handle = sequence( createHook(), // ... other hooks can go here ); ``` -------------------------------- ### Manage Arcjet client instances Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-bun/README.md Create a single client instance for base rules and extend it per-route using withRule() to optimize performance. ```ts // lib/arcjet.ts — create once, import everywhere import arcjet, { shield } from "@arcjet/bun"; import { env } from "bun"; export default arcjet({ key: env.ARCJET_KEY!, rules: [ shield({ mode: "LIVE" }), // base rules applied to every request ], }); ``` ```ts // routes/chat.ts — extend per-route with withRule() import aj from "./lib/arcjet.js"; import { detectBot, tokenBucket } from "@arcjet/bun"; const routeAj = aj.withRule(detectBot({ mode: "LIVE", allow: [] })).withRule( tokenBucket({ mode: "LIVE", refillRate: 2_000, interval: "1h", capacity: 5_000, }), ); export async function handler(req: Request) { const decision = await routeAj.protect(req, { requested: 500 }); // ... } ``` -------------------------------- ### View API response headers for preview bot Source: https://github.com/arcjet/arcjet-js/blob/main/examples/nextjs-bot-categories/README.md Expected headers for a detected preview bot. ```txt x-arcjet-bot-allowed: VERCEL_MONITOR_PREVIEW x-arcjet-bot-denied: ``` -------------------------------- ### MemoryCache#get Source: https://github.com/arcjet/arcjet-js/blob/main/cache/README.md Retrieves a value from the cache along with its remaining TTL. ```APIDOC ## MemoryCache#get(namespace: string, key: string): Promise<[T | undefined, number]> ### Description Attempts to retrieve a value from the cache. If a value exists, it will be returned with the remaining time-to-live (in seconds). ### Parameters - **namespace** (string) - Required - The namespace for the cache entry. - **key** (string) - Required - The key for the cache entry. ``` -------------------------------- ### fixedWindow(options) Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet/README.md Fixed window rate limit that resets the counter at the start of each window. ```APIDOC ## fixedWindow(options) ### Description Fixed window rate limit. Resets the counter at the start of each window. ### Parameters - **mode** (string) - Required - The operation mode: "LIVE" or "DRY_RUN". - **window** (string) - Required - Window duration (e.g., "1m"). - **max** (number) - Required - Maximum requests per window. ``` -------------------------------- ### Configure Fixed Window Rate Limiting Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet/README.md Resets the request counter at the start of each defined window. ```ts import arcjet, { fixedWindow } from "arcjet"; const aj = arcjet({ // ... rules: [ fixedWindow({ mode: "LIVE", window: "1m", // Window duration max: 100, // Maximum requests per window }), ], }); ``` -------------------------------- ### Add Arcjet Skill to AI Agent Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-fastify/README.md Install the Arcjet skill to provide documentation to your coding agent. ```sh npx skills add arcjet/skills ``` -------------------------------- ### Read a stream body Source: https://github.com/arcjet/arcjet-js/blob/main/body/README.md Example of reading a file stream using the readBody utility with a size limit. ```ts import fs from "node:fs"; import { readBody } from "@arcjet/body"; const body = await readBody(fs.createReadStream("example.ts"), { limit: 1024 }); console.log(body); ``` -------------------------------- ### Test bot protection Source: https://github.com/arcjet/arcjet-js/blob/main/examples/nestjs/README.md Execute a GET request using curl to trigger the bot protection rule. ```bash curl http://localhost:3000/bot ``` -------------------------------- ### Lint code Source: https://github.com/arcjet/arcjet-js/blob/main/arcjet-guard/CONTRIBUTING.md Run linting with oxlint. ```sh npm run lint ```