Welcome to my blog!

Api Best Practices

March 11, 2022 21:59

Api Best Practices (edit)

REST API Best Practices and Standards in 2022 (hevodata.com)


REST API Best Practices – REST Endpoint Design Examples (freecodecamp.org)


HTTP Method

Method Description
GET Used to retrieve a representation of a resource.
POST Used to create new new resources and sub-resources
PUT Used to update existing resources
PATCH Used to update existing resources
DELETE Used to delete existing resources

HTTP Status Code

Status Code range Meaning
100 – 199 Informational Responses.
For example, 102 indicates the resource is being processed
300 – 399 Redirects
For example, 301 means Moved permanently
400 – 499 Client-side errors
400 means bad request and 404 means resource not found
500 – 599 Server-side errors
For example, 500 means an internal server error

Common error HTTP status codes include:

  • 400 Bad Request – This means that client-side input fails validation.
  • 401 Unauthorized – This means the user isn’t not authorized to access a resource. It usually returns when the user isn’t authenticated.
  • 403 Forbidden – This means the user is authenticated, but it’s not allowed to access a resource.
  • 404 Not Found – This indicates that a resource is not found.
  • 500 Internal server error – This is a generic server error. It probably shouldn’t be thrown explicitly.
  • 502 Bad Gateway – This indicates an invalid response from an upstream server.
  • 503 Service Unavailable – This indicates that something unexpected happened on server side (It can be anything like server overload, some parts of the system failed, etc.).

13 Best Practices for Building RESTful APIs (viblo.asia)


1. OOP

OOP site:docs.microsoft.com

  1. Abstraction

  2. Inheritance

  3. Polymorphism

  4. Encapsulation

Classes, structs, and records | Microsoft Docs



SOLID site:docs.microsoft.com

  1. Single Responsibility principle

  2. Open/closed principle

  3. Liskov substitution principle

  4. Interface Segregation principle

  5. Dependency Inversion principle

Here are some relate articles you can refer to them:

SOLID With .Net Core

Solid Principles with C# .NET Core with Real-World Usage

How to apply SOLID principles with practical examples in C#

Sample: order-fullfillment-solid-practice

Designing the microservice application layer and Web API | Microsoft Docs


3. Custom Wrapper

ASP.NET Core and Web API: A Custom Wrapper for Managing Exceptions and Consistent Responses (vmsdurano.com)


NuGet: Swashbuckle

Code Samples

GlobalConfiguration.Configuration.Filters.Add(new CustomAuthorizeFilter());

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();


[EnableCors(origins: "*", headers: "*", methods: "*", SupportsCredentials = true)]
public class BaseApiController : ApiController { ... }

protected string GetModelStateErrorMessage()
var sb = new StringBuilder();
foreach (ModelState modelState in ModelState.Values)
foreach (ModelError error in modelState.Errors)
return sb.ToString();


Recent posts