Swagger Failed to Load API Definition: Fix 500 Fetch Error

Swagger UI opens, but instead of your endpoints it shows Failed to load API definition with a line such as Fetch error Internal Server Error /swagger/v1/swagger.json or Fetch error response status is 500 /swagger/v1/swagger.json. The page itself is fine. The UI could not download the swagger.json document behind it, and the HTTP status of that download tells you what is wrong. This post shows how to read that status and fix the six causes I could reproduce on .NET 10 with Swashbuckle.AspNetCore.

Animated table matching what the swagger.json URL shows to where to look: a 500 with exception text points to routes, HTTP attributes or schema ids; a 404 points to the environment or the path; JSON that loads points to the UI URL or CORS and CSP; works locally but not on the server points to the environment check and a relative URL. It is an illustration, not a screenshot.
Open swagger.json first, then match what you see (illustration, not a screenshot).

Swagger ‘Failed to load API definition’ fetch error: what it means

Swagger UI is a static page. When it starts, it requests the OpenAPI document (by default /swagger/v1/swagger.json) and draws it. If that request fails or returns something that is not valid JSON, the UI prints the message and the status. The status is the clue:

Short answer: the UI could not download swagger.json. Depending on the cause and the UI version, you see one of these:

Failed to load API definition.
Fetch error
Internal Server Error /swagger/v1/swagger.json

Fetch error response status is 500 /swagger/v1/swagger.json
Fetch error response status is 404 /swagger/v1/swagger.json
  • 500 / Internal Server Error: your app threw while generating the document. Causes 1 to 3.
  • 404 / Not Found: the document is not served at that URL. Causes 4 and 5.
  • The JSON opens fine in a new tab: the UI is asking for the wrong URL, or the browser blocks the request. Causes 5 and 6.

Step 1: open swagger.json and read the real error

Browse straight to https://localhost:<port>/swagger/v1/swagger.json (use the port from your launch settings, for example 5001). Do not rely on the UI message, because it hides the exception. With the Development environment, the 500 page shows the full exception text, and the same text is in the console where the app runs. Search that text for one of these phrases: Conflicting method/path combination, Ambiguous HTTP method or Can’t use schemaId. The same steps apply to any backend that serves an OpenAPI file, for example /v3/api-docs in Spring or /openapi.json in FastAPI: open the URL the UI is calling and read what the server returns. I only ran the .NET cases below.

Cause 1: two actions share the same route and HTTP method

This is the most common 500. Two actions that answer GET api/Orders make the document ambiguous:

[ApiController]
[Route("api/[controller]")]
public class OrdersController : ControllerBase
{
    [HttpGet]
    public IActionResult GetAll() => Ok();

    [HttpGet]                       // same route and verb as GetAll
    public IActionResult GetRecent() => Ok();
}

The swagger.json response on Swashbuckle 11.0.1 was:

Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Conflicting method/path combination
"GET api/Orders" for actions - OrdersController.GetAll (Api), OrdersController.GetRecent (Api).
Actions require a unique method/path combination for Swagger/OpenAPI 2.0 and 3.0.

Fix it by giving each action its own route:

[HttpGet]                  // GET api/orders
public IActionResult GetAll() => Ok();

[HttpGet("recent")]        // GET api/orders/recent
public IActionResult GetRecent() => Ok();

If you cannot change the routes yet, Swashbuckle can keep the first match. Treat this as a stop-gap, because the other action disappears from the documentation:

builder.Services.AddSwaggerGen(c =>
    c.ResolveConflictingActions(descriptions => descriptions.First()));

With this option in place the document loaded (HTTP 200) and listed only the first action.

Cause 2: a public controller method has no HTTP attribute

Every public method on a controller is an action. A helper without [HttpGet] or a similar attribute has no HTTP method, and the generator refuses it. This is the same page as the search for actions require an explicit HttpMethod binding:

public decimal CalculateTax(decimal amount) => amount * 0.13m;   // public, no attribute
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Ambiguous HTTP method for action - OrdersController.CalculateTax (Api).
Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0

Mark it with [NonAction], or better, make it private or move it into a service:

[NonAction]
public decimal CalculateTax(decimal amount) => amount * 0.13m;

Cause 3: two models have the same class name

Swashbuckle names each schema after the short class name. Two classes called Customer in different namespaces collide:

namespace Sales   { public class Customer { public string Name { get; set; } = ""; } }
namespace Billing { public class Customer { public string Account { get; set; } = ""; } }
System.InvalidOperationException: Can't use schemaId "$Customer" for type "$Billing.Customer".
The same schemaId is already used for type "$Sales.Customer".

Use the full name as the schema id. After this change the document loaded and contained Sales.Customer and Billing.Customer as separate schemas:

builder.Services.AddSwaggerGen(c =>
    c.CustomSchemaIds(type => type.FullName));

Cause 4: Swagger is only enabled in Development

