DEV Community

Cover image for Beyond Writing Endpoints: 4 Things I Consider When Building Production Ready .NET APIs
Abdullahi Suraj
Abdullahi Suraj

Posted on

Beyond Writing Endpoints: 4 Things I Consider When Building Production Ready .NET APIs

Building an API with ASP.NET Core is relatively straightforward.

You create controllers, define endpoints, connect a database, implement business logic and return responses.

But getting an API to work is not the same as building one that is reliable, secure and maintainable in production.

As software developers, we should think beyond whether an endpoint returns HTTP 200. We should also consider what happens when something goes wrong.

Here are four areas I believe deserve attention when building production ready .NET Web APIs.

1. Security should come first

Security shouldn't be an afterthought.

An API can work perfectly during development while still exposing sensitive operations or resources.

Some important considerations include:

  • Authentication: Who is making the request?
  • Authorisation: Is the user allowed to perform this operation?
  • Input validation: Are incoming requests valid?
  • Secrets management: Are credentials stored securely?
  • Rate limiting: Are endpoints protected against excessive requests?

One important distinction is that authentication doesn't automatically mean authorisation.

A user may be successfully authenticated but still shouldn't have access to every resource or operation.

Security needs to be considered at both the endpoint and business operation levels.

2. Handle errors consistently

Unexpected errors are inevitable in production systems.

Database connections fail, external services become unavailable, and unexpected inputs expose edge cases.

Returning different error formats across endpoints makes life difficult for API consumers.

ASP.NET Core provides exception handling middleware and Problem Details to help standardise error responses.

For example:

builder.Services.AddProblemDetails();

var app = builder.Build();

app.UseExceptionHandler();
app.UseStatusCodePages();
Enter fullscreen mode Exit fullscreen mode

A consistent error handling strategy makes APIs easier to consume and troubleshoot.

It's also important to avoid exposing internal exception details or sensitive information in production responses.

3. Logging and observability are essential

When an application runs locally, debugging is relatively straightforward.

In production, investigating an issue can be much more challenging.

Good logging should help answer questions such as:

  • What happened?
  • When did it happen?
  • Which operation was involved?
  • Did an external dependency fail?
  • Can we trace a request across multiple services?

Structured logging, correlation IDs, metrics and distributed tracing can make troubleshooting significantly easier.

Logging shouldn't simply mean writing messages whenever something happens. It should provide useful information that helps developers understand the behaviour of their applications.

4. Don't overlook testing and failure scenarios

An endpoint returning HTTP 200 doesn't necessarily mean the implementation is correct.

Consider what happens when:

  • A client sends invalid data.
  • A requested resource doesn't exist.
  • A user attempts an unauthorised operation.
  • A database becomes unavailable.
  • An external API times out.
  • A business rule is violated.

Unit tests help verify individual components, while integration tests help establish whether different parts of the application work together correctly.

For systems that depend on external services, consider resilience techniques such as timeouts, retries and circuit breakers.

However, retries should be implemented carefully, particularly for operations that could produce duplicate transactions or side effects.

The objective isn't to prevent every possible failure. It's to ensure the application behaves predictably when failures occur.

Final thoughts

Building production ready APIs involves much more than creating endpoints and connecting databases.

Security, error handling, observability and testing should all be considered throughout the development process.

Not every application needs a complicated architecture or every resilience pattern available. Engineering decisions should always reflect the actual requirements and complexity of the system.

But one thing is certain, an API that works is not necessarily an API that's ready for production.

I'd love to hear from other developers

What's one production issue you've encountered that changed how you approach API development?

Top comments (0)