DEV Community

Shaiju T
Shaiju T

Posted on • Updated on

Create Simple Serverless API using Azure Portal in few minutes

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"}
Enter fullscreen mode Exit fullscreen mode

Step 1: Setup Function App Deployment

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

Use the sidebar menu and Click Function App.

Alt Text

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

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

Step 3: Select Type of Function App

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

Alt Text

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

Click on Integrate option from Sidebar

Step 4: Change Function App Authorization level to Anonymous

Alt Text

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

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")
    };

}
Enter fullscreen mode Exit fullscreen mode

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

Alt Text

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 ?

Top comments (2)

Collapse
 
aaronpowell profile image
Aaron Powell

Just to echo the reply to a comment posing your above question:

Every project and solution is different so will require a different approach to building it. Some projects may be suited to being created through the portal while others are suited to development through other tools.

Just like there's the az cli and Azure PowerShell commands, or GitHub Actions/Azure Pipelines/Jenkins/Bamboo/etc., it's all about giving people options to build solutions in a way that works for their needs.

Collapse
 
shaijut profile image
Shaiju T

Thanks , that's correct. :)