next-request-ip
v1.0.7
Published
A Next.js-friendly fork of request-ip, optimized for Headers to retrieve client IP addresses.
Maintainers
Readme
next-request-ip
A Next.js-friendly fork of request-ip, optimized for working with the Web API Headers to retrieve client IP addresses.
Installation
npm install next-request-ip
# or
yarn add next-request-ipUsage
App Router (Next.js 13+)
import { getClientIp } from "next-request-ip";
import { headers } from "next/headers";
export default async function Page() {
const headersList = await headers();
const clientIp = getClientIp(headersList);
return (
<div>
<p>Your IP: {clientIp}</p>
</div>
);
}API Routes
import { NextResponse, type NextRequest } from "next/server";
import { getClientIp } from "next-request-ip";
export async function GET(request: NextRequest) {
const ip = getClientIp(request.headers);
return NextResponse.json({ ip });
}CommonJS (Node)
// If your environment uses CommonJS you can require the package:
const { getClientIp } = require('next-request-ip');
// Example usage with a standard Fetch-like Request
// (Pass a Headers-compatible object to getClientIp)
const headers = new Headers({ 'x-client-ip': '192.0.2.1' });
console.log(getClientIp(headers)); // '192.0.2.1'API
getClientIp(headers: Headers): string | null
Returns the client IP address from the request headers, or null if not found.
Checks the following headers in order of priority: Checks the following headers in order of priority (proxy/load-balancer headers first):
x-forwarded-forx-original-forwarded-forforwardedforwarded-forx-real-ipx-client-ipx-envoy-external-address(Envoy)x-envoy-client-address(Envoy)x-forwardedx-cluster-client-ipcf-connecting-ipdo-connecting-ipfastly-client-iptrue-client-ipx-appengine-user-ipCf-Pseudo-IPv4x-forwarded-forx-original-forwarded-forforwardedforwarded-forx-real-ipx-client-ipx-envoy-external-address(Envoy)x-envoy-client-address(Envoy)x-forwardedx-cluster-client-ipcf-connecting-ipdo-connecting-ipx-appengine-user-ipfastly-client-iptrue-client-ipCf-Pseudo-IPv4
Supports both IPv4 and IPv6 addresses.
Differences from request-ip
- Designed specifically for Next.js and the Web API
Headersobject - No dependencies on Node.js-specific request objects
- Optimized for modern web standards
Compatibility
- Minimum Node.js version: >= 20 (see
package.jsonengines)
License
MIT License - see LICENSE file for details.
Credits
This library is a fork of request-ip by Petar Bojinov.
Original copyright notice:
Copyright (c) 2014 Petar Bojinov