Create an HTTP triggered function in Azure


Python for Azure Functions is currently in preview. To receive important updates, subscribe to the Azure App Service announcements repository on GitHub.

This article shows you how to use command-line tools to create a Python project that runs in Azure Functions. The function you create is triggered by HTTP requests. Finally, you publish your project to run as a serverless function in Azure.

This article is the first of two quickstarts for Azure Functions. After you complete this article, you add an Azure Storage queue output binding to your function.


Before you start, you must have the following:

If you don't have an Azure subscription, create a free account before you begin.

Create and activate a virtual environment

To locally develop and test Python functions, you must work in a Python 3.6 environment. Run the following commands to create and activate a virtual environment named .env.

Bash or a terminal window:

python3.6 -m venv .env
source .env/bin/activate

PowerShell or a Windows command prompt:

py -3.6 -m venv .env

The remaining commands are run inside the virtual environment.

Create a local Functions project

A Functions project is the equivalent of a function app in Azure. It can have multiple functions that all share the same local and hosting configurations.

In the virtual environment, run the following command, choosing python as your worker runtime.

func init MyFunctionProj

A folder named MyFunctionProj is created, which contains the following three files:

  • local.settings.json is used to store app settings and connection strings when running locally. This file doesn't get published to Azure.
  • requirements.txt contains the list of packages to be installed on publishing to Azure.
  • host.json contains global configuration options that affect all functions in a function app. This file does get published to Azure.

Navigate to the new MyFunctionProj folder:

cd MyFunctionProj

Next, you update the host.json file to enable extension bundles.

Reference bindings

Extension bundles makes it easier to add binding extensions down the road. It also removes the requirement of installing the .NET Core 2.x SDK. Extension bundles requires version 2.6.1071 of the Core Tools, or a later version.

To reference the Azure Functions 2.x default bindings, open the host.json file and update contents to match the following code.

    "version": "2.0",
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"

Now, you can add a function to your project.

Create a function

To add a function to your project, run the following command:

func new

Choose the HTTP trigger template, type HttpTrigger as the name for the function, then press Enter.

A subfolder named HttpTrigger is created, which contains the following files:

  • function.json: configuration file that defines the function, trigger, and other bindings. Review this file and see that the value for scriptFile points to the file containing the function, while the invocation trigger and bindings are defined in the bindings array.

    Each binding requires a direction, type and a unique name. The HTTP trigger has an input binding of type httpTrigger and output binding of type http.

  • script file that is your HTTP triggered function. Review this script and see that it contains a default main(). HTTP data from the trigger is passed to this function using the req named binding parameter. Defined in function.json, req is an instance of the azure.functions.HttpRequest class.

    The return object, defined as $return in function.json, is an instance of azure.functions.HttpResponse class. To learn more, see Azure Functions HTTP triggers and bindings.

Run the function locally

The following command starts the function app, which runs locally using the same Azure Functions runtime that is in Azure.

func host start

When the Functions host starts, it writes something like the following output, which has been truncated for readability:

            @   %%%%%%    @
          @@   %%%%%%      @@
       @@@    %%%%%%%%%%%    @@@
     @@      %%%%%%%%%%        @@
       @@         %%%%       @@
         @@      %%%       @@
           @@    %%      @@


Content root path: C:\functions\MyFunctionProj
Now listening on:
Application started. Press Ctrl+C to shut down.


Http Functions:

        HttpTrigger: http://localhost:7071/api/MyHttpTrigger

[8/27/2018 10:38:27 PM] Host started (29486ms)
[8/27/2018 10:38:27 PM] Job host started

Copy the URL of your HttpTrigger function from the runtime output and paste it into your browser's address bar. Append the query string ?name=<yourname> to this URL and execute the request. The following shows the response in the browser to the GET request returned by the local function:

Test locally in the browser

Now that you have run your function locally, you can create the function app and other required resources in Azure.

Create a resource group

Create a resource group with the az group create. An Azure resource group is a logical container into which Azure resources like function apps, databases, and storage accounts are deployed and managed.

The following example creates a resource group named myResourceGroup.
If you are not using Cloud Shell, sign in first using az login.

az group create --name myResourceGroup --location westeurope

You generally create your resource group and the resources in a region near you. To see all supported locations for App Service plans, run the az appservice list-locations command.

Create an Azure Storage account

Functions uses a general-purpose account in Azure Storage to maintain state and other information about your functions. Create a general-purpose storage account in the resource group you created by using the az storage account create command.

In the following command, substitute a globally unique storage account name where you see the <storage_name> placeholder. Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only.

az storage account create --name <storage_name> --location westeurope --resource-group myResourceGroup --sku Standard_LRS

After the storage account has been created, the Azure CLI shows information similar to the following example:

  "creationTime": "2017-04-15T17:14:39.320307+00:00",
  "id": "/subscriptions/bbbef702-e769-477b-9f16-bc4d3aa97387/resourceGroups/myresourcegroup/...",
  "kind": "Storage",
  "location": "westeurope",
  "name": "myfunctionappstorage",
  "primaryEndpoints": {
    "blob": "",
    "file": "",
    "queue": "",
    "table": ""
    // Remaining output has been truncated for readability.

Create a function app in Azure

A function app provides an environment for executing your function code. It lets you group functions as a logical unit for easier management, deployment, and sharing of resources.

Run the following command using a unique function app name in place of the <APP_NAME> placeholder and the storage account name for <STORAGE_NAME>. The <APP_NAME> is also the default DNS domain for the function app. This name needs to be unique across all apps in Azure.

az functionapp create --resource-group myResourceGroup --os-type Linux \
--consumption-plan-location westeurope  --runtime python \
--name <APP_NAME> --storage-account  <STORAGE_NAME>


Linux and Windows apps cannot be hosted in the same resource group. If you have an existing resource group named myResourceGroup with a Windows function app or web app, you must use a different resource group.

You're now ready to publish your local functions project to the function app in Azure.

Deploy the function app project to Azure

After the function app is created in Azure, you can use the func azure functionapp publish Core Tools command to deploy your project code to Azure. In the following command, replace <APP_NAME> with the name of your app from the previous step.

func azure functionapp publish <APP_NAME>

You will see output similar to the following, which has been truncated for readability.

Getting site publishing info...

Preparing archive...
Uploading content...
Upload completed successfully.
Deployment completed successfully.
Syncing triggers...
Functions in myfunctionapp:
    HttpTrigger - [httpTrigger]
        Invoke url:

Copy the Invoke URL value for your HttpTrigger, which you can now use to test your function in Azure. The URL contains a code query string value that is your function key. This key makes it difficult for others to call your HTTP trigger endpoint in Azure.

Test the function in Azure

Use cURL to test the deployed function. Using the URL that you copied from the previous step, append the query string &name=<yourname> to the URL, as in the following example:


using cURL to call the function in Azure.

You can also paste the copied URL in to the address of your web browser. Again, append the query string &name=<yourname> to the URL before you execute the request.

Using a web browser to call the function.

Next steps

You've created a Python functions project with an HTTP triggered function, run it on your local machine, and deployed it to Azure. Now, extend your function by...