HTTP Trigger Reference

The HTTP trigger allows your Azure Function to be invoked by an HTTP request. It's a versatile trigger that can be used for building webhooks, APIs, and other web-based applications.

Configuration

When you create an HTTP trigger, you configure it in the function.json file. Here's a typical configuration:

{
  "scriptFile": "../run.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ],
      "route": "myapi/{name}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

Binding Properties

Property Description Required Type Default
type Must be httpTrigger for an input binding and http for an output binding. Yes String N/A
direction Must be in for the trigger and out for the response. Yes String N/A
name The name of the parameter in your function code that will receive the incoming request object (for in) or be used to set the response object (for out). Yes String N/A
authLevel Specifies the authorization level required to invoke the function. Options: anonymous, function, admin. No String function
methods An array of HTTP methods that the function will respond to (e.g., ["get", "post"]). If omitted, the function responds to all methods. No Array of Strings All methods
route Defines a custom route for the HTTP endpoint. This allows you to override the default route. Can include route parameters (e.g., api/users/{id}). No String Default route based on function name

Incoming Request Object

The request object (named req in the example above) provides access to all details of the incoming HTTP request. The specific properties available depend on your programming language.

Common Request Properties:

Example (Python)

import logging
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    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.",
             status_code=200
        )
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

Outgoing Response Object

The response object (named res in the example above) is used to construct the HTTP response sent back to the client. It typically has properties to set the status code, headers, and body.

Common Response Properties:

Tip: When returning JSON, ensure you set the Content-Type header to application/json.

Security and Authorization

The authLevel property in function.json controls access:

Routing

The route property provides powerful routing capabilities. You can define segments in your route that correspond to query parameters or parameters in the request body.

Example Route: api/products/{id}

If a request is made to /api/products/123, the value 123 will be available in the params object of the request (e.g., req.params.get('id') in Python).

Note: Overriding the default route with the route property can impact how you access your function. Be mindful of this when structuring your API.

Error Handling

Implement robust error handling by checking for missing parameters, invalid input, and potential exceptions. Return appropriate HTTP status codes (e.g., 400 for bad requests, 500 for server errors) and informative messages.

Warning: Avoid exposing sensitive information in error messages returned to the client.

Best Practices

For more advanced scenarios and language-specific details, please refer to the official Azure Functions documentation.