Hi, Last week, I started explaining the 14-step Web API cheat sheet you can use to design amazing Web APIs. And explained the first 7 practices. As a reminder, here is the list of 14 good practices:
This week, we’ll cover the rest. 8. Meaningful response codesI worked on one project where many API endpoints always returned 200 OK status codes. No matter if the request was successful or not. As a result, you have more complicated code on the frontend where you need to examine the returned data to know whether the request has failed or not. But, using meaningful response codes in web API design is crucial for effective communication between the server and the client. Familiarize yourself and use a status code from one of the 5 following categories:
9. Implement security measuresOne of the most underrated qualities any Web API can have is strong security measures. Why? Because failing to implement proper security can lead to security breaches that cause:
There are many ways to implement security in .NET. However, one recent addition to .NET 8 is ASP.NET Core 8 Identity. It introduces new APIs to simplify login and identity management. After you configure it in a few lines of code, you automatically get endpoints for:
See the code below on how to set it up. 10. CachingCaching is a way to store frequently accessed data in memory. So, the next time, you don’t need to fetch the data from the database. Or perform complex and time-consuming calculations. This reduces the load on the server and decreases response time for clients. There are a few ways of caching on the server:
You can also use other caching techniques:
11. VersioningChange is the only constant in programming. The API endpoints don’t stand still once they are initially implemented. Sooner or later, you will have to make a change to it. Some changes are harmless. Some involve changing the API endpoint so that you might break the existing API clients. To prevent issues like that, use versioning. It’s a practice where you manage changes and updates to an API without breaking existing clients. With versioning you:
If you want to prevent having angry clients, start using API versioning. 12. Rate limitingOnce, I had an issue where one of the public endpoints occasionally got spammed. Instead of one document, the malicious user would create 10-20 documents in the system in a short period. As a result, we had to identify those and delete them from the database manually. This can be avoided with rate limiting. It’s a technique where you restrict the number of requests a client can make to the API within a specified time frame. Rate limiting is crucial for maintaining the stability, security, and performance of the API. .NET has a built-in rate limiter. 13. API testingIf you want to stop wasting hundreds of hours manually checking whether your API works, write Web API tests. These are the tests that:
There are many ways to implement API tests. But one of the most popular is by using Testcontainers + WebApplicationFactory combination. Here’s what the resulting test looks like. 14. DocumentationDocumentation is the primary guide for developers who want to use your API. It should explain:
High-quality documentation can significantly improve the developer experience, reduce the learning curve, and increase the adoption of your API. The default way to start documenting your APIs in ASP.NET Core has been Swashbuckle. However, it’s being removed in .NET 9. Instead of it, you will be able to use Microsoft.AspNetCore.OpenApi package. Have any questions? Hit reply and let me know. Enjoy your weekend.
|
Weekly newsletter packed with code-improving tips, tools, and strategies to become a better .NET developer.
Lately, I’ve been paying more attention to what’s happening in the AI space. Maybe because of all the hype that surrounds it. Maybe because of the anxiety of whether AI will take my software development job. That's why I’ve decided to spend some spare time during the Xmas holiday to explore the state of AI software development tools. This email combines: My 1+ years of experience using a paid version of GitHub Copilot. 5+ hours of YouTube videos I’ve consumed in the last 2 weeks. So, let’s...
Today's issue is brought to you by the C# 13 and .NET 9 – Modern Cross-Platform Development Fundamentals. Build confidence in creating professional and high-performance web applications using the latest technologies in C# 13 and .NET 9 by Mark Price. Find out more here: C# 13 and .NET 9 Yesterday, We had a company Xmas party. Before dinner at a restaurant, we went to the escape room event. If you are unfamiliar with escape rooms, they're interactive puzzle experiences where you and your...
2 weeks ago, .NET 9 was released. If you haven’t had time to read the official release docs, don’t worry. I spent 1 hour investigating what's new in .NET 9. So you don't have to. Here are the top 10 improvements for C#, ASP.NET Core, and EF Core. 1. LINQ Index LINQ has always been an extremely useful tool for .NET developers. However, with .NET 9, LINQ comes with 3 new methods. Let's begin with the LINQ Index. The Index method places every collection element against its position within that...