This Azure tutorial will discuss an Azure functions rest api example c#.
Suppose you need to create a REST API application. Then, you can create a simple REST API application using the Azure Function. It is really easy to do with the help of Azure Functions. We need to create multiple functions and then map them based on the operations like GET, POST, DELETE, etc
Azure Functions Rest API Example C#
Here, as part of the development activities, we will create the Azure Function App project with the latest IDE, i.e., Visual Studio 2019. We will deal with a Product list by implementing the REST API operation.
We will do the following operations with REST API implementation as part of our development activities
- Get all Products (GET api/Product)
- Get Product By ID(GET api/product/{id})
- Create a new Product(POST api/product)
- Update a Product(PUT api/product/{id})
- Delete a Product(DELETE api/product/{id})
Prerequisites
- Visual Studio 2019 with the Azure development workload must be installed on your machine. If you don’t have Visual Studio 2019 installed on your machine, install the Visual Studio 2019 now.
Creating a REST API with Azure Functions using Visual Studio 2019
Assuming you have installed Visual Studio 2019 with the Azure development workload, let’s start the development activity. First, we will create the Azure Function Project using Visual Studio 2019.
Open Visual Studio 2019

Click the Create a new project button on the Get Started window.

The next step is to select the template for the Project, search for the Azure Functions template, choose Azure Functions from the search results, and then click the Next button.

Please provide a name for your project, select the location where you want to save it and click Create in the Configure your new project window.

As we all know, a trigger is essential for an Azure Function. In this step, we need to select the trigger for our Azure Function. Select the HTTP trigger option on the window below, and choose the Storage Account (AzureWebJobsStorage) option as Storage Emulator. Then, select Anonymous as the Authorization level, and click the Create button.

The project was created successfully.

Create Model Class
Now, the next step is to create the Model Class. So, I have created a model class named Product. cs. It contains 3 class definitions. One is the Product class, which includes four properties: ID, ManufactureTime, ProductDescription, and IsSoldOut.
Public class Product
{
public string Id { get; set; } = Guid.NewGuid().ToString("n");
public DateTime ManufactureTime { get; set; } = DateTime.UtcNow;
public string ProductDescription { get; set; }
public bool IsSoldOut { get; set; }
}
The next class is named CreateProduct, and that contains one property, ProductDescription, as shown below
public class CreateProduct
{
public string ProductDescription { get; set; }
}
The third class is called UpdateProduct, containing ProductDescription and IsSoldOut. See below for the reference
public class UpdateProduct
{
public string ProductDescription { get; set; }
public bool IsSoldOut { get; set; }
}
Creating Product Method
Now, we have created all the Model class definitions above. Now is the time to develop all the necessary methods for each functionality. The idea is to create all the functions inside the Function1.cs class file. We will delete the default function code and replace it with our own function.
The first function is the CreateProduct, which contains the lines of code below.
If we try to understand this function, the First thing is that we are using the ILogger interface to log a message, i.e., “Creating a new Product Item”. Next, we read the body of the HTTP request into a string and, with the help of Newtonsoft.Json, deserialize it into an object of our CreateProduct class.
Next, create a new Product Item and copy the deserialized product description from our HTTP request body into it. We are adding the Product list items to the static list we created on the first line.
public static class Function1
{
public static readonly List<Product> productitems = new List<Product>();
[FunctionName("CreateProduct")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("Creating a new Product Item.");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var input = JsonConvert.DeserializeObject<CreateProduct>(requestBody);
var product = new Product() { ProductDescription = input.ProductDescription };
productitems.Add(product);
return new OkObjectResult(product);
}
}You can see the CreateProduct method below