Azure Functions HTTP Trigger Bindings

This document details how to configure and use the HTTP trigger binding for Azure Functions.

Overview

The HTTP trigger allows your Azure Function to be invoked by an HTTP request. It's a common choice for building web APIs, webhook handlers, and serverless microservices.

When a function is triggered by HTTP, the request and response objects are passed into your function code. This binding supports both GET and POST requests by default, but can be configured to accept other HTTP methods.

Configuration

The HTTP trigger is configured in the function.json file for your function. Here's a basic example:


{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
            

Key properties:

Accessing Request Data

The req parameter (or whatever name you define in function.json) provides access to the incoming HTTP request details.

The exact structure of the req object depends on the language runtime you are using. Here are examples for common runtimes:

JavaScript (Node.js)

In Node.js, the req object is an instance of HttpRequestMessage.

Example


module.exports = async function (context, req) {
    context.log('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
    };
};
                    

You can access query parameters via req.query, route parameters via req.params, and the request body via req.body.

Returning an HTTP Response

The HTTP output binding allows you to send a response back to the client.

Similar to the request, the structure of the response object varies by language.

JavaScript (Node.js)

Assign a response object to context.res. This object can have status, headers, and body properties.

Example


module.exports = async function (context, req) {
    context.log('Returning a custom response.');

    context.res = {
        status: 400,
        headers: {
            'Content-Type': 'application/json'
        },
        body: { message: 'Bad Request', error: true }
    };
};
                    

Routing

By default, HTTP-triggered functions are accessible at a URL that includes the function name. You can customize this using the route property in function.json.

If route is not set, the default route is api/{functionName}.

Example: Custom Route


{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": ["get"],
      "route": "myapi/products/{productId}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
                

With this configuration, a function named GetProduct would be accessible at /api/myapi/products/{productId}. The {productId} part is a route parameter that can be accessed in your code.

Customizing HTTP Methods

You can explicitly define which HTTP methods your function should respond to using the methods array in the function.json.

Example: Only accepting PUT requests


{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": ["put"],
      "route": "items/{itemId}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
                

Security Considerations

Always consider the authLevel setting. For public APIs, anonymous might be suitable, but for internal or sensitive operations, function or admin are recommended.

When using function or admin, you will need to include an API key in your requests:

Remember to protect your function keys and avoid committing them directly into your code. Use environment variables or Azure Key Vault.

Next Steps

Explore other bindings to enhance your Azure Functions capabilities. Consider input and output bindings for databases, queues, and storage services.