Arcjet
Contents
Add Arcjet Protection to Your App
Checklist
Step 1: Check Language Support
If the project's server-side code is not JavaScript, TypeScript, Python, or Go → tell the user in chat that Arcjet doesn't support their language yet. Don't modify the project, don't write a
, don't invent a package. Just say it and stop.
Step 2: Get an ARCJET_KEY into the project's env file
Before writing any code, the project needs a real
in its env file. Don't write Arcjet code first and "leave the key as a TODO" — that just produces dead code. Get the key first, then wire it up.
In order of preference:
- Arcjet CLI (preferred). Check whether you're already signed in, then retrieve a key.
- Arcjet MCP server (endpoint:
https://api.arcjet.com/mcp
) — for clients with built-in MCP. See references/mcp.md.
- Manual (last resort): tell the user to grab a key from https://app.arcjet.com.
CLI bootstrap (the normal path)
bash
npx -y @arcjet/cli@latest auth status # is the user already signed in?
# if not signed in:
npx -y @arcjet/cli@latest auth login # browser device flow, see references/cli.md
npx -y @arcjet/cli@latest teams list --output json --fields id,name
npx -y @arcjet/cli@latest sites list --team-id <team_id> --output json --fields id,name
# if no suitable site exists:
npx -y @arcjet/cli@latest sites create --team-id <team_id> --name "<project>"
npx -y @arcjet/cli@latest sites get-key --site-id <site_id> --output json --fields key
Write the
value to the project's env file as
. Match whatever the project already does — filename,
companion,
entry. If the project doesn't have a convention yet, default to whatever the framework expects and add the env file to
. Never hardcode the key in source.
See
references/cli.md for install options beyond
, agent-mode flags, and the full command reference.
Install the SDK with the project's package manager
Once you know which SDK you need (Step 3 below), install it via the package manager the project already uses —
,
,
,
,
,
,
,
, etc. Don't hand-edit
/
/
and guess a version: typed versions tend to be wrong (
doesn't exist for the Python SDK;
is stale for
; Go should use the module tag, not a copied pseudo-version), and the lockfile/module metadata won't get updated. Let the package manager pick the real version and pin it.
Step 3: Detect Protection Type and Read Reference
Determine which protection type applies:
| Request-based | Guard |
|---|
| When to use | Code has an HTTP request object (Express , Next.js , FastAPI , etc.) | No HTTP request (tool calls, MCP handlers, queue workers, background jobs, agent loops) |
| JS/TS SDK | , , , etc. | |
| Python SDK | (with / ) | (with / ) |
| Go SDK | github.com/arcjet/arcjet-go
(with ) | github.com/arcjet/arcjet-go
(with ) |
| Entry point | / | / |
A single project can use both — e.g. request-based on API routes and guard on agent tool calls.
Common misclassifications to watch for:
- MCP servers: the word "server" is misleading. MCP tools don't receive HTTP requests — they're invoked by an MCP client over stdio or SSE. Use Guard, not request-based.
- Background jobs / queue consumers: no HTTP request at the protection site. Use Guard.
- Server actions / RPC over HTTP (Next.js server actions, tRPC, etc.): there is an HTTP request underneath. Use request-based.
- Agent tool calls inside a request handler: if you want to limit per-user-per-route, request-based is fine. If you want per-tool budgets independent of any HTTP boundary, use Guard at the tool call site.
Read the appropriate reference:
- Request-based JS/TS: references/requests_javascript.md
- Request-based Python: references/requests_python.md
- Request-based Go: references/requests_go.md
- Guard JS/TS: references/guards_javascript.md
- Guard Python: references/guards_python.md
- Guard Go: references/guards_go.md
These references explain architectural decisions and patterns that can't be inferred from the source code alone. For exact API signatures, read the installed package's types and doc comments.
Step 4: Implement Protection
Follow the patterns in the reference file from Step 3. Key principles:
Request-based (HTTP routes):
- Create shared clients outside handlers and include Shield as a base rule. Use the exact constructor and rule names from the language reference.
- In JavaScript/TypeScript, use for route-specific rules and for the result.
- In Python, pass the complete rule list to / ; there is no client method. Check .
- In Go, create one at package scope. derives route-specific clients and returns , so handle initialization errors. Check .
- Call / inside each route handler (not in app-level middleware), once per request.
- Map denial reasons to HTTP responses. Only branch on reasons that produce a different response — there is no point in a Shield-specific arm that returns the same status as the default 403.
- Put the language's characteristic selector on the specific rule that needs it, then pass a trusted, authenticated user ID at protection time. Never rate limit by a client-controlled header unless a trusted proxy strips and rewrites it.
Guard (non-HTTP code):
- Client at module scope with (JS) or / (Python — pick async vs sync to match the function you're protecting).
- In Go, create one at package scope.
- Rules declared at module scope. Give each rule a meaningful so they show up usefully in the Console.
- One call per specific operation, with a hardcoded like or . Put it wherever you already know exactly what's happening — that can be inside the tool/task function itself, or right before calling it from a dispatch arm. Both work; pick whichever makes error propagation cleaner. What to avoid is the generic-dispatcher pattern (
handleToolCall(name, args)
calling guard(label=f"tools.{name}")
) — interpolated labels break grep and produce messy Console groupings.
- Label naming rules: labels are validated server-side as slugs — lowercase letters, digits, dash (), and dot () only, must start and end with a letter or digit, max 256 bytes. Underscores, uppercase, and slashes are rejected even though some SDK TSDoc comments claim otherwise. Use , not or .
- Pass on the call when you have useful auditing context (
metadata={"user_id": user_id, "request_id": ...}
). It appears in the Console alongside the decision.
- Branch on which rule denied, not just on . Use the per-rule accessors (e.g.
userLimit.deniedResult(decision)
for retry-after info) or the flat reason string (decision.reason === "PROMPT_INJECTION"
in JS, decision.reason == "PROMPT_INJECTION"
in Python) so the error you surface to the caller tells them why — "rate limited, retry in 12s" vs "input flagged as prompt injection" — instead of a generic "blocked." Note: guard's is a flat string literal, unlike the request-based SDK's tagged-helper API.
- Every rate-limit rule needs a and a :
- Per-user context (agent tool calls inside a logged-in session, queue jobs with a ): use the user/session id as the key.
- No user context (stdio MCP server, single-tenant worker): use a stable identifier you control — instance id, deployment name, or a literal like . Just be explicit.
- Check
decision.conclusion === "DENY"
(JS), decision.conclusion == "DENY"
(Python), or / decision.Conclusion == arcjet.ConclusionDeny
(Go) before proceeding.
Conventions outside the Arcjet flow
For everything that isn't an Arcjet-specific decision — dev scripts, file/module layout, named-vs-default exports, comment style, env-file naming, type hints, error class patterns — match the project's existing conventions. If the project has no convention yet, default to modern best practice for the language. This skill is opinionated about where Arcjet goes and how its API is used; it shouldn't reach further than that.
Step 5: Verify Decisions
After wiring up protection, confirm it's actually firing. Three steps:
1. Type-check / build first. Run
,
,
, or whatever check command the project uses. Catches wrong imports, wrong rule names, and stale type signatures before the user does.
2. Trigger a real call so a decision exists to check. Without one, the Console and CLI are empty and you can't tell whether protection is actually wired up.
- Request-based: start the dev server (,
uvicorn main:app --reload
, etc.) and the protected route. To trip a rate limit, loop the call: for i in {1..50}; do curl -s -o /dev/null -w "%{http_code}\n" http://localhost:3000/api/your-route; done
— you should see a mix of 200s and 429s once the limit is hit.
- Guard: invoke the protected function directly. A tiny script that imports the tool/task function and calls it twice (once to allow, once to exceed the limit) is usually the fastest path — e.g.
node -e "import('./src/tools.js').then(m => m.getWeather('SF', 'user_123'))"
or python -c "from worker import process_job; process_job({'user_id': 'user_123'})"
. For MCP servers, send a tool call via the MCP client / inspector. For queue workers, enqueue a real job. Don't try to test guard by ing anything — there's no HTTP surface.
3. Confirm the decision in the Arcjet platform.
- CLI:
npx -y @arcjet/cli@latest requests list --site-id <id>
(request-based) or ... guards list --site-id <id>
(Guard)
- MCP: /
- Console: https://app.arcjet.com
For deeper investigation:
arcjet requests explain --site-id <id> --request-id <id>
or
arcjet guards explain --site-id <id> --guard-id <id>
.
If you can't run the app in the current environment, tell the user exactly what to do (which command to run, what to look for in the output) instead of silently skipping verification.
Gotchas
- Wrong SDK/client: , , and Go's are for non-HTTP code. / / Python / Go are for HTTP routes. Using the wrong one is the most common mistake.
- Wrong placement: must not be called in Express middleware or Next.js middleware. Call it inside each route handler.
- Wrong layer for : don't put in a
handleToolCall(name, args)
dispatcher — put it inside each specific tool / task function so the and metadata can be hardcoded.
- Hand-edited dependency manifests: don't append to or to . Run the project's package manager so the version is real and the lockfile updates.
- Double-counting: Calling or multiple times for the same operation counts against rate limits multiple times.
- Never hardcode — always use environment variables.
Choosing Protections
When you need to pick which rules address the user's concern — bot abuse, rate limits, prompt injection, signup spam, PII, IP filtering, etc. — load references/choosing_protections.md. It maps common problems to Arcjet rules and explains the tradeoffs between strategies (e.g. token bucket vs sliding window). The mapping doesn't need to be in your context for the rest of the workflow.
Resources
For exact API signatures, parameter names, and the full set of rules and helpers, read the installed SDK's source — types and docstrings are the source of truth:
- Python SDK: https://github.com/arcjet/arcjet-py — package (request protection) and subpackage (non-HTTP guard).
- JavaScript / TypeScript SDK: https://github.com/arcjet/arcjet-js — monorepo with framework-specific packages (, , , , , etc.).
- Go SDK: https://github.com/arcjet/arcjet-go —
github.com/arcjet/arcjet-go
module with request and guard clients. The initial tagged release is .
- Docs: https://docs.arcjet.com — narrative guides, blueprints, and product reference.
- Console: https://app.arcjet.com — sites, keys, and decision history.