Tech Talk Hub

Design, Build High Performance Applications

Create RESTful API with Azure Functions and MongoDB

In today’s topic, we’re going to build a RESTful API using HTTP Triggers in Azure Functions that uses a MongoDB Atlas (NOSQL database).

We’re going to build this API using C#, so you’d need to at least understand the C# syntax to follow along. I’m not going to go dive into huge detail about Azure Functions and the complexities behind MongoDB, I’ll just be keeping it simple enough to demonstrate how they can all work together to make a simplistic API.

So, let us try to understand couple of terms which we are going to use in this article like Azure Functions and Mongo DB.

What are Azure Functions?

Azure Functions are small pieces of code that we can run in Azure without worrying too much about application infrastructure (this ‘worry’ varies from scenario to scenario. It really depends on the situation and your requirements).

We can use specific events to trigger actions in our Functions. For this tutorial, we’ll be triggering events based off HTTP requests.

What is MongoDB?

MongoDB is a NoSQL document database that provides developers with flexibility when it comes to indexing and querying. Data is stored in JSON documents, which allows us to change data structures over time.

MongoDB Atlas is essentially the managed service for MongoDB that we can use for our MongoDB clusters. We can host our MongoDB Atlas clusters on Azure, AWS or GCP.

Now, it is time to setup our sample project and we will start from Mongo DB cluster.

Set up the MongoDB Atlas Cluster

Before we start coding, we need to set up our MongoDB Atlas cluster. The MongoDB documentation has a fantastic guide on how to set up your Atlas cluster, so if you haven’t got one already, check out this guide.

Create the Azure Function

Once you’ve created your cluster, let’s create our Azure Function. I’m going to use Visual Studio 2019 to develop my function. In order to develop Azure Functions using Visual Studio, make sure that you have the Azure Development workload enabled.

Create a “New Project” and click on “Azure Functions”. Like I said earlier, we’re going to be doing this project in C# so make sure that’s the language selected in the templates.

VS – Azure Functions Selection

Now that you’ve selected the right template, give your API a name and set an easy to find location to save the project it.

Project Location

Now, we can select what type of Function that we wish to create. Since this is an API, I’m going to create a Function with HTTP Triggers. When you create HTTP Trigger Functions, you can set what Storage account that the Function will use along with the Authorization level.

We’re not going to be doing anything with Authorization here, so just set it to “Anonymous”. A quick comment about authorization, if you create a Function with a certain level but decide you need to change it later, you can do this in the code, so don’t fret about this setting too much. It’s just to help Visual Studio generate a template for you.

Azure Functions Setup

Click Create and congrats! You now have an Azure Function. It’s not great though so let’s do something about that.

Connect to MongoDB Cluster

Sweet, now that we’ve set up our function, let’s grab our connection string to our MongoDB Atlas cluster.

Head to your Atlas portal (if you’re not there already) and sign in. Click the ‘connect’ button as seen below:

MongoDB Cluster

Since we’re connecting an application to our cluster, select the Connect Your Application option for our connection method:

Mongo DB Connection String

Now we have to select a driver version. Choose “C#/.NET” as the driver and “2.5 or later” as the version. Copy the connection string as we’ll need it for later.

Mongo DB connection string

Azure Functions Project Setup:

Now, let’s create a new class called Startup.cs. This class will allow us to use Dependency Injection in our Functions.

Here in Startup class we are instantiating a Singleton instance of MongoClient so all functions can use it. All we need here is to get our connection setting for MongoDB Atlas cluster and pass it through as a string parameter.

I’ve put this actual connection string in my local.settings.json file which is picked up in the IConfiguration instance of config. It’s not a good idea to hard code connection strings or settings into your code, so this is one method in your code that you can use to protect secrets needed for your application.

Summary

In this topic, we learnt how we can build a really simple API using Azure Functions that uses MongoDB as a datastore. While this was a very simple project to do, hopefully, this tutorial has given you some ideas as to how you can use MongoDB in your Azure Functions.

If you want to see the whole sample, check out the code on GitHub!

If you have any questions, please let me know in the comment section below!

masoodwasim

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
www.000webhost.com