DEV Community

karleeov
karleeov

Posted on

Building Serverless APIs with Azure Functions and .NET 8

Building Serverless APIs with Azure Functions and .NET 8

Introduction

Azure Functions combined with .NET 8 provides a powerful, scalable serverless platform for building APIs. In this tutorial, we will create a production-ready API from scratch.

Why Azure Functions?

  • Pay only for what you use - No idle costs
  • Auto-scale - Handles any load
  • .NET 8 support - Latest performance improvements
  • Easy deployment - GitHub Actions, VS Code, or CLI

Prerequisites

  • .NET 8 SDK
  • Azure Functions Core Tools
  • Azure subscription (free tier works)

Step 1: Create the Project

# Create new Azure Functions project
func init MyApi --worker-runtime dotnet
cd MyApi
func new --name GetProducts --template "HTTP trigger" --authlevel function
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure for .NET 8

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
</Project>
Enter fullscreen mode Exit fullscreen mode

Step 3: Build a Product API

Model

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Function

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

public class ProductFunctions
{
    private readonly ILogger<ProductFunctions> _logger;
    private static readonly List<Product> Products = new()
    {
        new Product { Id = 1, Name = "Azure Subscription", Price = 29.99m, Category = "Cloud" },
        new Product { Id = 2, Name = "DevOps Consulting", Price = 150.00m, Category = "Services" }
    };

    [Function("GetProducts")]
    public HttpResponseData GetProducts(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "products")] HttpRequestData req)
    {
        var response = req.CreateResponse();
        response.WriteAsJsonAsync(Products);
        return response;
    }

    [Function("GetProductById")]
    public HttpResponseData GetProductById(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "products/{id}")] HttpRequestData req,
        int id)
    {
        var product = Products.FirstOrDefault(p => p.Id == id);
        var response = req.CreateResponse();

        if (product == null)
        {
            response.StatusCode = System.Net.HttpStatusCode.NotFound;
        }
        else
        {
            response.WriteAsJsonAsync(product);
        }

        return response;
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Deploy to Azure

# Login to Azure
az login

# Create function app
az functionapp create \n  --name my-dotnet-api \n  --resource-group my-rg \n  --storage-account mystorage123 \n  --consumption-plan-location eastus \n  --runtime dotnet-isolated

# Deploy
func azure functionapp publish my-dotnet-api
Enter fullscreen mode Exit fullscreen mode

Best Practices

  1. Use Dependency Injection - Cleaner code
  2. Configuration - Environment variables
  3. Logging - Application Insights

Monitoring

Azure Functions provides built-in monitoring:

  • Application Insights
  • Azure Monitor
  • Log Analytics

Conclusion

Azure Functions with .NET 8 is an excellent choice for building serverless APIs. Perfect for MVP-focused developers!


Azure #DotNET #Serverless #Tutorial #Microsoft

Top comments (1)

Collapse
 
nsdtattoo profile image
Rokeya sakhawat

Great insights on building serverless APIs with Azure Functions and .NET 8. The combination offers excellent scalability, performance, and cost efficiency for modern cloud applications. I appreciate how this approach simplifies backend development while allowing developers to focus on writing clean, event-driven code without worrying about infrastructure management.