Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Swashbuckle returns 404 for swagger.json with nothing in the logs

Your error

Failed to load API definition.2 more lines it can show

Fix it fast

Most common cause, 1 of 3

Swagger middleware is inside the Development-only block

Decide where the document should be public.

Fix
// Program.cs
app.UseSwagger();
app.UseSwaggerUI();

# one-off local run (needs .NET SDK 9.0.200 or later)
dotnet run -e ASPNETCORE_ENVIRONMENT=Development
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

A 404 for /swagger/v1/swagger.json with no exception means Swashbuckle never matched the request. Microsoft's sample enables UseSwagger only in Development, so a server running as Production has no route. In an IIS virtual directory, a leading slash in SwaggerEndpoint points at the site root. Fix the environment check, use v1/swagger.json, and match the SwaggerDoc name (Swashbuckle 11).

  • Applies to Swashbuckle.AspNetCore 11.0.1, .NET 10.0.15, .NET 11.0.0
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Request the JSON document directly, at the address the app is really published under, and note the environment line the app prints at start-up. A 404 together with Production points to cause 1. A 200 at the full path while the UI still fails points to cause 2. A 404 even in Development points to cause 3.

Run this first
curl -s -o /dev/null -w "%{http_code}\n" https://api.example.com/myapp/swagger/v1/swagger.json
# start-up log line to look for: "Hosting environment: Production"

Then find the line you see in this list and open its fix:

  1. 1Swagger middleware is inside the Development-only blockThe app logs Hosting environment: Production and both /swagger and /swagger/v1/swagger.json return 404
  2. 2The UI asks for swagger.json at the site root instead of the app's sub-path/myapp/swagger/v1/swagger.json returns 200, but the UI shows Fetch error Not Found /swagger/v1/swagger.json
  3. 3The document name or route template does not match what the UI requests404 even in Development, and SwaggerDoc uses a name (v2, MyApi) or a RouteTemplate that differs from the SwaggerEndpoint URL

What does the error look like?

  • Failed to load API definition.
  • Fetch error
  • Not Found /swagger/v1/swagger.json

Cause 1 of 3

Swagger middleware is inside the Development-only block

You seeThe app logs Hosting environment: Production and both /swagger and /swagger/v1/swagger.json return 404

Check

swagger.json returns 404 on a server when UseSwagger sits inside if (app.Environment.IsDevelopment()), as in Microsoft's Swashbuckle tutorial. ASP.NET Core falls back to Production when neither ASPNETCORE_ENVIRONMENT nor DOTNET_ENVIRONMENT is set, and Microsoft notes that deployed apps normally run as Production.

Check
grep -n -B2 -A3 'UseSwagger' Program.cs

Fix

Decide where the document should be public. To serve it in every environment, move the two calls out of the if block. To test a deployed build locally as Development, set the environment for one run instead of changing code.

Fix
// Program.cs
app.UseSwagger();
app.UseSwaggerUI();

# one-off local run (needs .NET SDK 9.0.200 or later)
dotnet run -e ASPNETCORE_ENVIRONMENT=Development

Verify

The same curl now prints 200, and the browser shows the document's JSON.

Verify
curl -s https://api.example.com/swagger/v1/swagger.json | head -c 120

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

The UI asks for swagger.json at the site root instead of the app's sub-path

You see/myapp/swagger/v1/swagger.json returns 200, but the UI shows Fetch error Not Found /swagger/v1/swagger.json

Check

Behind an IIS virtual directory or a reverse proxy that adds a path, a SwaggerEndpoint that starts with a slash sends the browser to the true root of the host. Microsoft's tutorial calls out this case: the document lives under the virtual directory, so the root-relative request finds nothing.

Check
grep -n 'SwaggerEndpoint' Program.cs

Fix

Give SwaggerEndpoint a path relative to the UI page, as the Swashbuckle README does. The browser then resolves it under whatever base path the app is served from.

Fix
app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("v1/swagger.json", "My API V1");
});

Verify

Reload /myapp/swagger. The browser's network tab shows the request going to /myapp/swagger/v1/swagger.json with status 200.

Verify
curl -s -o /dev/null -w "%{http_code}\n" https://api.example.com/myapp/swagger/v1/swagger.json

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 3

The document name or route template does not match what the UI requests

You see404 even in Development, and SwaggerDoc uses a name (v2, MyApi) or a RouteTemplate that differs from the SwaggerEndpoint URL

Check

Swashbuckle serves each document at /swagger/{documentName}/swagger.json, where the name is the first argument to SwaggerDoc. Its middleware answers 404 when the requested name is unknown. A custom RouteTemplate moves the JSON, and the UI keeps calling the old address until you change it too.

Check
grep -nE 'SwaggerDoc\(|RouteTemplate|SwaggerEndpoint' Program.cs

Fix

Use one name in all three places. If you changed RouteTemplate, it must still contain {documentName}, and the UI endpoint has to follow the new path.

Fix
builder.Services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});

app.UseSwagger(options =>
{
    options.RouteTemplate = "api-docs/{documentName}/swagger.json";
});
app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("/api-docs/v1/swagger.json", "My API V1");
});

Verify

The JSON answers at the new address and the UI lists the v1 document without an error box.

Verify
curl -s -o /dev/null -w "%{http_code}\n" https://localhost:5001/api-docs/v1/swagger.json

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Add a smoke test to the deploy pipeline that requests swagger.json at the published base path and fails on anything but 200.
  • Keep the SwaggerDoc name in one constant and build both the RouteTemplate and the SwaggerEndpoint from it.
  • Write down in the README which environments expose the API document, so nobody expects it on a Production server.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Raheeb, Loade Driver, Dhero.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.