Getting Started with Ocelot: A Simple API Gateway Implementation

Ocelot is a popular API gateway solution for .NET developers. In this guide, I'll walk you through a basic implementation to get you started with Ocelot.Before beginning, ensure you have .NET Core installed. For this example, I'm using .NET Core 3.1 with Ocelot package version 16.0.1. Start by creating a new Web API project and installing the Ocelot package from NuGet.Step 1: Configure Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddOcelot();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseOcelot().Wait();
}

Note: The .Wait() method is optional but can improve safety in certain scenarios.Step 2: Create ocelot.json configuration fileStarting with Ocelot 16.x and latter, the configuration uses "Routes" instead of the older "ReRoutes" structure.

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/xxx/entity",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "your-domain-or-ip",
          "Port": 80
        }
      ],
      "UpstreamPathTemplate": "/test/xxx/entity",
      "UpstreamHttpMethod": [ "Get" ]
    },
    {
      "DownstreamPathTemplate": "/api/xxx/entity",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "your-domain",
          "Port": 443
        }
      ],
      "UpstreamPathTemplate": "/product/xxx/entity",
      "UpstreamHttpMethod": [ "Get" ]
    }
  ]
}

Step 3: Update Program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config =>
{
    config.AddJsonFile("ocelot.json", false, true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder.UseUrls("http://localhost:5400");
    webBuilder.UseStartup<Startup>();
});

That's it! You now have a working Ocelot API gateway setup. Advanced features like circuit breaking and request filtering will be covered in futture posts.

Tags: .NET Core API Gateway Ocelot Web API microservices

Posted on Thu, 02 Jul 2026 16:45:39 +0000 by rawky