Does anyone know how to get the URL for a deploy for a Netlify Function? I’m calling a background function from a scheduled function, so the scheduled function needs to know the domain of the background function’s URL.
I could hard-code the domain, but then PR deploys and branch deploys will always make a request to the production deployment. I want them to make the request to the PR deploy or branch deploy in which the scheduled function is running (and similarly to localhost:8888 for local dev) – i.e. stay within the same deploy or environment.
For example, when I have the background function and scheduled function deployed from PR 567, I want the scheduled function to make its request to https://deploy-preview-567--my-site.netlify.app/function/my-function-background and when the functions are deployed to production, I want the request to go to https://my-site.netlify.app/function/my-function-background. The path or the URL never changes, but the domain does and I don’t know how to get it at runtime.
It looks like the environment variable DEPLOY_URL
is automatically defined when using netlify dev
, but not when deployed to production. When deployed to production, Netlify.env.get("DEPLOY_URL")
evaluates to undefined
.
The below is what Netlify’s AI said to help me with this, but I’m not sure if it’s 100% correct because the docs here don’t agree Environment variables and functions | Netlify Docs . The docs don’t mention DEPLOY_URL
or DEPLOY_PRIME_URL
.
When deploying functions on Netlify, there are several environment variables available that provide information about the deployment URLs. These variables are particularly useful when you need to reference your site's URL in your functions.
### Deploy URL Environment Variables
Netlify provides the following read-only environment variables related to deployment URLs:
- URL: The primary URL of your site (the production URL)
- DEPLOY_URL: The URL of the specific deploy
- DEPLOY_PRIME_URL: The URL of the current deploy or, for branch deploys and Deploy Previews, the URL of the deploy
These environment variables are automatically set by Netlify and can be accessed in your functions using process.env syntax Get started with Netlify.
### Using Environment Variables in Functions
Here's an example of how to access these environment variables in a Netlify Function:
exports.handler = async () => {
const siteUrl = process.env.URL;
const deployUrl = process.env.DEPLOY_URL;
const deployPrimeUrl = process.env.DEPLOY_PRIME_URL;
return {
statusCode: 200,
body: JSON.stringify({
message: "Deploy URLs",
siteUrl,
deployUrl,
deployPrimeUrl
})
};
};
### Automatic Deploy Subdomains
When you set up automatic deploy subdomains for your site, the DEPLOY_PRIME_URL environment variable will update for all relevant deploys Automatic deploy subdomains.
### Environment Variable Scopes
To ensure your environment variables are available to functions, make sure they have the appropriate scope. When creating environment variables using the Netlify UI, CLI, or API, they are available to all scopes by default, including Functions Manage deploys.
Note that environment variables created in a netlify.toml file are not available to the deploy environment, so they won't be accessible in your functions at runtime.