How to get rid of /api in Azure Function’s route?


pexels-photo-461901We looked at Azure Functions.

We also looked at security around Azure Function used to implement APIs.

Something people will quickly notice when implementing an Webhook / API  function is that its URL or route is always prepended by /api.

For instance, if we create a webhook function in C# and we setup the route template to “part”:

image

we will have a corresponding URL of “https://<FUNCTION APP NAME>.azurewebsites.net/api/part?code=<FUNCTION KEY>”

image

We did specify the “part”, we didn’t specify “api”.

Now maybe we don’t care or maybe we’ll remap that all using either Azure API Management or Azure Function Proxy, but we like to control our API URLs, so we do care.

All paths lead to the Host Configuration

It turns out that prefix is a function app-wide (as oppose to function instance) configuration and is set in host.json.

The schema for this file is documented here while the http section, the section of the file that interests us, is documented here.  The property http/routePrefix of host.json is set to api by default.

That file can be found in a few ways.  The easiest way there:

  • Go to the function application level of the left-hand side menu in the function pane
  • In the Overview top tab, select Function app settingsimage
  • This will open a new top tab
  • Find the content of host.json at the bottom of that new tab (scroll down)

Alternatively, we could:

Finally, for those who like going under the hood, we can go directly in File Storage.  Azure Function uses a storage account, more precisely, they use a File Share within that storage account named after the function app name.  Within that file share, if we go under /site/wwwroot of that share, we’ll find host.json.

Changing the configuration

In all cases, we’ll find it empty unless we tempered with it already.

As we mentioned the route prefix default to api so we need to explicitly define it.  In order to have no prefix we can define the host.json file as:


{
  "http": {
    "routePrefix": ""
  }
}

If we test the URL again, we’ll see the /api has disappeared.

image

Summary

Although the /api prefix seems a mandatory annoyance of working with Azure Function as API, it can easily be removed or modified.

Advertisements

2 thoughts on “How to get rid of /api in Azure Function’s route?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s