Get started with Python + FastAPI
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:
mkdir arcjet-fastapicd arcjet-fastapiuv inituv add arcjet fastapi uvicorn langchain langchain-openaiRequirements
Section titled “Requirements”- CPython 3.10 or later on macOS, Windows, and glibc Linux (Debian, Ubuntu,
RHEL, and
*-slim/ manylinux container images) - Alpine Linux and other musl-based systems aren’t supported. Prefer a glibc
container image such as
python:3.10-slimorastral/uv:python3.10-trixie-slim
Two runtime dependencies ship native code:
Those packages publish musllinux wheels, so pip install arcjet can succeed
on Alpine without a compiler. Alpine/musl isn’t a supported target. A missing
wheel forces a source build that needs Rust and a C toolchain.
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.
Set your environment variables:
# Export your Arcjet API key from https://console.arcjet.comARCJET_KEY="ajkey_..."ARCJET_ENV=development
# Export your OpenAI API key (used by LangChain)OPENAI_API_KEY="sk-..."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.
Create a new file at main.py with the contents:
import loggingimport os
from arcjet import ( Mode, arcjet, detect_bot, detect_prompt_injection, shield, token_bucket,)from fastapi import FastAPI, Requestfrom fastapi.responses import JSONResponsefrom langchain_core.output_parsers import StrOutputParserfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_openai import ChatOpenAIfrom pydantic import BaseModel
app = FastAPI()
logging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)
arcjet_key = os.getenv("ARCJET_KEY")if not arcjet_key: raise RuntimeError("ARCJET_KEY is required. Get one at https://console.arcjet.com")
openai_api_key = os.getenv("OPENAI_API_KEY")if not openai_api_key: raise RuntimeError( "OPENAI_API_KEY is required. Get one at https://platform.openai.com" )
llm = ChatOpenAI(model="gpt-4o-mini", api_key=openai_api_key)
prompt = ChatPromptTemplate.from_messages( [ ("system", "You are a helpful assistant."), ("human", "{message}"), ])
chain = prompt | llm | StrOutputParser()
class ChatRequest(BaseModel): message: str
aj = arcjet( key=arcjet_key, # Get your key from https://console.arcjet.com rules=[ # Shield protects your app from common attacks e.g. SQL injection shield(mode=Mode.LIVE), # Create a bot detection rule detect_bot( mode=Mode.LIVE, # An empty allow list blocks all bots, which is a good default for # an AI chat app allow=[ "CURL", # Allow curl so we can test it # Uncomment to allow these other common bot categories # See the full list at https://arcjet.com/bot-list # BotCategory.MONITOR, # Uptime monitoring services # BotCategory.PREVIEW, # Link previews e.g. Slack, Discord ], ), # Create a token bucket rate limit. Other algorithms are supported token_bucket( # Track budgets by arbitrary characteristics of the request. Here # we use user ID, but you could pass any value. Removing this will # fall back to IP-based rate limiting. characteristics=["userId"], mode=Mode.LIVE, refill_rate=5, # Refill 5 tokens per interval interval=10, # Refill every 10 seconds capacity=10, # Bucket capacity of 10 tokens ), # Detect prompt injection attacks before they reach your AI model detect_prompt_injection( mode=Mode.LIVE, # Blocks requests. Use Mode.DRY_RUN to log only ), ],)
@app.post("/chat")async def chat(request: Request, body: ChatRequest): # Replace with actual user ID from the user session userId = "your_user_id"
# Call protect() to evaluate the request against the rules decision = await aj.protect( request, # Deduct 5 tokens from the bucket requested=5, # Identify the user for rate limiting purposes characteristics={"userId": userId}, # Check the user message for prompt injection detect_prompt_injection_message=body.message, )
# Handle denied requests if decision.is_denied(): if decision.reason_v2.type == "PROMPT_INJECTION": return JSONResponse( {"error": "Prompt injection detected — please rephrase your message"}, status_code=400, ) status = 429 if decision.reason_v2.type == "RATE_LIMIT" else 403 return JSONResponse({"error": "Denied"}, status_code=status)
# All rules passed, proceed with handling the request reply = await chain.ainvoke({"message": body.message})
return {"reply": reply}4. Start app
uv run uvicorn main:app --reloadAnd send a message to the API endpoint:
curl -X POST http://localhost:8000/chat \ -H "Content-Type: application/json" \ -d '{"message": "What is the capital of France?"}'Requests appear in your Arcjet dashboard in real time.
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.