🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Server Rendering••By Komal Nakrani

Astro Client Address: Get the Visitor's IP Address

Get a visitor's IP in Astro with Astro.clientAddress or context.clientAddress: on-demand routes only, proxy and allowedDomains setup, errors, and privacy-safe use.

#Astro#Server Rendering#Astro Client Address#Middleware#Privacy

To get the visitor’s IP address in Astro, read Astro.clientAddress in a page or context.clientAddress in an endpoint, action or middleware. It only works on routes rendered on demand with an adapter, because a prerendered page is built before any visitor exists. Behind a reverse proxy with the Node adapter, set security.allowedDomains so Astro trusts the X-Forwarded-For header instead of reporting the proxy’s address.

Key Takeaways

  • clientAddress is a string property on Astro and on the API context.
  • Using it on a prerendered page throws “Astro.clientAddress is not available in prerendered pages.”
  • The adapter supplies the value: Vercel, Netlify and Cloudflare read it from the platform; Node reads the socket or X-Forwarded-For.
  • IP addresses are personal data in many jurisdictions; hash or truncate them before storing.

In a page, endpoint or middleware

---
// src/pages/debug/ip.astro
export const prerender = false;
const ip = Astro.clientAddress;
---
<p>Your IP address is {ip}</p>
// src/pages/api/whoami.ts
import type { APIRoute } from 'astro';

export const prerender = false;

export const GET: APIRoute = ({ clientAddress }) => Response.json({ ip: clientAddress });
// src/middleware.ts
import { defineMiddleware } from 'astro:middleware';

export const onRequest = defineMiddleware((context, next) => {
  if (!context.isPrerendered) {
    context.locals.ip = context.clientAddress;
  }
  return next();
});

The isPrerendered check matters in middleware: middleware also runs while static pages build, and reading clientAddress there throws. Actions get it through the second handler argument, context.clientAddress, which is how the rate limiting tip keys its counters.

The errors you might see

Error title Cause Fix
Astro.clientAddress is not available in prerendered pages. Static page or output without an adapter Add an adapter and export const prerender = false
Astro.clientAddress cannot be used inside prerendered routes. The specific route is prerendered Mark that route prerender = false
Astro.clientAddress is not available in current adapter. The adapter does not supply an address Use a supported adapter or read the platform header yourself

If you have no adapter yet, our no adapter installed fix walks through adding one.

Behind a proxy with the Node adapter

On your own server, nginx or a load balancer usually sits in front of Astro. The TCP connection then comes from the proxy, so the socket address is the proxy’s. The real client is in X-Forwarded-For, but Astro only trusts it when the request’s host matches security.allowedDomains, which protects you from clients forging the header:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import node from '@astrojs/node';

export default defineConfig({
  output: 'server',
  adapter: node({ mode: 'standalone' }),
  security: {
    allowedDomains: [{ hostname: 'example.com', protocol: 'https' }],
  },
});

Make sure the proxy sets X-Forwarded-For and X-Forwarded-Host; the Node adapter tip has an nginx example. Astro uses the first address in the header.

On Vercel, Netlify and Cloudflare, the adapter takes the address from the platform, so no configuration is needed. If you put a CDN in front of your own server, the CDN’s client IP header (for example CF-Connecting-IP from Cloudflare) is another option you can read with context.request.headers.get(), but only trust it if requests can reach your server solely through that CDN.

Use IPs responsibly

Under privacy laws such as the GDPR, an IP address can identify a person. For rate limiting, counting or abuse detection, you rarely need the raw value:

// src/lib/hash-ip.ts
export async function hashIp(ip: string, salt: string) {
  const data = new TextEncoder().encode(`${salt}:${ip}`);
  const digest = await crypto.subtle.digest('SHA-256', data);
  return [...new Uint8Array(digest)].slice(0, 8).map((b) => b.toString(16).padStart(2, '0')).join('');
}

Rotate the salt daily and the hashes cannot be linked across days. Our privacy-friendly analytics guide covers the wider picture. If you are running one of our Astro themes, it is static by default, so this only applies once you add server routes.

FAQ

How do I get the client IP address in Astro?

Use Astro.clientAddress in .astro pages or context.clientAddress in endpoints, actions and middleware. The route must be rendered on demand with an adapter.

Why is Astro.clientAddress not available?

The route is prerendered, or your adapter does not provide an address. Add an adapter, set export const prerender = false on the route, and in middleware skip prerendered requests with context.isPrerendered.

Why does Astro return 127.0.0.1 or my proxy’s IP?

You are behind a reverse proxy and Astro is not trusting forwarded headers. With the Node adapter, add your domain to security.allowedDomains and make sure the proxy sends X-Forwarded-For.

Can I get the visitor’s IP on a static Astro site?

Not at build time. A static page can call an on-demand endpoint from the browser, or rely on your host’s edge functions or logs.