MegaHydronics
MegaHydronics5d ago

Accessing client IP address in a query/mutation

Good day. Any help would be appreciated, as I am fairly new to Convex. Is there a way to expose HTTP headers, such as for obtaining the IP address in the example below? Or is an httpAction required for this use case?
const registerClient = mutation({
args: { userAgent: v.string() },
handler: async (ctx, { userAgent }) => {
// get ip address from 'x-forwarded-for' header
await ctx.db.insert('client', { userAgent, ip })
}
})
const registerClient = mutation({
args: { userAgent: v.string() },
handler: async (ctx, { userAgent }) => {
// get ip address from 'x-forwarded-for' header
await ctx.db.insert('client', { userAgent, ip })
}
})
6 Replies
Convex Bot
Convex Bot5d ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
MegaHydronics
MegaHydronicsOP5d ago
#ask-ai referred me to this post https://discord.com/channels/1019350475847499849/1281699105101451284 which answered my question.
ballingt
ballingt5d ago
What's your goal with this? Currently the incoming IP address is not exposed even in an httpAction.
MegaHydronics
MegaHydronicsOP5d ago
It's for simple analytics of users' geographical distribution.
const response = await fetch(`https://ipinfo.io/${ip}/json/?token=${process.env.ipinfoToken}`)
const response = await fetch(`https://ipinfo.io/${ip}/json/?token=${process.env.ipinfoToken}`)
ballingt
ballingt5d ago
If Convex were to expose as an extra header or ctx.meta.geo property would you want the geographical location directly, or still the IP address?
MegaHydronics
MegaHydronicsOP5d ago
For my use case, ctx.meta.geo would be great.