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 httpTriggerfor an input binding andhttpfor an output binding. | Yes | String | N/A | 
| direction | Must be infor the trigger andoutfor 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 (forout). | 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:
- method: The HTTP method (e.g., "GET", "POST").
- url: The full URL of the request.
- headers: An object containing all request headers.
- query: An object containing query string parameters.
- body: The request body. This will be parsed if the- Content-Typeheader is- application/json.
- params: Route parameters defined in the- routeproperty.
- rawBody: The raw request body as a string or byte array.
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:
- status_code: The HTTP status code (e.g., 200, 400, 404).
- body: The content of the response body. Can be a string, JSON object, or other data.
- headers: An object to set custom response headers (e.g.,- {"Content-Type": "application/json"}).
Content-Type header to application/json.
            Security and Authorization
The authLevel property in function.json controls access:
- anonymous: Anyone can call the function URL. Use with caution.
- function: Requires a function key passed in the- x-functions-keyheader or as a query parameter. This is the default.
- admin: Requires the master key, which is more secure and should only be used for administrative functions.
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).
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.
Best Practices
- Use appropriate HTTP methods (GET for retrieving, POST for creating, PUT for updating, DELETE for removing).
- Validate all incoming data rigorously.
- Keep HTTP triggered functions concise and focused on a single task.
- For complex operations, consider chaining functions together using Durable Functions or other orchestration patterns.
For more advanced scenarios and language-specific details, please refer to the official Azure Functions documentation.