Access function complete url from the function itlself

Hi

I was wondering if it possible to access the complete URL of a function from the function itself?
The event object contains the path and query parameters, but not the hostname/domain piece

For example, one of my functions would like to determine if it is running under myuser.netlify.com or branch--myuser.netlify.com.

Alternatively, there is maybe a way to reference “relative” resources within the function code?
Example:
myuser.netlify.com/.netlify/functions/hello generates a link to myuser.netlify.com/img/hello.png
and
branch--myuser.netlify.com/.netlify/functions/hello generates a link to branch--myuser.netlify.com/img/hello.png

Thanks!

Hi @tvanier, it’s possible to get the site url from the context object, specifically context.clientContext.custom.netlify. The following snippet shows how you would decode the base64-encoded data using javascript:

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 works for you.

Thanks @Dennis, sorry for the (very) late reply.
My function can access the site_url property of the decoded context.clientContext.custom.netlify no problem. Curious if the netlify context contain other data? Is it documented somewhere?

My function generates some html meta tags such as <meta property="og:image" content="{url}">, where {url} must be an absolute url to an image. I would like to co-host the image file on the same netlify site than my function. As explained here, instead of a single my-function.js file, I tried to deploy a folder like

functions/my-function-2/my-function-2.js
functions/my-function-2/my-image.png

So my image is available at https://my-site.netlify.app/functions/my-function-2/my-image.png

But

  1. my-function-2 does not appear on my functions dashboard and /.netlify/functions/my-function-2 returns 404? The above image url works though.
  2. could my function access the custom functions folder (if any) of the current site? In the netlify context maybe?

Thanks

Hi, I don’t think there is any other data in the context object and I don’t think there’s any doc about it. You could log out the content of that object using a console.log and that should give you an ides what’s inside.

Regarding your function, it is not like a site deploy, a function’s file structure is not accessible the way you mentioned. You’ll want to read through our Functions doc on how to deploy a function.

If you want to include an image in your function, then you will want to include it in your function (via import or require). This will tell our system to include the image when the function is bundled for deploy. Alternatively, you could deploy the image as part of your site (in the build directory) and access it in your function using the complete url to that image.

To summarize, the function environment is completely separate from a site deploy. Hope that addresses some of your questions.