Azure Functions Output Bindings

Output bindings allow your Azure Function to write data to other Azure services or external systems without needing to write explicit SDK code. This simplifies your function code by separating the concerns of your business logic from the integration logic.

How Output Bindings Work

Output bindings are defined in your function's function.json file (or through attributes in code for compiled languages). When your function executes and returns a value, or when you explicitly bind an output to a result, the Azure Functions runtime handles the communication with the specified service based on the binding configuration.

Common Output Binding Scenarios

Defining Output Bindings

Output bindings are declared in the bindings array of your function.json file. Each binding object requires at least the following properties:

Additional properties are specific to each binding type and define the connection string, path, or other configuration details.

Example: Blob Output Binding

This example shows how to define an output binding that writes to a blob container named output-container.


{
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "outputBlob",
      "type": "blob",
      "direction": "out",
      "path": "output-container/{name}.txt",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}
        

Example: Azure Service Bus Queue Output Binding

This example demonstrates an output binding for sending messages to an Azure Service Bus Queue named myqueue.


{
  "bindings": [
    {
      "name": "message",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "post"
      ]
    },
    {
      "name": "outputQueueItem",
      "type": "queue",
      "direction": "out",
      "queueName": "myqueue",
      "connection": "AzureWebJobsServiceBus"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}
        

Using Output Bindings in Code

The way you use output bindings depends on the language you are using.

JavaScript Example:

In JavaScript, the output binding is available as a context property. You can assign a value to it, and the runtime will handle the output.


module.exports = async function (context, req) {
    const name = (req.query.name || req.body.name || "World");
    const message = `Hello, ${name}!`;

    context.bindings.outputBlob = message; // Assign to the output binding defined in function.json

    context.res = {
        status: 200,
        body: `Message "${message}" sent to blob output.`
    };
};
        

C# Example:

In C#, you typically use output parameters or return types decorated with attributes.


using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

public static class BlobOutputFunction
{
    [FunctionName("BlobOutputFunction")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        [Blob("output-container/{Query.name}.txt", FileAccess.Write, Connection = "AzureWebJobsStorage")] out string outputBlob,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
        string name = req.Query["name"];
        string requestBody = new System.IO.StreamReader(req.Body).ReadToEnd();
        var data = Newtonsoft.Json.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}! Your message is being written to blob storage.";

        outputBlob = $"Processed message for: {name}. Content: {requestBody}"; // Assign value to the output parameter

        // For HTTP response, you would typically return an HttpResponseMessage or similar
        // This example focuses on the output binding itself.
    }
}
        

Binding to Return Value

For some languages, especially C# when using compiled models, you can bind an output binding to the function's return value. This can make your code even cleaner.


using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

public static class HttpAndBlobOutputFunction
{
    [FunctionName("HttpAndBlobOutputFunction")]
    // Bind the return value to a blob output
    public static CreatedResult Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
        [Blob("output-container/{Rand-guid}.json", FileAccess.Write, Connection = "AzureWebJobsStorage")] out string outputBlob,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
        string requestBody = new System.IO.StreamReader(req.Body).ReadToEnd();

        outputBlob = requestBody; // Assign the request body to the output blob

        // Return a CreatedResult, which is an HTTP response indicating success
        return new CreatedResult("/some/resource", "Data written to blob storage.");
    }
}
        

Note on Connection Strings

Output bindings often require a connection string to the target service. These are typically configured in your Azure Function App's application settings (e.g., AzureWebJobsStorage, AzureWebJobsServiceBus) and referenced by the connection property in your binding configuration.

Tip: Dynamic Output Paths

You can use tokens in your output binding paths, such as {name} from an HTTP trigger query parameter or request body, or {Rand-guid} to generate a unique identifier. This allows for dynamic file naming or message partitioning.

Supported Output Binding Types

Azure Functions supports a wide range of output bindings. Some of the most common include:

Type Description Example Use Case
blob Writes data to Azure Blob Storage. Saving processed images or log files.
queue Sends a message to an Azure Queue Storage queue. Queuing work for other services.
serviceBus Sends a message to an Azure Service Bus Queue or Topic. Decoupling microservices, publishing events.
cosmosDB Writes a document to an Azure Cosmos DB collection. Storing application state or analytics data.
table Writes an entity to Azure Table Storage. Storing simple key-value data.
sendGrid Sends an email via SendGrid. Notifications, user confirmations.
http Returns an HTTP response for an HTTP-triggered function. Standard web API responses.
signalr Sends messages to connected clients via Azure SignalR Service. Real-time updates for web applications.

Further Reading