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
.
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.
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
Step 3: Select Type of Function App
A Function App can be of different type like a Scheduler
or Web Api
etc
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
Click on Integrate option from Sidebar
Step 4: Change Function App Authorization level to Anonymous
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
.
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.
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)
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.Thanks , that's correct. :)