Skip to content

Commit bf9cc2e

Browse files
committed
2 parents 4390fc7 + c964a1e commit bf9cc2e

File tree

1 file changed

+143
-29
lines changed

1 file changed

+143
-29
lines changed

articles/azure-functions/functions-triggers-bindings.md

Lines changed: 143 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Triggers and bindings in Azure Functions
33
description: Learn to use triggers and bindings to connect your Azure Function to online events and cloud-based services.
44
ms.topic: conceptual
5-
ms.date: 08/14/2023
5+
ms.date: 09/06/2024
66
ms.custom: devdivchpfy22, devx-track-extended-java, devx-track-js, devx-track-python, devx-track-ts
77
zone_pivot_groups: programming-languages-set-functions
88
---
@@ -11,11 +11,11 @@ zone_pivot_groups: programming-languages-set-functions
1111

1212
In this article, you learn the high-level concepts surrounding functions triggers and bindings.
1313

14-
Triggers cause a function to run. A trigger defines how a function is invoked and a function must have exactly one trigger. Triggers have associated data, which is often provided as the payload of the function.
14+
Triggers cause a function to run. A trigger defines how a function is invoked and a function must have exactly one trigger. Triggers usually also pass data into your function, as you would with method calls.
1515

16-
Binding to a function is a way of declaratively connecting another resource to the function; bindings may be connected as *input bindings*, *output bindings*, or both. Data from bindings is provided to the function as parameters.
16+
Binding to a function is a way of declaratively connecting another resource to the function; bindings either pass data into your function (an *input binding*) or enable you to write data out from your function (an *output binding*) using *binding parameters*. Your function trigger is essentially a special kind of input binding.
1717

18-
You can mix and match different bindings to suit your needs. Bindings are optional and a function might have one or multiple input and/or output bindings.
18+
You can mix and match different bindings to suit your function's specific scenario. Bindings are optional and a function might have one or multiple input and/or output bindings.
1919

2020
Triggers and bindings let you avoid hardcoding access to other services. Your function receives data (for example, the content of a queue message) in function parameters. You send data (for example, to create a queue message) by using the return value of the function.
2121

@@ -24,53 +24,167 @@ Consider the following examples of how you could implement different functions.
2424
| Example scenario | Trigger | Input binding | Output binding |
2525
|-------------|---------|---------------|----------------|
2626
| A new queue message arrives which runs a function to write to another queue. | Queue<sup>*</sup> | *None* | Queue<sup>*</sup> |
27-
|A scheduled job reads Blob Storage contents and creates a new Azure Cosmos DB document. | Timer | Blob Storage | Azure Cosmos DB |
28-
|The Event Grid is used to read an image from Blob Storage and a document from Azure Cosmos DB to send an email. | Event Grid | Blob Storage and Azure Cosmos DB | SendGrid |
27+
| A scheduled job reads Blob Storage contents and creates a new Azure Cosmos DB document. | Timer | Blob Storage | Azure Cosmos DB |
28+
| The Event Grid is used to read an image from Blob Storage and a document from Azure Cosmos DB to send an email. | Event Grid | Blob Storage and Azure Cosmos DB | SendGrid |
2929
| A webhook that uses Microsoft Graph to update an Excel sheet. | HTTP | *None* | Microsoft Graph |
3030

3131
<sup>\*</sup> Represents different queues
3232

3333
These examples aren't meant to be exhaustive, but are provided to illustrate how you can use triggers and bindings together.
3434

35-
### Trigger and binding definitions
35+
>[!TIP]
36+
>Functions doesn't require you to use input and output bindings to connect to Azure services. You can always create an Azure SDK client in your code and use it instead for your data transfers. For more information, see [Connect to services](functions-reference.md#connect-to-services).
3637
37-
Triggers and bindings are defined differently depending on the development language.
38+
## Trigger and binding definitions
3839

