17

I have an Azure Function that runs on a timer trigger once a week. This works great and as expected, but about once or twice a month, a user needs this function to run upon request, so I need to do a post to the function to trigger it - much like you can do from the Azure portal.

Looking at the Azure portal, a http post request is being done to the function like:

https://{funcapp}.azurewebsites.net/admin/functions/{func}

However, if I do this from Postman, I get a Http 401 response. How would I go about to do this request?

One option I have a to rather change the trigger to a queue and have a second function run on the weekly basis that would add a message to the queue, but this seems a bit excessive to me.

3 Answers 3

26

If you want to call admin API to trigger your timer function, you need to add your function master key in your request or you'll get 401 Unauthorized.

Find it on Function app settings panel >Host Keys (All functions)> _master.

Add it in your request header x-functions-key:<masterkey>.

Note that in this post request to admin API, you need to send a application/json type body (contain at least an empty json {}), this format is required or you may get 415 Unsupported Media Type.

If this post request are executed by users and you don't expect master key exposing to them, I do recommend you to use solutions provided by @Marie, have a try and you may find it's not so excessive as you thought.

6
  • 1
    Thanks. I used the masterkey along with the x-functions-key header last night but received the 415 unsupported media type response. I don't really want to expose the key, so I am rather marking @Marie Hoeger's answer as the accepted answer. Aug 8, 2018 at 7:11
  • 1
    @mieliespoor Glad you find her answer useful, I think it's better as well. Just leave my suggestion for someone refer to admin api.
    – Jerry Liu
    Aug 8, 2018 at 7:30
  • 9
    This approach is now officially documented here: learn.microsoft.com/mt-mt/Azure/azure-functions/… Jan 9, 2019 at 11:12
  • 1
    Thanks for telling what has to be sent to admin endpoint, because there is no documentation on it. If one want's to trigger his locally running Timer Triggered Function, using httpie.org here is command line: echo '{}' | http post http://0.0.0.0:7071/admin/functions/dist Content-Type:application/json
    – Marecky
    Jan 24, 2019 at 15:14
  • 1
    @SlawomirBrzezinski the link learn.microsoft.com/Azure/azure-functions/… is a better one to share, as that way we get to view in the default browser language rather than mt-mt setup :)
    – Stephen
    Feb 22, 2021 at 12:07
6

What if you share business logic between functions by using the fact that a single function app can be composed of multiple functions? Then you can have one function.json trigger based off of an HTTP request and the other trigger based off of a timer.

Your function app architecture could look like:

MyFunctionApp
|     host.json
|____ shared
|     |____ businessLogic.js
|____ function1
|     |____ index.js
|     |____ function.json
|____ function2
      |____ index.js
      |____ function.json

In "function1/index.js" and "function2/index.js"

var logic = require("../shared/businessLogic");

module.exports = logic;

The function.json of function1 and function2 can be configured to different triggers (timer and HTTP or queue... whatever you want!).

In "shared/businessLogic.js

module.exports = function (context, req) {
    // This is where shared code goes. As an example, for an HTTP trigger:
    context.res = {
        body: "<b>Hello World</b>",
        status: 201,
        headers: {
            'content-type': "text/html"
        }
    };     
    context.done();
};

(This is a JavaScript example, but same holds for other languages!)

1

This is to give an example of the accepted answer. In the following sample Function App, there are two functions: A TimerTrigger, and and HTTPTrigger. Note the default name of "Run" was changed so that the functions have different names.

When this is run, the timer will be active, as well as the http trigger. In an actual app, they would call the same business logic.

public class Host
    {
        [FunctionName("OnTimerFired")]
        public void RunTimer([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
        }

        [FunctionName("OnHttpTrigger")]
        public static async Task<IActionResult> RunHttpTrigger(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }

    }

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.