Web API  

Types of APIs in C# (.NET)

Below is a complete, clear, and real-time–friendly explanation of all important API types in C#, their definitions, how they work, and how to create each one with real-time examples.

In C#, APIs generally fall into 5 major categories:

  1. SOAP API – WCF (Windows Communication Foundation)

  2. REST API – ASP.NET Web API / ASP.NET Core API

  3. gRPC API – High-performance contract-based API

  4. Minimal API – Lightweight API in .NET 6+

  5. GraphQL API – Query-based API

Below you will find:
✔ Definition
✔ When to use
✔ Real-time example
✔ How to create (steps + sample code)

1. SOAP API using WCF (Legacy – XML Based)

Definition

SOAP stands for Simple Object Access Protocol.
It uses XML for request and response and follows strict rules.

Use Case

  • ✔ Banking systems

  • ✔ Insurance

  • ✔ Enterprise internal communication

  • ✔ When strict contract and security is required (WS-Security)

Real-Time Example

Example: "Loan Status API" exposed by a bank to other systems.

How to Create WCF SOAP API in C#

Step 1: Create WCF Service Application

File → New → Project → WCF Service Application

IService1.cs

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetLoanStatus(int loanId);
}

Service1.svc.cs

public class Service1 : IService1
{
    public string GetLoanStatus(int loanId)
    {
        return "Status: Approved";
    }
}

web.config

WCF exposes SOAP endpoints automatically.

2. REST API using ASP.NET Web API / ASP.NET Core API

Definition

REST (Representational State Transfer) uses:

  • ✔ JSON (default)

  • ✔ HTTP verbs (GET, POST, PUT, DELETE)

It is the most commonly used API today.

Use Case

  • ✔ Mobile apps (Android/iOS)

  • ✔ Web applications

  • ✔ E-commerce

  • ✔ Third-party integrations

  • ✔ Payment gateways

Real-Time Example

"E-commerce Order API", "Login API", "Product API".

How to Create REST API in ASP.NET Core

Step 1: Create Project

File → New → Project → ASP.NET Core Web API

Model

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

Controller

[ApiController]
[Route("api/[controller]")]
public class ProductController : ControllerBase
{
    [HttpGet]
    public IActionResult GetProducts()
    {
        return Ok(new List<string> { "Laptop", "Mobile" });
    }

    [HttpPost]
    public IActionResult AddProduct(Product p)
    {
        return Ok("Product Inserted");
    }
}

3. Minimal API (Introduced in .NET 6)

Definition

Minimal API is a super lightweight REST API created without controllers.

Use Case

  • ✔ Microservices

  • ✔ Small modules

  • ✔ IoT devices

  • ✔ Internal APIs

Real-Time Example

“Health Check API”
“Insert Logs API”

How to Create Minimal API

Program.cs

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/hello", () => "Hello World");
app.MapPost("/save", (Product p) => $"Saved {p.Name}");

app.Run();

Fastest way to expose endpoints.

4. gRPC API

Definition

gRPC is a high-performance, contract-first, binary communication protocol.
Uses Protocol Buffers (.proto) instead of JSON/XML.

Use Case

  • ✔ High-speed internal microservices

  • ✔ Real-time applications

  • ✔ Banking & stock trading apps

  • ✔ Communication between servers

Real-Time Example

"Stock price update service", "Chat service".

How to Create gRPC API in .NET Core

Step 1: Create Project

File → New → Project → gRPC Service

Proto File

protos/greet.proto

syntax = "proto3";

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply);
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

C# Service

public class GreeterService : Greeter.GreeterBase
{
    public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
    {
        return Task.FromResult(new HelloReply
        {
            Message = "Hello " + request.Name
        });
    }
}

5. GraphQL API

Definition

GraphQL allows the client to ask exactly what data it needs (query based).
Unlike REST where server decides the shape.

Use Case

  • ✔ Large data applications

  • ✔ Social media sites

  • ✔ Search applications

Real-Time Example

Facebook uses GraphQL for retrieving user feeds.

How to Create GraphQL API in C#

Install Package

HotChocolate.AspNetCore

Query.cs

public class Query
{
    public string Hello() => "Hello GraphQL";
}

Program.cs

builder.Services.AddGraphQLServer()
                .AddQueryType<Query>();

app.MapGraphQL();

Comparison of API Types

API TypeFormatSpeedSecurityBest For
SOAP (WCF)XMLMediumVery HighBanking, govt
REST APIJSONFastMediumWeb, mobile, ecommerce
Minimal APIJSONVery FastMediumMicroservices
gRPCProtoBufExtremely FastHighInternal microservices
GraphQLJSONFastMediumComplex data apps