Azure Functions Documentation

Introduction to Azure Functions Bindings

Azure Functions bindings provide a declarative way to connect your functions to other Azure services and external data sources. Instead of writing boilerplate code to interact with services like storage queues, service buses, or Cosmos DB, you can use bindings to simplify your function code. Bindings define how your function is triggered and what data it interacts with.

What are Bindings?

Bindings are composed of two primary types:

A single function can have multiple input and output bindings. Some bindings can serve as both input and output.

Trigger Bindings

A special type of input binding is a trigger. A trigger defines the event that causes your function to execute. For example, an HTTP trigger starts a function when an HTTP request is received, a timer trigger starts it on a schedule, and a queue trigger starts it when a new message arrives in a storage queue.

Declarative Approach

Bindings use a declarative model, meaning you define the connections in your function's configuration file (e.g., function.json for JavaScript and C# in-process, or attributes in code for C# out-of-process and other languages). This abstracts away the complexities of service SDKs and connection management.

Example: HTTP Trigger and a Storage Output Binding

Consider a simple HTTP-triggered function that receives a name via a query parameter and writes a greeting to a storage table:


// In function.json (for JavaScript)

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "table",
      "direction": "out",
      "name": "outputTable",
      "tableName": "greetings",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
            

// In index.js

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 greeting = name
        ? `Hello, ${name}! Your greeting has been recorded.`
        : 'Please pass a name on the query string or in the request body';

    context.bindings.outputTable = {
        PartitionKey: "GREETINGS",
        RowKey: Date.now().toString(),
        message: greeting
    };

    context.res = {
        status: 200,
        body: greeting
    };
};
            

In this example:

Benefits of Using Bindings

Azure Functions supports a wide range of built-in bindings for popular Azure services (e.g., Blob Storage, Cosmos DB, Service Bus, Event Hubs, Twilio) and third-party services. You can also create custom bindings if needed.