Creating Serverless REST API with Azure Functions using nodeJS - HTTP Trigger
Tags: azure, azurefunctions, node, serverless
Published: 2020-09-20 07:39:39

Hello guys, in this article I am gonna briefly talk about creating serverless REST API with azure functions using nodeJS right from creating an azure account to deploying and consuming the API. I am not gonna talk much about serverless compute and its benefits, as I guess there are plenty of articles that talk in great length about it. So let's dive into it.

Microsoft Azure provides a lot of cloud computing services using which we can build some components of our application with ease and more flexibility. Like other cloud service providers, you can upscale and downscale your resources with a mere click of a button. I would strongly recommend you to use VSCode while working on Azure Functions, as it contains lots of extensions which will make our life easy. Azure functions can now be written in five Stacks:
* .NET Core
* Node.js (We are gonna use this to create functions:relaxed:)
* Python
* Java
* Powershell Core

## Creating and configuring Functions in VSCode

First, create an account on [Azure](http://portal.azure.com/). Microsoft provides a free account to play with its services, try to make use of it, and understand the core concepts before using it in your product. Once you create your account in Azure, we can then directly use VSCode for all the other things. You need the following extensions in VSCode:
* [Azure Account](https://marketplace.visualstudio.com/items?itemName=ms-vscode.azure-account)
* [Azure Functions](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions)

Now you need to sign in to your Azure account from VSCode, launch the command palette(Press Ctrl+Shift+P), and type _Azure: Sign In_ and hit enter to log in.
![Azure Login From VSCode](https://dev-to-uploads.s3.amazonaws.com/i/lj9ykph5vsm4u2gh559g.png)

Once you complete your login, let us now create our first Azure Function by opening the command palette again and type _Azure Functions: Create Function_ 
![Create Azure Function From VSCode Cmd Palette](https://dev-to-uploads.s3.amazonaws.com/i/udcm4401lnubw678zntk.png)
This will then trigger a couple of prompts asking you to 
* Select your programming language (choose javascript)
* Select your template (Choose HTTP Trigger, this makes your function to be a REST API)
* Give a name for your function (I gave _azure-http-test_)
* Select authorization level (Choose Function)

And then your function app is created.
![Function App Created](https://dev-to-uploads.s3.amazonaws.com/i/zquxyihzx36sp792x82b.png)

Let us breakdown the folder structure we got:
* _package.json_ - Since we choose javascript as our programming language, this was created. We can now treat this whole app as a normal node application.
* _host.json_ - Default configuration for our app (Better leave as it is now).
* _local.settings.json_ - Environment variables when you run your app locally for development.
* _azure-http-test_ - This folder is a REST API endpoint and it contains two files:
     * function.json - This is the configuration file for this API which contains bindings where you can define the method(GET, POST...,) of your REST API and URL here.
     * index.js - This where you write your logic for your API.

Let us now modify our app to make it like a simple POST API by changing the methods to _post_ and adding a route to bindings in function.json. This route will be the path for this endpoint. You can access the body sent through POST API from the _req.body_ object in index.js. Incase of GET API, after defining the parameters in bindings array of function.json, you access it in index.js through _context.bindingData.parameterName_ .

![Change Bindings](https://dev-to-uploads.s3.amazonaws.com/i/cyvold6rb5wxea2phz85.png)
You can run the app in debug mode by pressing F5. Once you run the app, the terminal will show you the local endpoint for testing.

## Deploying Functions to Azure

We have successfully created a simple POST API using azure functions, now let's do the deployment into Azure. Open the command palette again and type _Azure functions: Deploy to Function App_.
![Deploy Azure Function](https://dev-to-uploads.s3.amazonaws.com/i/ldtzh9yfzvijtsclp9it.png)
This will then ask you to select your subscription, once you select your subscription, the next prompt will ask for selecting the function app, if you haven't created any function app, select _Create new Function App in Azure (Advanced)_ . This will then trigger a couple of prompts:
* Enter a unique name for your function app
* Select a runtime stack (choose Node.js)
* Choose an OS
* Choose a hosting plan (Use App service plan for free plan)
* Choose an OS Service plan
* Choose a Resource Group (It's for grouping your services, you can use this for sharing resources or billing purposes)
* Choose a Storage Account (It holds all your blobs, files, etc..,)
* Choose an Application Insights Resource (It helps you in monitoring your services, you can skip it if you want).

Once you complete all the above fields, the deployment will start, it will take a few minutes and then you can head back to your [Azure](http://portal.azure.com/) portal to see the deployed function.
![Functions Page](https://dev-to-uploads.s3.amazonaws.com/i/cqqbdql0v45jgrri3h9c.png)

_Et voilà!_, you have successfully created and deployed your first serverless API, you can get the endpoint for your function by going into that function's overview page and click _Get Function Url_. You can see the logs from the Monitor section and you can set your environment variables for the different environment from the azure portal itself, go to _configuration_ of your function app, and set the environment variables in the _Application settings_ tab.

Azure functions have many other templates, which we will see in my next articles.

Thanks for reading.