HTTP Triggers
HTTP triggers allow you to execute Azure Functions in response to HTTP requests. This is a fundamental trigger type for building serverless APIs, webhooks, and other web-enabled services.
How HTTP Triggers Work
When an HTTP trigger is configured for a function, Azure Functions creates an endpoint (URL) that listens for incoming HTTP requests. When a request arrives at this endpoint, the function is invoked. The request details (method, headers, body, query parameters) are passed to your function, and your function can then perform actions and return an HTTP response.
Configuration
HTTP triggers are typically configured in the function.json file (for JavaScript, C#, etc.) or through attributes (for C# in-process/isolated models).
Example function.json for a JavaScript HTTP Trigger:
            
{
  "scriptFile": "index.js",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
            Key properties:
- type: "httpTrigger": Specifies this binding is for an HTTP trigger.
- direction: "in": Indicates this binding is an input to the function.
- name: "req": The name used to access the incoming request object within your function code.
- methods: An array of allowed HTTP methods (e.g., "get", "post", "put", "delete"). If omitted, all methods are allowed.
- authLevel: Controls access to the function. Common values include:- anonymous: No API key required.
- function: Requires a function-specific API key.
- admin: Requires the master API key.
 
Function Code (JavaScript Example)
The req object contains information about the incoming request, and the context.res (or similar depending on runtime) is used to construct the outgoing response.
// index.js
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. Pass a name in the query string or in the request body for a personalized response.';
    context.res = {
        status: 200,
        body: responseMessage,
        headers: {
            'Content-Type': 'application/json'
        }
    };
};
            Request Object Properties
The req object typically provides access to:
- req.query: An object containing query string parameters.
- req.body: The request body (parsed if possible, e.g., JSON).
- req.headers: An object containing request headers.
- req.method: The HTTP method of the request (e.g., "GET", "POST").
- req.params: Route parameters (if defined in the route).
Response Object Properties
You construct the response by setting properties on the output binding (e.g., context.res):
- status: The HTTP status code (e.g., 200, 404, 500).
- body: The content of the response body.
- headers: An object for setting response headers.
- isRaw: A boolean to indicate if the body should be sent as-is (e.g., for binary data).
HTTP Methods and Routing
You can restrict the methods your HTTP trigger responds to. For more complex routing, you can define routes in function.json or use attribute routing in C#.
Example with Route Parameters (function.json)
This requires a custom route definition, often managed by the host or specific runtime configurations. For many scenarios, route parameters are defined directly in the function definition or via host.json.
Common Use Cases
- Building RESTful APIs.
- Creating webhooks to receive events from other services.
- Processing form submissions.
- Implementing simple request/response patterns for web applications.