Sponsored
Many thanks to the sponsors who make it possible for this newsletter to be free for readers. Become a sponsor.
An API Gateway is a component of the app-delivery infrastructure that sits between clients and services and provides centralized handling of API communication between them. It also delivers security, policy enforcement, and monitoring and visibility across on-premises, multi-cloud, and hybrid environments. API Gateway acts as a front door for managing incoming API requests. This includes requests from various clients like web, mobile, or external third-party systems. Some of the key features of an API Gateway are:
The API Gateway is responsible for routing requests from clients to the appropriate microservice. This allows for a clean separation of concerns and decoupling of the client from the underlying microservices.
An API Gateway can translate between different protocols, such as HTTP and gRPC, making it easier to expose microservices over a variety of interfaces.
The API Gateway can also distribute incoming requests evenly across multiple instances of a microservice to improve performance and resilience.
The API Gateway can cache frequently requested data to reduce the load on the microservices and improve response times.
An API Gateway can provide security features, such as authentication, authorization, and encryption, to protect microservices from unauthorized access.
The API Gateway can collect and aggregate metrics and logs from the microservices and provide visibility into the health and performance of the system as a whole.

Ocelot is widely used in the .NET community as it integrates well with other .NET and ASP.NET Core features and services. It's a lightweight API Gateway, making it an attractive choice for us, .NET developers, looking to implement an API gateway without introducing a lot of additional complexity or overhead. You can see more details here.
To implement Ocelot API Gateway in .NET you should create at minimum 3 API Projects: Two microservices as use-case and the ApiGateway itself.
Here's how I did it:
There are 2 different .NET Web Api projects that represent microservices: Products and Users. These services will be called by the third Gateway project depending on what kind of request is needed.
Let's create controllers in each of the microservices with some default endpoints.
Products Microservice - I will create a ProductsController with only one endpoint that returns a list of products - in this case, it will be a hardcoded list of 3 products:
[ApiController][Route("/api/[controller]")]public class ProductsController : ControllerBase{ private static readonly List<string> Products = [ "Apple", "Milk", "Juice" ]; private readonly ILogger<ProductsController> _logger; public ProductsController(ILogger<ProductsController> logger) { _logger = logger; } [HttpGet(Name = "GetProducts")] public List<string> Get() { return Products; }}
UserMicroservice - In a similar way, I will create the UsersController:
[ApiController][Route("/api/[controller]")]public class UsersController : ControllerBase{ private static readonly List<string> Users = [ "Stefan", "Dajana", "Milan" ]; private readonly ILogger<UsersController> _logger; public UsersController(ILogger<UsersController> logger) { _logger = logger; } [HttpGet(Name = "GetUsers")] public List<string> Get() { return Users; }}
We have microservices ready, we can focus on creating the Gateway. Before everything, we need to install an Ocelot package from NuGet Package Manager in Gateway project. The first thing to do to set up the Ocelot API Gateway in .NET is to create an ocelot.json configuration file.
{ "GlobalConfiguration": {}, "Routes": [ {} ]}
So, what is ocelot.json file? This file serves as the setup instructions for the API Gateway, consisting of two main parts: a collection of Routes and a GlobalConfiguration.
{ "Routes": [ { "DownstreamPathTemplate": "/api/Products", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5123 } ], "UpstreamPathTemplate": "/api/Products", "UpstreamHttpMethod": ["Get"] }, { "DownstreamPathTemplate": "/api/Users", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5233 } ], "UpstreamPathTemplate": "/api/Users", "UpstreamHttpMethod": ["Get"] } ], "GlobalConfiguration": { "BaseUrl": "http://localhost:5165" }}
Explanation:
builder.Services.AddOcelot(builder.Configuration);
## What next?This was a fairly simple demonstration of an Ocelot API Gateway implementation with 2 microservices.Ocelot offers many more features, such as [Rate Limiting](https://thecodeman.net/posts/how-to-implement-rate-limiter-in-csharp), Authentication, Cashing and many more.If you liked this issue, feel free to suggest if you want Part 2 with a little more advanced stuff.Until then, you can see the full code for today's issue at the following [GitHub repository](https://github.com/StefanTheCode/OcelotApiGatewayDemo).That's all from me today. See ya on the next Monday coffee. ## Wrapping Up <!--END-->
Stop arguing about code style. In this course you get a production-proven setup with analyzers, CI quality gates, and architecture tests — the exact system I use in real projects. Join here.
Not sure yet? Grab the free Starter Kit — a drop-in setup with the essentials from Module 01.
Design Patterns that Deliver — Solve real problems with 5 battle-tested patterns (Builder, Decorator, Strategy, Adapter, Mediator) using practical, real-world examples. Trusted by 650+ developers.
Just getting started? Design Patterns Simplified covers 10 essential patterns in a beginner-friendly, 30-page guide for just $9.95.
Every Monday morning, I share 1 actionable tip on C#, .NET & Architecture that you can use right away. Join here.
Join 20,000+ subscribers who mass-improve their .NET skills with actionable tips on C#, Software Architecture & Best Practices.
Subscribe to the TheCodeMan.net and be among the 20,000+ subscribers gaining practical tips and resources to enhance your .NET expertise.