Azure Functions: Output Bindings
Tags: azure, azurefunctions
Published: 2022-10-30 16:27:03

---
title: Azure Functions: Output Bindings
published: true
date: 2022-10-30 12:25:51 UTC
tags: #azure #azurefunctions
canonical_url: https://blog.cloudbuff.in/azure-functions-part-7
---

This is the seventh article in this [series](https://dev.to/sjejurkar/up-and-running-with-azure-functions-1m8p) on Azure Functions. In this article we will take a look at output bindings.

The general pattern of a function is: receive data from some source, process it in some way and send it to some destination. Bindings are a way for Azure Functions to connect to data sources and destinations. They allow our code to receive data from some source (HTTP request, Storage Queue, etc.) and optionally send data to some destination (Storage Queue, Storage Blob, etc.).

There are two types of bindings:

- input binding - connection to input data source
- output binding - connection to output data destination

## Output binding

Let's consider an example. We have an HTTP endpoint and the data posted to this endpoint must be put into a Storage Queue. Here the trigger is HTTP request and output binding is Storage Queue. HTTP is the implicit input binding in this case.

First, let's go to Visual Studio code and start Azurite from the Command Palette.

![00_VSCode_azurite_start.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664731785680/fAIkx8Zop.png)

Then start Azure Storage Explorer and create a new queue called `binding-queue`.

![StorageExplorer_new_queue.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667062628117/su-3JvndY.png)

Let's add a new function called `BindingDemoFunction` as shown below (source code is available on [GitHub](https://github.com/sjejurkar/blog-azure-functions/blob/main/BindingDemoFunction.cs)).

![BindingDemoFunction_code.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667062652925/X1idrvNJO.png)

This function is similar to the `SimpleHttpFunction` we had created in a [previous article](https://dev.to/sjejurkar/up-and-running-with-azure-functions-part-2-150d) but there are some important differences.

Under `FunctionName` declaration, we have line `[return: Queue("binding-queue")]`. This declares an output binding of type `Queue` and queue name `binding-queue`.

Return type for `Run` method is `Task<string>` instead of `Task<IActionResult>`.

And the function returns a `string` instead of `OkObjectResult` object.

The method returns an string, the output binding takes the string, creates a message and puts it into `binding-queue` Storage Queue. Simple and straight forward.

## Test the function

Now, let's run the function. We should see the function URL in the logs displayed in VS Code terminal. ![BindingDemoFunction_run.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667062666194/qo1gNL60U.png)

We will use the popular [Postman](https://www.postman.com/downloads/) tool to test our function. Create a POST request with `http://localhost:7071/api/BindingDemoFunction` as URL and a simple JSON as the body. We should get back 200 status when the function runs successfully.

![Postman_request.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667062985072/4uUPHN0n5.png)

Let's go over to Storage Explorer and verify that the message has been put into the queue. We may have to click the Refresh button for the message to be visible.

![StorageExplorer_queue_message.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667062865318/3pTGVqrDB.png)

> Note: We have previously set `AzureWebJobsStorage` property as `UseDevelopmentStorage=true` in `local.settings.json` file. Hence the message is created correctly in the queue.

## Conclusion

In this article, we saw how to create an output binding for a Storage Queue and send a message to the queue from our Function. In the next article, we will see how to use Azure Key Vault to protect sensitive information such as connection strings, so our Function does not have direct access to it.