BadRequest when Create Or Update Host Secret (aka Api Key) for Function App

When creating or updating host secret (aka ApiKey) for Function App using ARM Api, have you ever received a error message “Properties object is not present in the request body.”? Well I did.

Here’s full error response body I was receiving:

{
    "Code": "BadRequest",
    "Message": "Properties object is not present in the request body.",
    "Target": null,
    "Details": [
        {
            "Message": "Properties object is not present in the request body."
        },
        {
            "Code": "BadRequest"
        },
        {
            "ErrorEntity": {
                "ExtendedCode": "51006",
                "MessageTemplate": "{0} object is not present in the request body.",
                "Parameters": [
                    "Properties"
                ],
                "Code": "BadRequest",
                "Message": "Properties object is not present in the request body."
            }
        }
    ],
    "Innererror": null
}

Message states, I’m sending incorrect body. So what I’m sending in the body? Just a simple JSON:

{
  "Name": "ApiKey",
  "Value": "<my-secret-api-key>"
}

Let’s see in the docs: https://docs.microsoft.com/en-us/rest/api/appservice/webapps/createorupdatehostsecret#request-body.

According to thee docs, I should send just two parameters in the body “name” and “value”:

Name Type Description
name string Key name
value string Key value

Looks easy. But ARM Api keeps telling me something is missing. Not so easy, though. How should correct JSON look like then?

Correct JSON should look like so:

{
  "Properties": {
    "Name": "ApiKey",
    "Value": "<my-secret-api-key>"
  }
}

Thats it! Simple, isn’t it?

Bonus: how to create Function App Host Secret using PowerShell.

Thanks for reading!