Bot protection for Go
Arcjet bot detection lets you manage traffic by automated clients and bots.
What is Arcjet?
Arcjet is the AI agent runtime security platform. Discover the agents running in your organization, enforce policy across every action, prompt, and tool call, and keep the evidence to prove what happened. Detect prompt injection, authorize agent tool calls, redact PII, and block bots and abuse.Quick start
Section titled “Quick start”This guide shows you how to protect your
1. Install Arcjet
Section titled “1. Install Arcjet”In your project root, install the SDK:
In your project root, add the Arcjet Go SDK:
go get github.com/arcjet/arcjet-goThe SDK requires Go 1.25 or later.
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 ARCJET_KEY in your environment. NewClient and NewGuardClient
read it when Key is empty. The Go SDK does not read
ARCJET_ENV.
# Arcjet key for your site (from <https://console.arcjet.com>).# More info: <https://docs.arcjet.com/environment#arcjet-key>.ARCJET_KEY=ajkey_yourkey3. Add bot protection
Section titled “3. Add bot protection”Create a new file at main.go with the contents:
package main
import ( "encoding/json" "log" "net/http" "os"
"github.com/arcjet/arcjet-go")
var aj = must(arcjet.NewClient(arcjet.Config{ Key: os.Getenv("ARCJET_KEY"), Rules: []arcjet.Rule{ arcjet.DetectBot(arcjet.BotOptions{ Mode: arcjet.ModeLive, // Blocks requests. Use ModeDryRun to log only. Allow: []string{ arcjet.BotCategorySearchEngine, // Google, Bing, etc. // arcjet.BotCategoryMonitor, // Uptime monitoring // arcjet.BotCategoryPreview, // Link previews (Slack, Discord) }, }), },}))
func handler(w http.ResponseWriter, r *http.Request) { decision, err := aj.Protect(r.Context(), r) if err != nil { log.Printf("arcjet: %v", err) }
if decision.IsDenied() { http.Error(w, "Forbidden", http.StatusForbidden) return }
// Requests from hosting IPs are often bots. Allow them on API endpoints // if your clients run in data centers. // https://docs.arcjet.com/blueprints/vpn-proxy-detection if decision.IP.IsHosting { http.Error(w, "Forbidden", http.StatusForbidden) return }
if decision.IsSpoofedBot() { http.Error(w, "Forbidden", http.StatusForbidden) return }
_ = json.NewEncoder(w).Encode(map[string]string{"message": "Hello world"})}
func main() { http.HandleFunc("/", handler) log.Fatal(http.ListenAndServe(":8000", nil))}
func must[T any](v T, err error) T { if err != nil { log.Fatal(err) } return v}4. Start server
go run .Then make a curl request. You get a 403 Forbidden response because curl
is considered an automated client by default. For more information about
allowing it, see
Identifying bots.
curl -i http://localhost:8000The requests also appear in the Arcjet dashboard.
5. Choose bots to allow or deny
Section titled “5. Choose bots to allow or deny”Arcjet maintains a list of known bots so you can choose to either only
allow specific bots
or only
deny specific bots.
Pass exactly one of allow or deny on each rule. An empty allow list
blocks every detected bot.
Bots can be identified individually or through managed categories. For more details, see Identifying bots.
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.
What next?
Section titled “What next?”Explore
Section titled “Explore”Arcjet can be used with specific rules on individual routes or as general protection on your entire application. You can set up rate limiting for your API, minimize fraudulent registrations with the signup form protection and more.
Get help
Section titled “Get help”Need help with anything? Email support@arcjet.com to get support from our engineering team.