Introduction: APIs Are Not Scary, They're Very Convenient
\n\nImagine you're ordering a pizza. You call the restaurant, give your address and order — and 30 minutes later, the delivery is at your door. An API in programming works exactly the same way: it's that "phone call" through which one program communicates with another. Only instead of a voice, there are HTTP requests, and instead of pizza, there's data.
\n\nIn the world of C# development, APIs are fundamental. Any modern project, whether it's a web application, mobile backend, or enterprise service, somehow "calls" external services. And here arises the main question: which protocol to choose? REST, GraphQL, or gRPC? Each is good in its own way, but each is for its own task.
\n\nIn this article, we'll break down working with APIs in C# using real examples. You'll learn how to send HTTP requests, how to make C# work with GraphQL and gRPC, and most importantly — you'll understand when to use what. Let's go!
\n\n\n\n
1. REST API in C#: A Classic That Never Gets Old
\n\nREST (Representational State Transfer) is an architectural style that has remained the de facto standard for many years. Simple, understandable, works over HTTP. If you've ever written code that sends GET or POST requests — you've already worked with REST.
\n\nHow to Send REST Requests in C#
\n\nIn C#, the HttpClient class is most commonly used for working with REST. It's built into .NET and can do everything: from simple GET requests to complex POST requests with JSON bodies.
Here's what a typical request to a REST API in C# looks like:
\n\nusing System;\nusing System.Net.Http;\nusing System.Threading.Tasks;\n\nclass Program\n{\n static async Task Main(string[] args)\n {\n using HttpClient client = new HttpClient();\n client.BaseAddress = new Uri("https://api.example.com/");\n \n HttpResponseMessage response = await client.GetAsync("users/1");\n if (response.IsSuccessStatusCode)\n {\n string json = await response.Content.ReadAsStringAsync();\n Console.WriteLine(json);\n }\n }\n}\n\n\nShort and clear. HttpClient lives long, so it's usually created once and reused (in real projects — via a DI container).
When REST Is Your Choice
\n- \n
- You're writing a public API for the web or mobile applications. \n
- Clients are browsers, mobile devices, IoT. \n
- You need simplicity and HTTP-level caching. \n
Cons of REST: data redundancy (often more data comes than needed) and the N+1 query problem, where multiple calls are required to get related data.
\n\n\n\n
2. GraphQL in C#: Request Only What You Need
\n\nGraphQL is a query language created by Facebook. Its main feature: the client decides which fields it needs. No extra information, no dozens of endpoints — one request, one response.
\n\nSending a GraphQL Request from C#
\n\nTo work with GraphQL in C#, you can use HttpClient directly, but it's more convenient to use the GraphQL.Client library (NuGet package GraphQL.Client).
Example of a request to a GraphQL API:
\n\nusing System;\nusing System.Threading.Tasks;\nusing GraphQL;\nusing GraphQL.Client.Http;\nusing GraphQL.Client.Serializer.Newtonsoft;\n\nclass Program\n{\n static async Task Main(string[] args)\n {\n using var client = new GraphQLHttpClient("https://api.example.com/graphql", new NewtonsoftJsonSerializer());\n\n var request = new GraphQLRequest\n {\n Query = @"\n {\n user(id: 1) {\n name\n email\n posts {\n title\n }\n }\n }"\n };\n\n var response = await client.SendQueryAsync<dynamic