Arcjet IP detection reference
The Arcjet IP detection library provides a utility to find the public IP of a Request.
What are Arcjet utilities?
Arcjet utilities are independent libraries that do not require the use of the main Arcjet SDK—they can be used with or without other Arcjet rules.
We take the pain out of implementing security tasks through these utilities to provide a security as code approach to developer-first security.
Why
The public IP of a Request is difficult to discern, but some platforms provide
specific mechanisms for accessing it—such as the X-Real-IP
header added or
overwritten by Vercel. The @arcjet/ip
library provides a streamlined API over
these mechanisms based on the current platform.
Install
npm install -S @arcjet/ip
Usage
import ip from "@arcjet/ip";
// Some Request-like object, such as node's `http.IncomingMessage`, `Request` or// Next.js' `NextRequest`const request = new Request("/your-route");
// Returns the first non-private IP address detectedconst publicIp = ip(request);console.log(publicIp);
Platform protections
Additional guards can be applied with the platform
option, such as
{ platform: "fly-io" }
, { platform: "cloudflare" }
, or
{ platform: "vercel" }
.
import ip from "@arcjet/ip";
// Some Request-like object, such as node's `http.IncomingMessage`, `Request` or// Next.js' `NextRequest`const request = new Request("/your-route");
// Also optionally takes a platform for additional protectionconst platformGuardedPublicIp = ip(request, { platform: "fly-io" });console.log(platformGuardedPublicIp);
Proxy filtering
Most proxies will add themselves in the chain of public IP addresses. Trusted
proxies may be specified with the proxies
option, and they will be
ignored when detecting a public IP.
import ip from "@arcjet/ip";
// Some Request-like object, such as node's `http.IncomingMessage`, `Request` or// Next.js' `NextRequest`const request = new Request("/your-route");
// You can also pass a list of trusted proxies to ignoreconst proxyExcludedPublicIp = ip(request, { proxies: ["103.31.4.0"] });console.log(proxyExcludedPublicIp);
API
ip(request: RequestLike, options?: Options)
Look up an IP address in a Request-like object, such as Request
, Node’s
http.IncomingMessage
, or Next.js’ NextRequest
.
Types:
type RequestLike = | { ip?: unknown; socket?: PartialSocket | undefined; info?: PartialInfo | undefined; requestContext?: PartialRequestContext | undefined; headers: Headers; } | { ip?: unknown; socket?: PartialSocket | undefined; info?: PartialInfo | undefined; requestContext?: PartialRequestContext | undefined; headers: Record<string, string | string[] | undefined>; };
type Options = { platform?: Platform | undefined; proxies?: Array<string> | undefined;};
type Platform = "cloudflare" | "fly-io" | "vercel";
What next?
Arcjet can protect your entire app or individual routes with just a few lines of code. Using the main Arcjet SDK you can setup bot protection, rate limiting for your API, minimize fraudulent registrations with the signup form protection and more.