Understanding Web APIs: How to Build and Use Them in ASP.NET Core

Abhishikth ThulAbhishikth Thul
4 min read

✅ 1. Introduction

If you’ve ever ordered food at a restaurant, you already understand how an API works 🍽️.

An API is like the waiter — it takes your request ("I want pizza") to the kitchen (the backend/server), and returns your food (the data) back to you.

In web development, APIs are how different applications and services talk to each other.

For me, the real "Aha!" moment came when I built my first API — a simple weather app that fetched live data from a public API. Suddenly, it clicked: “This is how real apps work under the hood!”

In this blog, I’ll walk you through what APIs are, how to build one in ASP .NET Core, and how to test them like a pro — all with real examples.


🧠 2. What is a Web API?

API stands for Application Programming Interface. In simple terms:

🗣️ It's a messenger that lets one program talk to another.

Example:

GET https://api.weatherapi.com/v1/current.json?q=Mumbai

This call asks for Mumbai's current weather — and the response is structured, machine-readable JSON.

APIs let your frontend (React/HTML) talk to your backend (C#/Python/etc.), or allow apps to consume third-party services like weather, maps, payments, etc.


🌐 3. What is ASP .NET Core Web API?

ASP .NET Core is Microsoft’s modern framework for building powerful web apps and APIs — all using C# and .NET.

What makes it awesome:

  • ✅ Fully cross-platform (.NET 6+)

  • ✅ Built-in Swagger support for testing

  • ✅ Super clean with Minimal APIs (less boilerplate)

  • ✅ Supports JSON, RESTful design, and HTTP methods

You can use it with React, Angular, mobile apps, or even as a backend for IoT devices!


⚙️ 4. Setting Up a Web API Project

Let’s build one!

💻 Option A: Using Visual Studio

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

  • Select .NET 6 or .NET 8

  • Uncheck “Enable OpenAPI” if you want to start bare-bones (optional)

💻 Option B: Using CLI

dotnet new webapi -n MyWeatherApi
cd MyWeatherApi
dotnet run

You’ll see this in your terminal:

Now listening on: https://localhost:5001

Your API is alive !


💻 5. Creating Your First Endpoint

Let's add a classic "hello world" to your Program.cs:

app.MapGet("/hello", () => "Hello from API!");

Visit https://localhost:5001/hello

You’ll see:

Hello from API!

✅ That’s your first API!


🧪 6. Testing with Postman

Postman is like your API playground.

It lets you:

  • Send requests

  • See responses (JSON, errors, status codes)

  • Try GET, POST, etc.

Example:

  • Method: GET

  • URL: https://localhost:5001/hello

🟢 Response:

"Hello from API!"

You can also use Swagger UI at /swagger — it’s built-in when you use AddSwaggerGen()!


🔐 7. Real-Life API Example: Weather Endpoint

Let’s call a real weather API using HttpClient:

app.MapGet("/weather", async () =>
{
    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Add("User-Agent", "MyApp");

    string city = "Mumbai";
    string apiKey = "your_real_key_here";
    string url = $"https://api.weatherapi.com/v1/current.json?key={apiKey}&q={city}";

    var response = await httpClient.GetAsync(url);
    var result = await response.Content.ReadAsStringAsync();

    return response.IsSuccessStatusCode
        ? Results.Content(result, "application/json")
        : Results.Problem("Failed to fetch weather.");
});

🔍 What It Does:

  • Sends a GET request to WeatherAPI

  • Reads JSON response

  • Returns it to your browser or Postman


🧠 8. Understanding HTTP Verbs

APIs use HTTP methods to perform actions:

VerbMeaningExample
GETRead data/students
POSTAdd data/login, /add-student
PUTUpdate data/students/1
DELETEDelete data/students/2

These match the CRUD operations:

  • Create

  • Read

  • Update

  • Delete


💡 9. Tips for Clean APIs

Here are some tips from my own learning:

✅ Use clear endpoint names: /students, /login, /weather
✅ Define model classes like Student, User
✅ Return the right status codes (200, 401, 404)
✅ Keep logic clean and readable

Advanced? You can separate code into Controllers, use DTOs, and plug into databases later.


🔄 10. What’s Next?

Now that you’ve got your first APIs running:

  • Want to connect them to a React frontend?

  • Want to store data in a SQL Server or MongoDB?

  • Add JWT authentication and role-based access? 🔐

APIs are the heart of modern apps — and you’ve just learned how to make them beat.


💻 11. Final Thoughts

Building your own APIs can feel magical — like giving your apps a voice 🗣️.

With ASP.NET Core, it’s super accessible, fast, and beginner-friendly. If you’ve followed this blog and created even a single endpoint — congrats! 🎉 You're on the path to becoming a backend developer.

Got questions? Want to go deeper into auth or database APIs? Drop a comment or hit me up — let’s learn together! 🙌

0
Subscribe to my newsletter

Read articles from Abhishikth Thul directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhishikth Thul
Abhishikth Thul