How to get rid of /api in Azure Function’s route?
Solution ·We 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”:
we will have a corresponding URL of “https://<FUNCTION APP NAME>.azurewebsites.net/api/part?code=<FUNCTION KEY>”
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 settings
- 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:
-
- Go to the function application level of the left-hand side menu in the function pane
- In the Platform features top tab, select App Service Editor
- This will open a new browser tab with URL https://<FUNCTION APP NAME>.scm.azurewebsites.net/dev/wwwroot/
- We can then easily find host.json under the root
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.
Summary
Although the /api prefix seems a mandatory annoyance of working with Azure Function as API, it can easily be removed or modified.