The project template wraps Swagger in if (app.Environment.IsDevelopment()). On a server the environment is usually Production, so the UI route may not exist or swagger.json answers 404. I ran the same app with ASPNETCORE_ENVIRONMENT=Production and got 404 for /swagger/v1/swagger.json. Enable it for the environments you choose:

if (app.Environment.IsDevelopment() || app.Environment.IsStaging())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

A related mistake gives a very confusing result: if app.UseSwagger() is missing and only app.UseSwaggerUI() is present, the UI page loads (200) but swagger.json returns 404, which is exactly this error message. Keep both calls. Think before exposing API documentation publicly in production; enabling Swagger per environment shows the options.

Cause 5: the UI points to the wrong URL

If the app runs under a sub-path, for example an IIS virtual directory or behind a reverse proxy at /MyService/, an absolute endpoint such as /swagger/v1/swagger.json points at the site root instead of your app. A relative endpoint is resolved from the UI page, so it keeps the prefix:

app.UseSwagger();
app.UseSwaggerUI(c =>
    c.SwaggerEndpoint("v1/swagger.json", "My API v1"));   // relative

I checked the generated UI configuration: the absolute form produced "url":"/swagger/v1/swagger.json" and the relative form produced "url":"v1/swagger.json". I did not run the app behind IIS or a proxy.

If you change the JSON route, the template must contain {documentName} and the UI must use the same path. The document name is case-sensitive, so v1 in SwaggerDoc("v1", ...) must match v1 in the URL.

app.UseSwagger(c => c.RouteTemplate = "docs/{documentName}/swagger.json");
app.UseSwaggerUI(c =>
{
    c.RoutePrefix = "docs";
    c.SwaggerEndpoint("v1/swagger.json", "My API v1");
});

Using the built-in OpenAPI support instead of Swashbuckle? Then the document is at /openapi/v1.json, not /swagger/v1/swagger.json, so point the UI there (Use the generated OpenAPI documents (Microsoft Learn)). The details are in the AddSwaggerGen and AddOpenApi guide.

Cause 6: CORS or Content Security Policy blocks the request

If swagger.json opens directly but the UI still fails, press F12 and read the Console and Network tabs. A CORS or CSP message means a security header stops the UI from loading the document or its own scripts. See Content Security Policy for Swagger UI. If the Network tab shows the request succeeding with a login page, an authentication filter is redirecting it, so exclude the swagger path from it.

What was tested and what was not

  • Run on macOS with .NET SDK 10.0.401 and Swashbuckle.AspNetCore 11.0.1: causes 1, 2 and 3 (the 500 responses quoted above) and their fixes, the ResolveConflictingActions stop-gap, cause 4 (404 in Production, 200 in Staging after the change), the missing UseSwagger call, and the generated UI configuration for absolute and relative endpoints.
  • Not tested: an app hosted under IIS or behind a reverse proxy, the CORS and CSP cases in a real browser, the authentication redirect, Swagger UI in other frameworks (Spring, FastAPI), and older Swashbuckle versions. The exact wording of the exception can differ between versions.

Official references: Get started with Swashbuckle and ASP.NET Core (Microsoft Learn) and Swashbuckle.AspNetCore on GitHub.

Summary

  • The message only says the UI could not load the document. Open the swagger.json URL and read the status and exception.
  • 500 means a duplicate route, a public method without an HTTP attribute, or two models with the same name.
  • 404 means Swagger is not enabled in this environment, UseSwagger() is missing, or the URL is wrong; use a relative endpoint for sub-paths.

Found this useful?

Bookmark this page and share it with your team. Get new .NET, cloud and database tutorials in your inbox.

Swagger failed to load API definition FAQ

Why does Swagger show ‘Failed to load API definition’ after deployment but work locally?

Usually the environment check or the URL. The app runs in Production on the server, so Swagger is switched off and swagger.json returns 404, or the app sits under a sub-path and the UI asks for the wrong address. Open the swagger.json URL on the server to see which one it is.

What does ‘Fetch error response status is 500 /swagger/v1/swagger.json’ mean, and what about 404?

The server threw an exception while building the document. Open the swagger.json URL in the browser or check the app log to read it. The common causes are two actions with the same route and verb, a public method with no HTTP attribute, or two classes with the same name. A 404 instead means the document is not served at that address: Swagger is disabled in the environment, UseSwagger is missing, or the URL is wrong.

Does the Swagger UI show the same error for the built-in OpenAPI document?

Yes, the UI shows the same message whenever it cannot load the document. With AddOpenApi and MapOpenApi the document is at /openapi/v1.json, so the UI must be configured with that URL instead of /swagger/v1/swagger.json.

Which cause was it for you? Tell us in the comments.

31 thoughts on “Swagger Failed to Load API Definition: Fix 500 Fetch Error”

Leave a Reply

Your email address will not be published. Required fields are marked *