Create Simple Serverless API using Azure Portal in few minutes
Tags: cloudskills, showdev, azure, dotnet
Published: 2020-01-03 20:17:56

Want to create simple `Serverless` API in minutes ? Let's Jump In...

**Prerequisite:**

A free Account in Azure Portal

https://azure.microsoft.com/en-us/free/

**Our Agenda:**

Create Simple `Serverless API` which can receive `HTTP` request and return below `JSON`

    {"message":"Hello","name":"DEV.to"}



**Step 1: Setup Function App Deployment**


Go to portal https://portal.azure.com

Use the sidebar menu and Click `Function App`. 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/i7a99rnqq3um8t480y8d.png)

 Click Add and this will show below screen, Here select or enter `Resource Group Name`, `Enter Function App Name`, `Select Runtime Stack`, `Select Region` and Finally click `Review and Create`. It will take few minutes to setup deployment machine for your Function App.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/nzh6zwday1oy9i0qdqxr.png)


**Step 2: Choose Development Environment**

Once deployment is done, you need to click `plus` icon on left sidebar to start creating your `Function App`

Here we need to Choose Development Environment, in this case we are using `Azure Portal`

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ozh0t1jg6rkb2mkxu9h1.png)

**Step 3: Select Type of Function App**

A Function App can be of different type like a `Scheduler` or `Web Api` etc

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/37tq65azey1xv4vzbycd.png)

In this case we will select API and Click Create.

Once done you will see auto generated `Function App` with some `code` like below, Don’t worry about the `code` now, lets skip it, later we will write our own `code`

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ykzniy96zdlff477nny2.png)

Click on Integrate option from Sidebar

**Step 4: Change Function App Authorization level to Anonymous**


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/r0zfnx73u5x2l059cpdl.png)

 Select Anonymous from Authorization level drop down and finally Save the changes.

We are doing this just for demo purpose, In Real World App we will not allow Anonymous users access instead the user needs to pass some `code` or `key` to access the `Function App` API 


**Step 5: Write some Code and Go live**

Here we will write simple `C#` code to return some `Json`.



![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/gs571kivgj4k1mgjixus.png)

*Copy and Paste below code:*

    #r "Newtonsoft.Json"
    using System.Net;
    using Newtonsoft.Json;
    using System.Text;

    public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, 
    TraceWriter log)
    {

        var myObj = new {message = "Hello", name = "DEV.to"};
        var jsonToReturn = JsonConvert.SerializeObject(myObj);

        return new HttpResponseMessage(HttpStatusCode.OK) 
        {
        Content = new StringContent(jsonToReturn, Encoding.UTF8, 
        "application/json")
        };

    }


Save your code and Click get Function URL, Open the URL in browser and you will see below `JSON` result.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/nnaocdoea0l988xhblyw.png)

Congratulations for creating your first `Serverless` API. :)


**A Question to Azure professionals.**

Why there is option as Azure Portal Development Environment to create Function App? , In real world we will write more code like fetching 
from database etc. I think in that case Azure Portal option in not suitable. 

**What do you think?**

**Did this help ? Do you have any suggestions to improve this post ?**