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:

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.


Leave a comment