I am building a Gatsby site with Netlify functions and trying to capture the client IP address in a Netlify function to forward it to an external API. However, the client IP address is not being received in the request headers of the Netlify function.
I have tried accessing the client IP address using the following headers in my Netlify function:
event.headers['x-nf-client-connection-ip']
event.headers['x-forwarded-for']
event.headers['client-ip']
None of these headers seem to contain the correct client IP address.
Here’s the relevant code snippet from my Netlify function:
const clientIp = event.headers['x-forwarded-for'] || event.headers['x-nf-client-connection-ip'];
console.log('Client IP:', clientIp);
// Make the request to the external API using built-in fetch
const response = await fetch(apiUrl, {
headers: {
'Content-Type': 'application/json',
'X-Forwarded-For': clientIp || 'Unknown',
},
});
I have also checked the Netlify function logs, and the clientIp
variable is either undefined or not capturing the correct IP address.
I have reached out to the external API provider, and they confirmed that they are not receiving the X-Forwarded-For
header with the client IP address in the requests coming from my Netlify function.
I have searched through the Netlify documentation and forums but couldn’t find a solution specific to my Gatsby setup.
Could you please help me understand why the client IP address is not being captured correctly in my Netlify function and how I can forward it to the external API? Are there any Gatsby-specific configurations or settings I need to consider?
Any guidance or assistance would be greatly appreciated. Thank you in advance!