> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/honojs/hono/llms.txt
> Use this file to discover all available pages before exploring further.

# ConnInfo

> Access HTTP connection and network information

The ConnInfo helper provides type definitions for accessing HTTP connection information, including remote address, port, and transport protocol details. This is useful for logging, security checks, and network debugging.

## Import

```typescript theme={null}
import type { ConnInfo, GetConnInfo, NetAddrInfo, AddressType } from 'hono/conninfo'
```

## Type Definitions

The ConnInfo helper exports TypeScript types for working with connection information:

### ConnInfo

The main connection information interface:

```typescript theme={null}
interface ConnInfo {
  /**
   * Remote connection information
   */
  remote: NetAddrInfo
}
```

### NetAddrInfo

Network address information for a connection:

```typescript theme={null}
type NetAddrInfo = {
  /**
   * Transport protocol type
   */
  transport?: 'tcp' | 'udp'
  
  /**
   * Transport port number
   */
  port?: number
  
  /**
   * IP address or hostname
   */
  address?: string
  
  /**
   * Address type (IPv4 or IPv6)
   */
  addressType?: AddressType
} & (
  | {
      address: string
      addressType: AddressType
    }
  | {}
)
```

### AddressType

The type of network address:

```typescript theme={null}
type AddressType = 'IPv6' | 'IPv4' | undefined
```

### GetConnInfo

Helper type for functions that extract connection info from context:

```typescript theme={null}
type GetConnInfo = (c: Context) => ConnInfo
```

## Usage with Adapters

Connection information extraction is adapter-specific. Many runtime adapters provide a `getConnInfo` function:

### Cloudflare Workers

```typescript theme={null}
import { Hono } from 'hono'
import { getConnInfo } from 'hono/cloudflare-workers'

const app = new Hono()

app.get('/ip', (c) => {
  const info = getConnInfo(c)
  return c.json({
    address: info.remote.address,
    addressType: info.remote.addressType,
  })
})
```

### Deno

```typescript theme={null}
import { Hono } from 'hono'
import { getConnInfo } from 'hono/deno'

const app = new Hono()

app.get('/connection', (c) => {
  const info = getConnInfo(c)
  return c.json({
    ip: info.remote.address,
    port: info.remote.port,
    transport: info.remote.transport,
  })
})
```

### Bun

```typescript theme={null}
import { Hono } from 'hono'
import { getConnInfo } from 'hono/bun'

const app = new Hono()

app.get('/client-info', (c) => {
  const info = getConnInfo(c)
  return c.text(`Client IP: ${info.remote.address}`)
})
```

### Node.js

```typescript theme={null}
import { Hono } from 'hono'
import { getConnInfo } from '@hono/node-server/conninfo'

const app = new Hono()

app.use('*', async (c, next) => {
  const info = getConnInfo(c)
  console.log(`Request from ${info.remote.address}:${info.remote.port}`)
  await next()
})
```

## Common Use Cases

### IP Address Logging

Log client IP addresses for requests:

```typescript theme={null}
import { getConnInfo } from 'hono/cloudflare-workers'

app.use('*', async (c, next) => {
  const info = getConnInfo(c)
  console.log(`[${new Date().toISOString()}] ${c.req.method} ${c.req.path} - ${info.remote.address}`)
  await next()
})
```

### IP-based Rate Limiting

Implement simple IP-based rate limiting:

```typescript theme={null}
import { getConnInfo } from 'hono/cloudflare-workers'

const rateLimitMap = new Map<string, number>()

app.use('*', async (c, next) => {
  const info = getConnInfo(c)
  const ip = info.remote.address || 'unknown'
  
  const count = rateLimitMap.get(ip) || 0
  if (count > 100) {
    return c.text('Rate limit exceeded', 429)
  }
  
  rateLimitMap.set(ip, count + 1)
  await next()
})
```

### Geolocation-based Content

Serve different content based on IP address:

