Azure Functions

Serverless Compute for Event-Driven Applications

HTTP Triggers

HTTP triggers allow your Azure Function to be invoked via an HTTP request. This is a common way to build serverless APIs, webhooks, and microservices. When an HTTP trigger is configured, Azure Functions creates a publicly accessible URL for your function.

How HTTP Triggers Work

When a request is made to the function's URL:

Creating an HTTP Triggered Function

You can create HTTP triggered functions using various languages and development tools. Here's a conceptual example using 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, /* Defaults to 200 */
        body: responseMessage
    };
};
            

Request and Response Objects

The req and res objects provide access to the incoming HTTP request and the outgoing HTTP response, respectively.

Request (req) Properties:

Response (res) Properties:

You assign an object to context.res to define the response. Common properties include:

Route Configuration

HTTP triggers can be configured with specific routes to define how URLs map to functions. This allows for more complex API structures.

For example, a route like api/users/{userId} can capture the userId from the URL and make it available in req.params.

Example Route Configuration (function.json):


{
  "scriptFile": "index.js",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ],
      "route": "items/{itemId}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
                

With the above configuration, a request to /api/items/123 would have req.params.itemId set to "123".

Authentication and Authorization

Azure Functions provides several built-in authorization levels for HTTP triggers:

You can configure the authLevel in the function's binding configuration.

Key Concepts

Next Steps

Explore different bindings to connect your functions to various Azure services and external data sources.