HTTP trigger bindings
This article explains how to configure and use the HTTP trigger binding in Azure Functions.
Overview
The HTTP trigger allows your function to be invoked via an HTTP request. It's one of the most common triggers, enabling you to build serverless APIs, webhooks, and more.
Configuration
The HTTP trigger is configured in your function's function.json
file. Here's a basic example:
{
"scriptFile": "run.cs",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
Binding Properties
The HTTP trigger binding has the following properties:
Property | Description | Required |
---|---|---|
type |
Must be set to httpTrigger . |
Yes |
direction |
Must be set to in . |
Yes |
name |
The name of the parameter in your function code that represents the incoming HTTP request. | Yes |
methods |
An array of HTTP methods (e.g., get , post , put , delete ) that the trigger should respond to. If omitted, it defaults to all methods. |
No |
authLevel |
Specifies the authorization level required to invoke the function. Can be anonymous , function , or admin . |
No (defaults to function ) |
route |
Defines a custom route template for the HTTP trigger, overriding the default route derived from the function's folder name. | No |
Request and Response
When the HTTP trigger is invoked:
- The incoming HTTP request is bound to the parameter specified by the
name
property in your function code. - Your function's return value or the parameter bound with
direction: "out"
andtype: "http"
is used to construct the HTTP response.
Accessing Request Properties
You can access various properties of the incoming HTTP request, such as:
req.method
: The HTTP method of the request.req.headers
: A dictionary of request headers.req.query
: A dictionary of query parameters.req.body
: The request body (parsed as JSON if theContent-Type
isapplication/json
).req.params
: URL route parameters (ifroute
is used).
Creating a Response
You can create an HTTP response by returning an object from your function or by setting properties on the output binding parameter:
statuscode
: The HTTP status code.body
: The response body.headers
: A dictionary of response headers.
Example (Node.js)
Here's a Node.js example of an HTTP-triggered function:
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'
}
};
};
Example (C#)
Here's a C# example:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
public static class HttpExample
{
[FunctionName("HttpExample")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}!";
return new OkObjectResult(responseMessage);
}
}
Tip
Use the route
property in function.json
to create cleaner API endpoints, such as /api/users/{id}
.
Important
When using the function
authentication level, you will need to include an API key in the x-functions-key
header or as a query parameter (code=YOUR_FUNCTION_KEY
) when making requests to your function.