Azure Functions Documentation

Output Bindings

Output bindings enable your Azure Functions to send data to other Azure services or external resources without requiring you to write explicit SDK code. This simplifies your function code, making it more declarative and easier to manage.

How Output Bindings Work

In Azure Functions, output bindings are defined in your function's configuration (e.g., function.json for JavaScript/C# scripts, or attributes in C# code). When your function executes successfully, the return value or an explicitly defined output parameter is passed to the configured output binding. The runtime then handles the process of writing this data to the target service.

Key Benefit

Output bindings abstract away the complexity of interacting with downstream services, allowing you to focus on your function's core logic.

Common Output Bindings

Here are some frequently used output bindings:

HTTP Response Binding

This is a special output binding that returns a value to the caller of an HTTP-triggered function. It's often bound to the name res.

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

In your function code (e.g., Node.js):

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
    };
};

Blob Storage Output Binding

Allows you to write data to a blob in Azure Blob Storage.

{
  "bindings": [
    {
      "name": "inputQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "outputBlob",
      "type": "blob",
      "direction": "out",
      "path": "output-container/{queueTrigger}.txt",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

The path can use tokens from the trigger (e.g., {queueTrigger}) or other input bindings to dynamically name the output blob.

Cosmos DB Output Binding

Used to insert or update documents in an Azure Cosmos DB collection.

{
  "bindings": [
    {
      "name": "inputDocument",
      "type": "httpTrigger",
      "direction": "in",
      "methods": ["post"],
      "authLevel": "anonymous"
    },
    {
      "name": "outputDocument",
      "type": "cosmosDB",
      "direction": "out",
      "databaseName": "ToDoList",
      "collectionName": "Items",
      "createIfNotExists": false,
      "connection": "CosmosDBConnection"
    }
  ]
}

The function can return a single object or an array of objects to be inserted.

Service Bus Output Binding

Enables sending messages to a Service Bus queue or topic.

{
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": ["post"],
      "authLevel": "anonymous"
    },
    {
      "name": "message",
      "type": "serviceBus",
      "direction": "out",
      "queueOrTopicName": "myoutputqueue",
      "connection": "ServiceBusConnection"
    }
  ]
}

In code, you would typically assign the message payload to context.bindings.message.

Connection Strings

Remember to configure the connection strings (e.g., AzureWebJobsStorage, CosmosDBConnection, ServiceBusConnection) in your Function App's application settings.

Defining Output Bindings

Output bindings can be defined in:

  • function.json file for script-based languages (JavaScript, Python, PowerShell, etc.).
  • Attributes in your code for compiled languages (C#, F#, Java).

Binding Expressions

You can use binding expressions to dynamically set properties of your output bindings, such as blob paths or queue names, based on input data or other binding values.

For more detailed information on specific output bindings and their configuration options, please refer to the official Azure Functions documentation.