39-
| Language | Triggers and bindings are configured by... |
40-
|-------------|--------------------------------------------|
41-
| C# class library | &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decorating methods and parameters with C# attributes |
42-
| Java | &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decorating methods and parameters with Java annotations |
43-
| JavaScript/PowerShell/Python/TypeScript | &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;updating [function.json](./functions-reference.md) ([schema](http://json.schemastore.org/function)) |
40+
Triggers and bindings are defined differently depending on the development language. Make sure to select your language at the [top](#top) of the article.
4441

45-
For languages that rely on function.json, the portal provides a UI for adding bindings in the **Integration** tab. You can also edit the file directly in the portal in the **Code + test** tab of your function. Visual Studio Code lets you easily [add a binding to a function.json file](functions-develop-vs-code.md?tabs=nodejs#add-a-function-to-your-project) by following a convenient set of prompts.
42+
Bindings can be either input or output bindings. Not all services support both input and output bindings. See your specific binding extension for [specific bindings code examples](#bindings-code-examples).
4643

47-
In .NET and Java, the parameter type defines the data type for input data. For instance, use `string` to bind to the text of a queue trigger, a byte array to read as binary, and a custom type to de-serialize to an object. Since .NET class library functions and Java functions don't rely on *function.json* for binding definitions, they can't be created and edited in the portal. C# portal editing is based on C# script, which uses *function.json* instead of attributes.
44+
::: zone pivot="programming-language-csharp"
45+
For C# class library functions, triggers and bindings are configured by decorating methods and parameters with C# attributes, where the specific attribute applied might depend on the C# runtime model, as you can see in this Queue storage trigger example:
4846

49-
To learn more about how to add bindings to existing functions, see [Connect functions to Azure services using bindings](add-bindings-existing-function.md).
47+
### [Isolated worker model](#tab/isolated-process)
5048

51-
For languages that are dynamically typed such as JavaScript, use the `dataType` property in the *function.json* file. For example, to read the content of an HTTP request in binary format, set `dataType` to `binary`:
49+
This example shows the HTTP trigger definition (`HttpTrigger`) on the `Run` method for a function named `HttpExample` that returns a `MultiResponse` object:
50+
51+
:::code language="csharp" source="~/functions-docs-csharp/functions-add-output-binding-storage-queue-isolated/HttpExample.cs" range="11-14":::
52+
53+
This example shows the `MultiResponse` object definition which both returns an `HttpResponse` to the HTTP request and writes messages to a storage queue using a `QueueOutput` binding:
54+
55+
:::code language="csharp" source="~/functions-docs-csharp/functions-add-output-binding-storage-queue-isolated/HttpExample.cs" range="33-38":::
56+
57+
For more information, see the [C# isolated worker model guide](dotnet-isolated-process-guide.md#methods-recognized-as-functions).
58+
59+
### [In-process model](#tab/in-process)
60+
61+
This example shows the HTTP trigger definition (`HttpTrigger`) on the `Run` method for a function named `HttpExample` that writes to a storage queue defined by the `Queue` and `StorageAccount` attributes on the `msg` parameter:
62+
63+
:::code language="csharp" source="~/functions-docs-csharp/functions-add-output-binding-storage-queue-cli/HttpExample.cs" range="14-19":::
64+
65+
For more information, see the [C# in-process model guide](functions-dotnet-class-library.md#methods-recognized-as-functions).
66+
67+
---
68+
69+
Legacy C# Script functions use a function.json definition file. For more information, see the [Azure Functions C# script (.csx) developer reference](functions-reference-csharp.md).
70+
::: zone-end
71+
::: zone pivot="programming-language-java"
72+
For Java functions, triggers and bindings are configured by annotating specific methods and parameters. This example shows the HTTP trigger definition on the `run` method for a function named `HttpTriggerQueueOutput` where the trigger is defined in the `@HttpTrigger` annotation that writes to a storage queue defined by the `@QueueOutput` annocation on the `message` parameter:
73+
74+
:::code language="java" source="~/functions-quickstart-java/functions-add-output-binding-storage-queue/src/main/java/com/function/Function.java" range="16-23":::
75+
76+
For more information, see the [Java developer guide](functions-reference-java.md#triggers-and-annotations).
77+
::: zone-end
78+
::: zone pivot="programming-language-javascript,programming-language-typescript"
79+
The way that triggers and binding are defined for Node.js functions depends on the specific version of Node.js for Functions:
80+
81+
### [v4](#tab/node-v4)
82+
83+
In Node.js for Functions version 4, you configure triggers and bindings using objects exported from the `@azure/functions` module. For more information, see the [Node.js developer guide](functions-reference-node.md?pivots=nodejs-model-v4#inputs-and-outputs).
84+
85+
### [v3](#tab/node-v3)
86+
87+
In Node.js for Functions version 3, you configure triggers and bindings in a function-specific `function.json` file in the same folder as your code. For more information, see the [Node.js developer guide](functions-reference-node.md?pivots=nodejs-model-v3#inputs-and-outputs).
88+
89+
---
90+
91+
This example is an HTTP triggered function that creates a queue item for each HTTP request received:
92+
93+
### [JavaScript](#tab/javascript/node-v4)
94+
95+
:::code language="javascript" source="~/azure-functions-nodejs-v4/js/src/functions/storageQueueOutput1.js" :::
96+
97+
### [TypeScript](#tab/typescript/node-v4)
98+
99+
:::code language="typescript" source="~/azure-functions-nodejs-v4/ts/src/functions/storageQueueOutput1.ts" :::
100+
101+
### [JavaScript](#tab/javascript/node-v3)
102+
103+
This example `function.json` file defines the function.
52104

53105
```json
54106
{
55-
"dataType": "binary",
56-
"type": "httpTrigger",
57-
"name": "req",
58-
"direction": "in"
107+
"bindings": [
108+
{
109+
"type": "httpTrigger",
110+
"direction": "in",
111+
"authLevel": "function",
112+
"name": "input"
113+
},
114+
{
115+
"type": "http",
116+
"direction": "out",
117+
"name": "$return"
118+
},
119+
{
120+
"type": "queue",
121+
"direction": "out",
122+
"name": "myQueueItem",
123+
"queueName": "outqueue",
124+
"connection": "MyStorageConnectionAppSetting"
125+
}
126+
]
59127
}
60128
```
61129

62-
Other options for `dataType` are `stream` and `string`.
130+
### [TypeScript](#tab/typescript/node-v3)
63131

64-
## Binding direction
132+
This example `function.json` file defines the function.
65133

66-
All triggers and bindings have a `direction` property in the [function.json](./functions-reference.md) file:
134+
```json
135+
{
136+
"bindings": [
137+
{
138+
"type": "httpTrigger",
139+
"direction": "in",
140+
"authLevel": "function",
141+
"name": "input"
142+
},
143+
{
144+
"type": "http",
145+
"direction": "out",
146+
"name": "$return"
147+
},
148+
{
149+
"type": "queue",
150+
"direction": "out",
151+
"name": "myQueueItem",
152+
"queueName": "outqueue",
153+
"connection": "MyStorageConnectionAppSetting"
154+
}
155+
]
156+
}
157+
```
158+
159+
---
67160

68-
- For triggers, the direction is always `in`
69-
- Input and output bindings use `in` and `out`
70-
- Some bindings support a special direction `inout`. If you use `inout`, only the **Advanced editor** is available via the **Integrate** tab in the portal.
161+
::: zone-end
162+
::: zone pivot="programming-language-powershell"
163+
This example `function.json` file defines the function:
71164

72-
When you use [attributes in a class library](functions-dotnet-class-library.md) to configure triggers and bindings, the direction is provided in an attribute constructor or inferred from the parameter type.
165+
:::code language="json" source="~/functions-docs-powershell/functions-add-output-binding-storage-queue-cli/HttpExample/function.json" range="3-26":::
166+
167+
For more information, see the [PowerShell developer guide](functions-reference-powershell.md#bindings).
168+
::: zone-end
169+
::: zone pivot="programming-language-python"
170+
The way that the function is defined depends on the version of Python for Functions:
171+
172+
### [v2](#tab/python-v2)
173+
174+
In Python v2, you define the function directly in code using decorators:
175+
176+
:::code language="python" source="~/functions-docs-python-v2/function_app.py" range="4-9" :::
177+
178+
179+
### [v1](#tab/python-v1)
180+
181+
In Python v1, this example `function.json` file defines the function:
182+
183+
:::code language="json" source="~/functions-docs-powershell/functions-add-output-binding-storage-queue-cli/HttpExample/function.json" range="3-26":::
184+
185+
---
73186

187+
::: zone-end
74188
## Add bindings to a function
75189

76190
You can connect your function to other services by using input or output bindings. Add a binding by adding its specific definitions to your function. To learn how, see [Add bindings to an existing function in Azure Functions](add-bindings-existing-function.md).
@@ -85,7 +199,7 @@ Specific binding extension versions are only supported while the underlying serv
85199

86200
## Bindings code examples
87201

88-
Use the following table to find examples of specific binding types that show you how to work with bindings in your functions. First, choose the language tab that corresponds to your project.
202+
Use the following table to find more examples of specific binding types that show you how to work with bindings in your functions. First, choose the language tab that corresponds to your project.
89203

90204
[!INCLUDE [functions-bindings-code-example-chooser](../../includes/functions-bindings-code-example-chooser.md)]
91205

0 commit comments

Comments
 (0)