Follow this guide to level up your .NET Career! A well-structured roadmap on what you need to know to become a Star .NET Developer in 2024 and beyond!
To read more practical articles, subscribe to my newsletter, where I have started a .NET 8 Zero to Hero Series: https://t.co/BZNvyzpTgd
#dotnet #developer #roadmap #dev #microsoft
How do you design a good API?
Here are 5 tips for designing a quality API.
API design is deciding how your API will expose data and functionality to consumers. A good API design describes the API endpoints and resources in some standard format.
There are 4 key stages of API design:
1 - Determine what the API should do
2 - Define the API contracts (OpenAPI)
3 - Validate your assumptions with tests
4 - Document the API (endpoints, error codes, etc.)
Here are 5 more tips for designing a good API.
1) ๐ฃ๐ฟ๐ถ๐ผ๐ฟ๐ถ๐๐ถ๐๐ฒ ๐๐ผ๐ป๐๐ถ๐๐๐ฒ๐ป๐ฐ๐ ๐ฎ๐ป๐ฑ ๐ฆ๐ถ๐บ๐ฝ๐น๐ถ๐ฐ๐ถ๐๐
Your API should have consistent naming conventions, response formats, and an error-handling strategy. Simplicity in API design makes it easier for developers to understand and integrate with your API.
2) ๐๐บ๐ฏ๐ฟ๐ฎ๐ฐ๐ฒ ๐ฅ๐๐ฆ๐ง๐ณ๐๐น ๐ฃ๐ฟ๐ถ๐ป๐ฐ๐ถ๐ฝ๐น๐ฒ๐
You should design your API according to RESTful principles, emphasizing statelessness, a client-server architecture, and a uniform interface. REST APIs are the standard in most .NET applications.
3) ๐จ๐๐ฒ ๐๐ต๐ฒ ๐ฐ๐ผ๐ฟ๐ฟ๐ฒ๐ฐ๐ ๐๐ง๐ง๐ฃ ๐ฆ๐๐ฎ๐๐๐ ๐๐ผ๐ฑ๐ฒ๐
You should use the correct HTTP status codes to communicate the outcome of API requests. This includes successful operations (2xx), client errors (4xx), and server errors (5xx). It helps consumers of your API understand what went wrong and how to rectify it.
4) ๐๐บ๐ฝ๐น๐ฒ๐บ๐ฒ๐ป๐ ๐๐ฃ๐ ๐ฉ๐ฒ๐ฟ๐๐ถ๐ผ๐ป๐ถ๐ป๐ด
Plan for future changes by implementing versioning in your API. This allows you to make improvements and changes without breaking existing client integrations. The most common approach is URL versioning.
5) ๐๐๐๐ต๐ก ๐ฎ๐ป๐ฑ ๐๐๐๐ต๐ญ
Implement authentication, authorization, and data encryption where necessary. Protecting sensitive data and ensuring that only authorized users can access specific API endpoints is crucial for maintaining the trust and integrity of your API.
If you liked this, consider joining 48,000+ engineers in The .NET Weekly.
โ One practical tip every Saturday
โ .NET best practices and actionable advice
โ Software Architecture deep dives and fresh ideas
Subscribe here โ https://t.co/IAdIqoDe37
What is something you do to design a good API?
The easiest way to send HTTP requests in .NET is using an HttpClient.
But there's an even simpler alternative.
The problem with HttpClient?
If used incorrectly, you could run into port exhaustion and DNS issues.
You also need to take care of serialization and deserialization manually.
Thankfully, there's an excellent library that solves this.
It's called Refit.
It's an automatic, type-safe REST API client for .NET.
In this video, I'll show you how to use Refit to easily integrate with an HTTP API.
Watch it here: https://t.co/vhWMGESVPU
๐ช๐ต๐ฎ๐ ๐ถ๐ ๐๐ฟ๐ผ๐๐-๐ข๐ฟ๐ถ๐ด๐ถ๐ป ๐ฅ๐ฒ๐๐ผ๐๐ฟ๐ฐ๐ฒ ๐ฆ๐ต๐ฎ๐ฟ๐ถ๐ป๐ด (๐๐ข๐ฅ๐ฆ)?
Browsers use CORS to prevent websites from requesting data from different URLs. A browser request includes an origin header in the request message. The browser allows the request if it gets to the server of the exact origin; if not, it blocks it.
We can deal with CORS issues on the backend. Cross-origin requests require that the values for origin and ๐๐ฐ๐ฐ๐ฒ๐๐-๐๐ผ๐ป๐๐ฟ๐ผ๐น-๐๐น๐น๐ผ๐-๐ข๐ฟ๐ถ๐ด๐ถ๐ป in the response headers match and the server sets it. When you add an origin to the backend code, the CORS middleware only permits this URL to communicate with other origins and be utilized for cross-origin resource requests.
There are two ways to fix CORS issues:
๐ญ. ๐๐ผ๐ป๐ณ๐ถ๐ด๐๐ฟ๐ฒ ๐๐ต๐ฒ ๐๐ฎ๐ฐ๐ธ๐ฒ๐ป๐ฑ ๐๏ฟฝ๏ฟฝ ๐๐น๐น๐ผ๐ ๐๐ข๐ฅ๐ฆ
The server can let all domains withย ๐๐ฐ๐ฐ๐ฒ๐๐-๐๐ผ๐ป๐๐ฟ๐ผ๐น-๐๐น๐น๐ผ๐-๐ข๐ฟ๐ถ๐ด๐ถ๐ป: *. This turns off the same-origin policy, which is not recommended. Another option would be only to allow a particular domain, which is a better option, e.g., ๐๐ฐ๐ฐ๐ฒ๐๐-๐๐ผ๐ป๐๐ฟ๐ผ๐น-๐๐น๐น๐ผ๐-๐ข๐ฟ๐ถ๐ด๐ถ๐ป: ๐ต๐๐๐ฝ๐://๐๐ผ๐บ๐ฒ๐ฑ๐ผ๐บ๐ฎ๐ถ๐ป.๐ฐ๐ผ๐บ.
๐ฎ. ๐จ๐๐ฒ ๐ฎ ๐ฃ๐ฟ๐ผ๐ ๐ ๐ฆ๐ฒ๐ฟ๐๐ฒ๐ฟ
We can use a proxy server to call external API. It acts as a middleware between the client and the server. If the server doesn't return proper headers defined by CORS, we can add them to the proxy.
Image credits: Rapid API.
#api