Overview
An HTTP trigger enables your Azure Function to be invoked directly via an HTTP request. Combined with input and output bindings, it lets you read from or write to services such as Azure Blob Storage, Cosmos DB, or queues without writing integration code.
When to Use
- Building RESTful APIs or webhooks.
- Providing lightweight serverless endpoints.
- Integrating with external services that call a URL.
- Processing HTTP requests and routing data to other Azure services via bindings.
C# Sample
Function signature with HTTP trigger and Blob output binding:
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
public static class HttpBlobFunction
{
[FunctionName("HttpBlobFunction")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
[Blob("samples/{rand-guid}.txt", FileAccess.Write)] Stream outputBlob,
ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
using var writer = new StreamWriter(outputBlob);
await writer.WriteAsync(requestBody);
return new OkObjectResult("Blob created with request payload");
}
}
JavaScript Sample
Node.js function with HTTP trigger and Cosmos DB output:
module.exports = async function (context, req) {
const item = {
id: Date.now().toString(),
data: req.body || {}
};
context.bindings.outputDocument = item; // Cosmos DB output binding
context.res = {
status: 200,
body: "Item stored in Cosmos DB"
};
};
Python Sample
Python function with HTTP trigger and Queue output:
import json
import logging
def main(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse:
logging.info('Python HTTP trigger processed a request.')
try:
body = req.get_json()
except ValueError:
return func.HttpResponse("Invalid JSON", status_code=400)
msg.set(json.dumps(body)) # Queue output binding
return func.HttpResponse("Message queued", status_code=200)
Testing the Function
Use the form below to simulate a POST request to the function endpoint. The response will be displayed after submission.
Best Practices
- Validate incoming data and return proper HTTP status codes.
- Use
AuthorizationLevel.Function
for secure endpoints. - Leverage binding expressions to route data dynamically.
- Minimize response latency by offloading heavy work to downstream bindings.
- Implement proper logging and monitoring with Application Insights.