```typescript theme={null}
import { getConnInfo } from 'hono/cloudflare-workers'

app.get('/localized', async (c) => {
  const info = getConnInfo(c)
  const ip = info.remote.address
  
  // Use IP geolocation service
  const region = await getRegionFromIP(ip)
  
  return c.json({
    ip,
    region,
    message: getLocalizedMessage(region)
  })
})
```

### Security Checks

Implement IP allowlisting or blocklisting:

```typescript theme={null}
import { getConnInfo } from 'hono/cloudflare-workers'

const ALLOWED_IPS = ['192.168.1.1', '10.0.0.1']

app.use('/admin/*', async (c, next) => {
  const info = getConnInfo(c)
  const ip = info.remote.address
  
  if (!ip || !ALLOWED_IPS.includes(ip)) {
    return c.text('Forbidden', 403)
  }
  
  await next()
})
```

### Debug Information

Return connection details for debugging:

```typescript theme={null}
import { getConnInfo } from 'hono/cloudflare-workers'

app.get('/debug/connection', (c) => {
  const info = getConnInfo(c)
  
  return c.json({
    remote: {
      address: info.remote.address,
      addressType: info.remote.addressType,
      port: info.remote.port,
      transport: info.remote.transport,
    },
    headers: Object.fromEntries(c.req.raw.headers),
  })
})
```

## IPv4 vs IPv6

Check the address type to handle IPv4 and IPv6 differently:

```typescript theme={null}
import { getConnInfo } from 'hono/cloudflare-workers'

app.get('/ip-version', (c) => {
  const info = getConnInfo(c)
  const type = info.remote.addressType
  
  if (type === 'IPv6') {
    return c.text(`IPv6 address: ${info.remote.address}`)
  } else if (type === 'IPv4') {
    return c.text(`IPv4 address: ${info.remote.address}`)
  } else {
    return c.text('Address type unknown')
  }
})
```

## Adapter Availability

Connection info support varies by runtime adapter:

<CardGroup cols={2}>
  <Card title="Cloudflare Workers" icon="cloud">
    Full support via `hono/cloudflare-workers`
  </Card>

  <Card title="Deno" icon="circle-check">
    Full support via `hono/deno`
  </Card>

  <Card title="Bun" icon="circle-check">
    Full support via `hono/bun`
  </Card>

  <Card title="Node.js" icon="node">
    Available via `@hono/node-server/conninfo`
  </Card>
</CardGroup>

<Note>
  Check your specific adapter's documentation for `getConnInfo` availability and implementation details.
</Note>

## Behind Proxies

When your application is behind a proxy or load balancer, the remote address will be the proxy's IP. Use headers like `X-Forwarded-For` for the actual client IP:

```typescript theme={null}
import { getConnInfo } from 'hono/cloudflare-workers'

app.get('/real-ip', (c) => {
  // Try X-Forwarded-For first (from proxy)
  const forwardedFor = c.req.header('X-Forwarded-For')
  if (forwardedFor) {
    const clientIP = forwardedFor.split(',')[0].trim()
    return c.json({ ip: clientIP, source: 'X-Forwarded-For' })
  }
  
  // Fall back to direct connection info
  const info = getConnInfo(c)
  return c.json({ ip: info.remote.address, source: 'direct' })
})
```

<Warning>
  **Security**: Only trust `X-Forwarded-For` and similar headers when you control the proxy. These headers can be spoofed by clients if not properly configured.
</Warning>

## Best Practices

<CardGroup cols={2}>
  <Card title="Check Adapter Support" icon="list-check">
    Verify your runtime adapter supports `getConnInfo` before using
  </Card>

  <Card title="Handle Undefined" icon="shield">
    Always handle cases where address or port may be undefined
  </Card>

  <Card title="Proxy Awareness" icon="network-wired">
    Use appropriate headers when behind proxies or load balancers
  </Card>

  <Card title="Privacy Compliance" icon="user-shield">
    Be mindful of privacy laws when logging or storing IP addresses
  </Card>
</CardGroup>
