Skip to content

Get started: Bun + Hono and Arcjet

This guide will help you protect your Bun + Hono app using Arcjet.

What is Arcjet? Arcjet helps developers protect their apps in just a few lines of code. Implement rate limiting, bot protection, email validation, and defense against common attacks.

SDK installation

Follow these steps to get started. You can also find an example app on GitHub.

1. Install Arcjet

In your project root, run the following command to install the SDK:

Terminal window
bun add @arcjet/bun

2. Set your key

Create a free Arcjet account then follow the instructions to add a site and get a key. Add it to a .env.local file in your Bun project root.

Arcjet only accept non-local IP addresses with a fallback to 127.0.0.1 in development mode, and since Bun doesn’t set NODE_ENV for you like Node.js does, you also need to set ARCJET_ENV in your environment file.

.env.local
# Run Arcjet in development mode because Bun.sh doesn't explicitly
# set an environment variable like Node.js does
ARCJET_ENV=development
ARCJET_KEY=ajkey_yourkey

3. Add a rate limit to a route

The example below applies a token bucket rate limit rule to a route where we identify the user based on their ID e.g. if they are logged in. The bucket is configured with a maximum capacity of 10 tokens and refills by 5 tokens every 10 seconds. Each request consumes 5 tokens.

We also configure a Shield rule which adds protection against common attacks, such as SQL injection.

/index.ts
import arcjet, { tokenBucket } from "@arcjet/bun";
import { Hono } from "hono";
import { env } from "bun";
const aj = arcjet({
key: env.ARCJET_KEY!, // Get your site key from https://app.arcjet.com
characteristics: ["userId"], // track requests by a custom user ID
rules: [
// Create a token bucket rate limit. Other algorithms are supported.
tokenBucket({
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
refillRate: 5, // refill 5 tokens per interval
interval: 10, // refill every 10 seconds
capacity: 10, // bucket maximum capacity of 10 tokens
}),
],
});
const app = new Hono();
app.get("/", async (c) => {
const userId = "user123"; // Replace with your authenticated user ID
const decision = await aj.protect(c.req.raw, { userId, requested: 5 }); // Deduct 5 tokens from the bucket
console.log("Arcjet decision", decision.conclusion);
if (decision.isDenied()) {
return c.json({ error: "Too many requests" }, 429);
}
return c.json({ message: "Hello world" });
});
const port = 3000;
console.log(`Server is running on port ${port}`);
export default {
fetch: aj.handler(app.fetch),
port,
};

4. Start app

Start your app and load http://localhost:3000.

Terminal window
bun run --hot index.ts

Refresh the page a few times to trigger the rate limit. The requests will also show up in the Arcjet dashboard. Note: If you load this in a browser, you may see 2 requests - one for the page and one for a favicon.

FAQs

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 Arcjet 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 Arcjet 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 Arcjet 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?

Arcjet can be used with specific rules on individual routes or as general protection on your entire application. You can customize bot protection, rate limiting for your API and minimize fraudulent registrations with the signup form protection.

Get help

Need help with anything? Email us or join our Discord to get support from our engineering team.