Mike Hohne
Mike Hohne

Reputation: 472

Calling an Azure Function from Javascript file

I'm new to Azure Functions and trying to find the best way to use Azure Functions across different environments. I set up a Slot called "Staging" and the function URL looks like this, https://myapp-staging.azurewebsites.net/api/getAssets. My question is that in different environments how would I be able to call this URL, in a Javascript file, if different Slots append the name of that Slot to each environment URL? I'd want my URL to be something relative like /api/getAssets, but i'm not sure if that's possible. Maybe environment variables?

An example Staging call:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://myapp-staging.azurewebsites.net/api/getAssets",
  "method": "GET",
}

An example Production call:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://myapp-production.azurewebsites.net/api/getAssets",
  "method": "GET",
}

The whole environment thing is throwing me off.

Upvotes: 1

Views: 1382

Answers (2)

Alexey Rodionov
Alexey Rodionov

Reputation: 1456

Easiest way to get function invoke url is clicking on "Get function URL" on the portal. See "Test the function" section:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function

You can get URLs for both prod and staging slot.

Upvotes: 2

Naren
Naren

Reputation: 887

There is an environment variable called WEBSITE_HOSTNAME, value of the value of environment variable is YourAppName.azurewebsites.net. In your case it would be myapp-staging.azurewebsites.net and myapp-production.azurewebsites.net respectively.

You can check all the environment variables and their values here https://myapp-production.scm.azurewebsites.net/Env.cshtml

Upvotes: 4

Related Questions