Knowing the current public hostname of a Netlify function

Hello everyone,

Is it possible, from inside a lambda handler, to know the public hostname this function is accessed from?

For example, if I have a function on, how can I know, from inside this function handler, that it’s running on

What I want to build:
My goal is to have one website available through multiple domain names. I added a redirect to my website so all incoming request to the root actually go through my lambda. Based on which domain is currently targeted, I want to return slightly different content.

What I tried
During development, I was using netlify-lambda to locally serve my functions. In that context, I could read and correctly get localhost:9000 so I assumed this was the way to go.

But once I deployed in production, I realized the headers exposed are totally different and do not include any mention of the public hostname.

Other workaround I thought of
I thought that maybe I could redirect each domain to a different subdirectory and then add redirect rules for each of this subdirectories to pass and additional header mirroring the domain name.

Something like goes to and goes to and then I add redirects such as /foo1 goes to my lambda with X-hostname: as an additional header.

But I couldn’t find a way to have domains target anything else than the root, so I’m also blocked here.

Any idea, even an convoluted one, how I could have my lambdas aware of the url they’re running on?

1 Like

Hi, @pixelastic, thank you for your patience.

It might help to clarify something before we go further. Are the function calls themselves happening because of a Netlify redirect? Or is site javascript making the calls to the functions? It sounds like the former is the case but I wanted to be sure.

Oh, that’s an interesting point, I forgot about that part.

The functions is happening because of a redirect, namely:

  from = "/"
  to = "/.netlify/functions/index"
  status = 200
  force = true

So a visit to the root of the project does actually land on the lambda, and that’s where I’d like to read the hostname.

Thanks for your time,

So the hostname of the lambda function is available inside the context object. Here’s an example of how to do it: deploy-id-test/getdeploy.js at master · depadiernos/deploy-id-test · GitHub

function extractNetlifySiteFromContext(context) {
  data = context.clientContext.custom.netlify
  decoded = JSON.parse(Buffer.from(data, "base64").toString("utf-8"))
  return decoded

Let me know if that helps.

1 Like

How about if it’s called as an API endpoint?

Having the same issue where is available in dev but not in preview/production.

I don’t have any data in dev under context.clientContext at all.

Thank you!

Ah, I’m not sure there is a workaround for that. If you are invoking the function from somewhere that is not the site it is associated with, you could probably include a custom-header that includes the request host, so you can check a single place, since the context object is not available in ‘netlify dev’ and I doubt it would ever be.

This doesn’t seem to work locally with netlify-cli. Here is context.clientContext:

{ identity: [Object], user: [Object] },

Notice that there is not custom property.

Hi @donavon,

What version of the CLI are you using. This seems to work in 5.2.11:

context.clientContext logs this:

  identity: {
    url: '',
    token: ''
  user: {
    exp: 12345,
    sub: '',
    email: '',
    app_metadata: { provider: 'email', roles: [Array] },
    user_metadata: { full_name: '' }

As you can see, roles shows [Array] instead of actual array, so the logs is not going in dept. Seems like you might be logging something else? Object is the correct representation of what type it is.

Is there a solution for ntl dev AND prod to get the hostname in a function from a client js fetch call? Reading this thread I cannot see one. In dev, works, but likely not in prod. Is there a workaround? Maybe check if typeof(!='undefined' before in prod to not get a functions error? In dev, context.clientContext for me results in {}, mentioned in the thread that it does not work.

Hey @tik9

When I log out event in a deployed function, I see I see this regardless of the method used—browser, cURL, or JS fetch.

ok, I did ntl deploy --prod and can log the function:Sep 3, 10:27:38 AM: 38e15b75 INFO 1 { custom: { netlify: 'eyJzaXRlX3VybCI6Imh0dHBzOi8vNjMxMzBmZDM4MGZlMDU2OTI1OGYxZGEwLS10aWsyLm5ldGxpZnkuYXBwIn0=' } } 2 Sep 3, 10:27:38 AM: 38e15b75 Duration: 143.05 ms Memory Usage: 86 MB. So event.headers…
works on prod.