HTTP Trigger
The HTTP trigger allows your Azure Function to be invoked by an HTTP request. It's one of the most common triggers used for building web APIs, webhooks, and other event-driven services.
How it Works
When an HTTP request is sent to your function's endpoint, the HTTP trigger intercepts it. This trigger provides properties about the incoming request, such as the HTTP method, headers, query parameters, and the request body. It also allows you to return an HTTP response.
Configuration
The HTTP trigger is configured in the function.json file for your function. Here's a typical configuration:
{
    "scriptFile": "../run.py",
    "bindings": [
        {
            "authLevel": "function",
            "type": "httpTrigger",
            "direction": "in",
            "name": "req",
            "methods": [
                "get",
                "post"
            ],
            "route": "items/{id}"
        },
        {
            "type": "http",
            "direction": "out",
            "name": "res"
        }
    ]
}Key Properties:
- type: Must be- httpTriggerfor an incoming HTTP trigger and- httpfor an outgoing HTTP response.
- direction:- infor triggers and inputs,- outfor outputs.
- name: The name of the variable used in your function code to access the trigger data (e.g.,- req) or the output binding (e.g.,- res).
- methods: An array of allowed HTTP methods (e.g.,- "get",- "post",- "put",- "delete"). If omitted, all methods are allowed.
- route: Allows you to define a custom route for your function, enabling more complex API structures. For example,- "items/{id}"allows you to capture the value of- idfrom the URL.
- authLevel: Controls how the HTTP endpoint is secured. Common values include:- anonymous: No authentication required.
- function: Requires a function-specific API key.
- admin: Requires a master API key (use with caution).
 
Accessing Request Data
Inside your function code, the req object (or whatever you named it) provides access to the HTTP request details. The exact properties depend on your chosen language.
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
        )Example (JavaScript):
module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
    context.res = {
        status: 200,
        body: responseMessage
    };
};Returning an HTTP Response
The res object (or your designated output binding name) is used to define the HTTP response that will be sent back to the client. You can set the status code, headers, and body of the response.
- status: The HTTP status code (e.g.,- 200,- 404,- 500).
- body: The content of the response body. Can be a string, JSON, or binary data.
- headers: An object containing HTTP response headers.
Common Scenarios
- API Endpoints: Build RESTful APIs by defining different routes and HTTP methods.
- Webhooks: Receive notifications from external services.
- Event Handlers: Trigger functions based on events from other services that can make HTTP requests.
- Synchronous Operations: When you need an immediate response to a request.
Security Considerations
Always use appropriate authLevel settings to protect your HTTP endpoints. For production environments, consider using API Management or other security measures.