Azure Functions Documentation

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:

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:

Response Object Properties

You construct the response by setting properties on the output binding (e.g., context.res):

Note: The exact structure of the request and response objects may vary slightly depending on the language runtime and the Azure Functions host version.

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

Tip: For building robust APIs, consider using Azure API Management in front of your Azure Functions to handle authentication, rate limiting, caching, and more.