Azure Functions Python Library

2.0.0 · active · verified Thu Apr 09

The `azure-functions` Python library provides the core programming model for developing serverless functions on Azure Functions. It enables developers to write event-driven code in Python that scales automatically and runs in response to various triggers (e.g., HTTP requests, timer, queue messages). This library, currently at version 2.0.0, is designed for the Python v2 programming model and is typically used with Azure Functions runtime v4.x. It receives updates in alignment with the Azure Functions platform's development cycle, which often includes features, performance improvements, and security patches.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a simple HTTP-triggered Azure Function using the Python v2 programming model. It shows how to define a function with the `@app.route` decorator, access request parameters (`HttpRequest`), and return an HTTP response (`HttpResponse`). This code is placed in a `function_app.py` file.

import azure.functions as func
import logging
import os

# Instantiate the FunctionApp object
app = func.FunctionApp()

# Define an HTTP trigger function using a decorator
@app.route(route="http_example", methods=["GET", "POST"])
def http_example(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    # Get name from query parameters or request body
    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body for a personalized response.",
             status_code=200
        )

view raw JSON →