How to remove /api from Azure Functions URLs

Note: All of the following assumes you’re working with Azure Functions 2.0.

Azure Functions are great. By default, all HTTP functions on Azure have their URLs prefixed with /api. For example, if you have functions named Function1 and Function2, you’d reach them by calling http://my-app.azurewebsites.net/api/Function1 and http://my-app.azurewebsites.net/api/Function2.

Usually, this is fine. But it seems like an arbitrary restriction. What if you’d rather not have it?

Fortunately, there’s a configuration option in your function app’s host.json file to change it. Unfortunately, as I’m writing this, Microsoft’s documentation is incorrect.

It tells you your host.json should look something like this if you want to remove the /api prefix:

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

This won’t work. If you’re running Visual Studio, it’ll tell you Property name is not allowed by the schema. Instead, the http object must be placed inside of extensions:

{
  "version": "2.0",
  "extensions": {
    "http": {
      "routePrefix": ""
    }
  }
}

Setting routePrefix to an empty string removes /api from the URL. Now, to access Function1, you’d call http://my-app.azurewebsites.net/Function1.

Also note the version property. If you don’t add this, the Azure Functions local simulator will flip out and fall into an infinite loop of scary-looking error messages:

If host.json is blank, the simulator will be fine. But as soon as you add anything else to host.json, you also have to add a version, or the simulator will go crazy.

And that’s it! You now know how to remove /api from your Azure Functions URLs. You can also use a non-blank string to use a prefix other than api. If you’d like your functions URLs prefixed by popcorn or cats, there’s nothing stopping you!

One Reply to “How to remove /api from Azure Functions URLs”

  1. Jens Caasen says:

    Great, thank you for this. This is one thing where you can not ask an LLM, as their training data is wrong in the first place. A good old google search had to do to find this information from your blog

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2024 Ryan Peden . Powered by WordPress. Theme by Viva Themes.