Let’s create an Azure Function that can retrieve messages from a Cosmos Db.

Photo by Clarisse Meyer on Unsplash

Azure Functions doesn’t support MongoDb API, so make sure you’ve chosen SQL API.

Create a Azure Function App:

If you are using Visual Studio Code

If you are using Visual Studio

Azure Function Apps acts as a project folder for Azure Functions. They have their own URL, so your Function App needs to have a unique name, as this is used in the URL generated. See the Function App as an App Service.

Let’s create the function!

We have a lot of triggers to choose from, but let’s keep this simple and choose HTTP Trigger

For the language i prefer JavaScript, but you could choose C# or F#.

Give your function a name, like “GetMessages” and choose Function as Authorization level.

Determines what keys, if any, need to be present on the request in order to invoke the function. The authorization level can be one of the following values: anonymous —No API key is required. function —A function-specific API key is required. This is the default value if none is provided. admin —The master key is required. For more information, see the section about authorization keys.

Now, let’s integrate with Cosmos Db! Click “Integrate” below your newly created function.

In the top, we have some values and triggers we can choose from.

The HTTP (res) trigger is chosen as we chose a HTTP Trigger for our function.

Let’s add a new Input, so click the “New Input” button.

Choose Azure Cosmos DB

Now it’s time to change the configuration to match your Cosmos Db. Remember that Azure Functions doesn’t support MongoDb Api yet.

This is my current configuration

Click save and now let’s configure the Outputs section.

The only change we need to make here, is to check the “ Use function return value” checkbox and click save.

If “Use function return value” isn’t checked, you won’t get a response output from your function.

It’s time to write your function code. This sounds hard, but the Cosmos Db input we just configured, made this very simple!

Click on your function, in my regards, it’s: GetMessages.

We are now taken to an editor with two files: function.json and index.js. function.json is were our configuration is, the configuration we’ve just did in “Integrate”. index.js is our actual Azure Function.

Right now our function is configured as a POST, so let’s change that up a bit.

In the tab “Test”, delete the Request body and change HTTP Method to a “GET”.

Now get ready to be exited. You’ve thought we had to write a lot of auth and stuff for the Cosmos DB right?

Pretty cool right?

Let’s test this out, and click the Run button.

I’m pretty exited about how easy that was and I can’t wait to test this out even more!

If you liked this article and want me to write more of these, please leave a comment or contact me on Twitter :-)